xref: /llvm-project/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp (revision 4eb394d6c1772dadbceb80f72bb884ba116a58d9)
1 //=== MallocChecker.cpp - A malloc/free checker -------------------*- C++ -*--//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines malloc/free checker, which checks for potential memory
11 // leaks, double free, and use-after-free problems.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "ClangSACheckers.h"
16 #include "InterCheckerAPI.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/ParentMap.h"
19 #include "clang/Basic/SourceManager.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
22 #include "clang/StaticAnalyzer/Core/Checker.h"
23 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
26 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
27 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
28 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
29 #include "llvm/ADT/ImmutableMap.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include "llvm/ADT/SmallString.h"
32 #include "llvm/ADT/StringExtras.h"
33 #include <climits>
34 
35 using namespace clang;
36 using namespace ento;
37 
38 namespace {
39 
40 // Used to check correspondence between allocators and deallocators.
41 enum AllocationFamily {
42   AF_None,
43   AF_Malloc,
44   AF_CXXNew,
45   AF_CXXNewArray,
46   AF_IfNameIndex,
47   AF_Alloca
48 };
49 
50 class RefState {
51   enum Kind { // Reference to allocated memory.
52               Allocated,
53               // Reference to released/freed memory.
54               Released,
55               // The responsibility for freeing resources has transferred from
56               // this reference. A relinquished symbol should not be freed.
57               Relinquished,
58               // We are no longer guaranteed to have observed all manipulations
59               // of this pointer/memory. For example, it could have been
60               // passed as a parameter to an opaque function.
61               Escaped
62   };
63 
64   const Stmt *S;
65   unsigned K : 2; // Kind enum, but stored as a bitfield.
66   unsigned Family : 30; // Rest of 32-bit word, currently just an allocation
67                         // family.
68 
69   RefState(Kind k, const Stmt *s, unsigned family)
70     : S(s), K(k), Family(family) {
71     assert(family != AF_None);
72   }
73 public:
74   bool isAllocated() const { return K == Allocated; }
75   bool isReleased() const { return K == Released; }
76   bool isRelinquished() const { return K == Relinquished; }
77   bool isEscaped() const { return K == Escaped; }
78   AllocationFamily getAllocationFamily() const {
79     return (AllocationFamily)Family;
80   }
81   const Stmt *getStmt() const { return S; }
82 
83   bool operator==(const RefState &X) const {
84     return K == X.K && S == X.S && Family == X.Family;
85   }
86 
87   static RefState getAllocated(unsigned family, const Stmt *s) {
88     return RefState(Allocated, s, family);
89   }
90   static RefState getReleased(unsigned family, const Stmt *s) {
91     return RefState(Released, s, family);
92   }
93   static RefState getRelinquished(unsigned family, const Stmt *s) {
94     return RefState(Relinquished, s, family);
95   }
96   static RefState getEscaped(const RefState *RS) {
97     return RefState(Escaped, RS->getStmt(), RS->getAllocationFamily());
98   }
99 
100   void Profile(llvm::FoldingSetNodeID &ID) const {
101     ID.AddInteger(K);
102     ID.AddPointer(S);
103     ID.AddInteger(Family);
104   }
105 
106   void dump(raw_ostream &OS) const {
107     switch (static_cast<Kind>(K)) {
108 #define CASE(ID) case ID: OS << #ID; break;
109     CASE(Allocated)
110     CASE(Released)
111     CASE(Relinquished)
112     CASE(Escaped)
113     }
114   }
115 
116   LLVM_DUMP_METHOD void dump() const { dump(llvm::errs()); }
117 };
118 
119 enum ReallocPairKind {
120   RPToBeFreedAfterFailure,
121   // The symbol has been freed when reallocation failed.
122   RPIsFreeOnFailure,
123   // The symbol does not need to be freed after reallocation fails.
124   RPDoNotTrackAfterFailure
125 };
126 
127 /// \class ReallocPair
128 /// \brief Stores information about the symbol being reallocated by a call to
129 /// 'realloc' to allow modeling failed reallocation later in the path.
130 struct ReallocPair {
131   // \brief The symbol which realloc reallocated.
132   SymbolRef ReallocatedSym;
133   ReallocPairKind Kind;
134 
135   ReallocPair(SymbolRef S, ReallocPairKind K) :
136     ReallocatedSym(S), Kind(K) {}
137   void Profile(llvm::FoldingSetNodeID &ID) const {
138     ID.AddInteger(Kind);
139     ID.AddPointer(ReallocatedSym);
140   }
141   bool operator==(const ReallocPair &X) const {
142     return ReallocatedSym == X.ReallocatedSym &&
143            Kind == X.Kind;
144   }
145 };
146 
147 typedef std::pair<const ExplodedNode*, const MemRegion*> LeakInfo;
148 
149 class MallocChecker : public Checker<check::DeadSymbols,
150                                      check::PointerEscape,
151                                      check::ConstPointerEscape,
152                                      check::PreStmt<ReturnStmt>,
153                                      check::PreCall,
154                                      check::PostStmt<CallExpr>,
155                                      check::PostStmt<CXXNewExpr>,
156                                      check::PreStmt<CXXDeleteExpr>,
157                                      check::PostStmt<BlockExpr>,
158                                      check::PostObjCMessage,
159                                      check::Location,
160                                      eval::Assume>
161 {
162 public:
163   MallocChecker()
164       : II_alloca(nullptr), II_malloc(nullptr), II_free(nullptr),
165         II_realloc(nullptr), II_calloc(nullptr), II_valloc(nullptr),
166         II_reallocf(nullptr), II_strndup(nullptr), II_strdup(nullptr),
167         II_kmalloc(nullptr), II_if_nameindex(nullptr),
168         II_if_freenameindex(nullptr) {}
169 
170   /// In pessimistic mode, the checker assumes that it does not know which
171   /// functions might free the memory.
172   enum CheckKind {
173     CK_MallocChecker,
174     CK_NewDeleteChecker,
175     CK_NewDeleteLeaksChecker,
176     CK_MismatchedDeallocatorChecker,
177     CK_NumCheckKinds
178   };
179 
180   enum class MemoryOperationKind {
181     MOK_Allocate,
182     MOK_Free,
183     MOK_Any
184   };
185 
186   DefaultBool IsOptimistic;
187 
188   DefaultBool ChecksEnabled[CK_NumCheckKinds];
189   CheckName CheckNames[CK_NumCheckKinds];
190   typedef llvm::SmallVector<CheckKind, CK_NumCheckKinds> CKVecTy;
191 
192   void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
193   void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
194   void checkPostStmt(const CXXNewExpr *NE, CheckerContext &C) const;
195   void checkPreStmt(const CXXDeleteExpr *DE, CheckerContext &C) const;
196   void checkPostObjCMessage(const ObjCMethodCall &Call, CheckerContext &C) const;
197   void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
198   void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
199   void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
200   ProgramStateRef evalAssume(ProgramStateRef state, SVal Cond,
201                             bool Assumption) const;
202   void checkLocation(SVal l, bool isLoad, const Stmt *S,
203                      CheckerContext &C) const;
204 
205   ProgramStateRef checkPointerEscape(ProgramStateRef State,
206                                     const InvalidatedSymbols &Escaped,
207                                     const CallEvent *Call,
208                                     PointerEscapeKind Kind) const;
209   ProgramStateRef checkConstPointerEscape(ProgramStateRef State,
210                                           const InvalidatedSymbols &Escaped,
211                                           const CallEvent *Call,
212                                           PointerEscapeKind Kind) const;
213 
214   void printState(raw_ostream &Out, ProgramStateRef State,
215                   const char *NL, const char *Sep) const override;
216 
217 private:
218   mutable std::unique_ptr<BugType> BT_DoubleFree[CK_NumCheckKinds];
219   mutable std::unique_ptr<BugType> BT_DoubleDelete;
220   mutable std::unique_ptr<BugType> BT_Leak[CK_NumCheckKinds];
221   mutable std::unique_ptr<BugType> BT_UseFree[CK_NumCheckKinds];
222   mutable std::unique_ptr<BugType> BT_BadFree[CK_NumCheckKinds];
223   mutable std::unique_ptr<BugType> BT_FreeAlloca[CK_NumCheckKinds];
224   mutable std::unique_ptr<BugType> BT_MismatchedDealloc;
225   mutable std::unique_ptr<BugType> BT_OffsetFree[CK_NumCheckKinds];
226   mutable IdentifierInfo *II_alloca, *II_malloc, *II_free, *II_realloc,
227                          *II_calloc, *II_valloc, *II_reallocf, *II_strndup,
228                          *II_strdup, *II_kmalloc, *II_if_nameindex,
229                          *II_if_freenameindex;
230   mutable Optional<uint64_t> KernelZeroFlagVal;
231 
232   void initIdentifierInfo(ASTContext &C) const;
233 
234   /// \brief Determine family of a deallocation expression.
235   AllocationFamily getAllocationFamily(CheckerContext &C, const Stmt *S) const;
236 
237   /// \brief Print names of allocators and deallocators.
238   ///
239   /// \returns true on success.
240   bool printAllocDeallocName(raw_ostream &os, CheckerContext &C,
241                              const Expr *E) const;
242 
243   /// \brief Print expected name of an allocator based on the deallocator's
244   /// family derived from the DeallocExpr.
245   void printExpectedAllocName(raw_ostream &os, CheckerContext &C,
246                               const Expr *DeallocExpr) const;
247   /// \brief Print expected name of a deallocator based on the allocator's
248   /// family.
249   void printExpectedDeallocName(raw_ostream &os, AllocationFamily Family) const;
250 
251   ///@{
252   /// Check if this is one of the functions which can allocate/reallocate memory
253   /// pointed to by one of its arguments.
254   bool isMemFunction(const FunctionDecl *FD, ASTContext &C) const;
255   bool isCMemFunction(const FunctionDecl *FD,
256                       ASTContext &C,
257                       AllocationFamily Family,
258                       MemoryOperationKind MemKind) const;
259   bool isStandardNewDelete(const FunctionDecl *FD, ASTContext &C) const;
260   ///@}
261   ProgramStateRef MallocMemReturnsAttr(CheckerContext &C,
262                                        const CallExpr *CE,
263                                        const OwnershipAttr* Att,
264                                        ProgramStateRef State) const;
265   static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE,
266                                       const Expr *SizeEx, SVal Init,
267                                       ProgramStateRef State,
268                                       AllocationFamily Family = AF_Malloc);
269   static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE,
270                                       SVal SizeEx, SVal Init,
271                                       ProgramStateRef State,
272                                       AllocationFamily Family = AF_Malloc);
273 
274   // Check if this malloc() for special flags. At present that means M_ZERO or
275   // __GFP_ZERO (in which case, treat it like calloc).
276   llvm::Optional<ProgramStateRef>
277   performKernelMalloc(const CallExpr *CE, CheckerContext &C,
278                       const ProgramStateRef &State) const;
279 
280   /// Update the RefState to reflect the new memory allocation.
281   static ProgramStateRef
282   MallocUpdateRefState(CheckerContext &C, const Expr *E, ProgramStateRef State,
283                        AllocationFamily Family = AF_Malloc);
284 
285   ProgramStateRef FreeMemAttr(CheckerContext &C, const CallExpr *CE,
286                               const OwnershipAttr* Att,
287                               ProgramStateRef State) const;
288   ProgramStateRef FreeMemAux(CheckerContext &C, const CallExpr *CE,
289                              ProgramStateRef state, unsigned Num,
290                              bool Hold,
291                              bool &ReleasedAllocated,
292                              bool ReturnsNullOnFailure = false) const;
293   ProgramStateRef FreeMemAux(CheckerContext &C, const Expr *Arg,
294                              const Expr *ParentExpr,
295                              ProgramStateRef State,
296                              bool Hold,
297                              bool &ReleasedAllocated,
298                              bool ReturnsNullOnFailure = false) const;
299 
300   ProgramStateRef ReallocMem(CheckerContext &C, const CallExpr *CE,
301                              bool FreesMemOnFailure,
302                              ProgramStateRef State) const;
303   static ProgramStateRef CallocMem(CheckerContext &C, const CallExpr *CE,
304                                    ProgramStateRef State);
305 
306   ///\brief Check if the memory associated with this symbol was released.
307   bool isReleased(SymbolRef Sym, CheckerContext &C) const;
308 
309   bool checkUseAfterFree(SymbolRef Sym, CheckerContext &C, const Stmt *S) const;
310 
311   bool checkDoubleDelete(SymbolRef Sym, CheckerContext &C) const;
312 
313   /// Check if the function is known free memory, or if it is
314   /// "interesting" and should be modeled explicitly.
315   ///
316   /// \param [out] EscapingSymbol A function might not free memory in general,
317   ///   but could be known to free a particular symbol. In this case, false is
318   ///   returned and the single escaping symbol is returned through the out
319   ///   parameter.
320   ///
321   /// We assume that pointers do not escape through calls to system functions
322   /// not handled by this checker.
323   bool mayFreeAnyEscapedMemoryOrIsModeledExplicitly(const CallEvent *Call,
324                                    ProgramStateRef State,
325                                    SymbolRef &EscapingSymbol) const;
326 
327   // Implementation of the checkPointerEscape callabcks.
328   ProgramStateRef checkPointerEscapeAux(ProgramStateRef State,
329                                   const InvalidatedSymbols &Escaped,
330                                   const CallEvent *Call,
331                                   PointerEscapeKind Kind,
332                                   bool(*CheckRefState)(const RefState*)) const;
333 
334   ///@{
335   /// Tells if a given family/call/symbol is tracked by the current checker.
336   /// Sets CheckKind to the kind of the checker responsible for this
337   /// family/call/symbol.
338   Optional<CheckKind> getCheckIfTracked(AllocationFamily Family) const;
339   Optional<CheckKind> getCheckIfTracked(CheckerContext &C,
340                                         const Stmt *AllocDeallocStmt) const;
341   Optional<CheckKind> getCheckIfTracked(CheckerContext &C, SymbolRef Sym) const;
342   ///@}
343   static bool SummarizeValue(raw_ostream &os, SVal V);
344   static bool SummarizeRegion(raw_ostream &os, const MemRegion *MR);
345   void ReportBadFree(CheckerContext &C, SVal ArgVal, SourceRange Range,
346                      const Expr *DeallocExpr) const;
347   void ReportFreeAlloca(CheckerContext &C, SVal ArgVal,
348                         SourceRange Range) const;
349   void ReportMismatchedDealloc(CheckerContext &C, SourceRange Range,
350                                const Expr *DeallocExpr, const RefState *RS,
351                                SymbolRef Sym, bool OwnershipTransferred) const;
352   void ReportOffsetFree(CheckerContext &C, SVal ArgVal, SourceRange Range,
353                         const Expr *DeallocExpr,
354                         const Expr *AllocExpr = nullptr) const;
355   void ReportUseAfterFree(CheckerContext &C, SourceRange Range,
356                           SymbolRef Sym) const;
357   void ReportDoubleFree(CheckerContext &C, SourceRange Range, bool Released,
358                         SymbolRef Sym, SymbolRef PrevSym) const;
359 
360   void ReportDoubleDelete(CheckerContext &C, SymbolRef Sym) const;
361 
362   /// Find the location of the allocation for Sym on the path leading to the
363   /// exploded node N.
364   LeakInfo getAllocationSite(const ExplodedNode *N, SymbolRef Sym,
365                              CheckerContext &C) const;
366 
367   void reportLeak(SymbolRef Sym, ExplodedNode *N, CheckerContext &C) const;
368 
369   /// The bug visitor which allows us to print extra diagnostics along the
370   /// BugReport path. For example, showing the allocation site of the leaked
371   /// region.
372   class MallocBugVisitor : public BugReporterVisitorImpl<MallocBugVisitor> {
373   protected:
374     enum NotificationMode {
375       Normal,
376       ReallocationFailed
377     };
378 
379     // The allocated region symbol tracked by the main analysis.
380     SymbolRef Sym;
381 
382     // The mode we are in, i.e. what kind of diagnostics will be emitted.
383     NotificationMode Mode;
384 
385     // A symbol from when the primary region should have been reallocated.
386     SymbolRef FailedReallocSymbol;
387 
388     bool IsLeak;
389 
390   public:
391     MallocBugVisitor(SymbolRef S, bool isLeak = false)
392        : Sym(S), Mode(Normal), FailedReallocSymbol(nullptr), IsLeak(isLeak) {}
393 
394     virtual ~MallocBugVisitor() {}
395 
396     void Profile(llvm::FoldingSetNodeID &ID) const override {
397       static int X = 0;
398       ID.AddPointer(&X);
399       ID.AddPointer(Sym);
400     }
401 
402     inline bool isAllocated(const RefState *S, const RefState *SPrev,
403                             const Stmt *Stmt) {
404       // Did not track -> allocated. Other state (released) -> allocated.
405       return (Stmt && (isa<CallExpr>(Stmt) || isa<CXXNewExpr>(Stmt)) &&
406               (S && S->isAllocated()) && (!SPrev || !SPrev->isAllocated()));
407     }
408 
409     inline bool isReleased(const RefState *S, const RefState *SPrev,
410                            const Stmt *Stmt) {
411       // Did not track -> released. Other state (allocated) -> released.
412       return (Stmt && (isa<CallExpr>(Stmt) || isa<CXXDeleteExpr>(Stmt)) &&
413               (S && S->isReleased()) && (!SPrev || !SPrev->isReleased()));
414     }
415 
416     inline bool isRelinquished(const RefState *S, const RefState *SPrev,
417                                const Stmt *Stmt) {
418       // Did not track -> relinquished. Other state (allocated) -> relinquished.
419       return (Stmt && (isa<CallExpr>(Stmt) || isa<ObjCMessageExpr>(Stmt) ||
420                                               isa<ObjCPropertyRefExpr>(Stmt)) &&
421               (S && S->isRelinquished()) &&
422               (!SPrev || !SPrev->isRelinquished()));
423     }
424 
425     inline bool isReallocFailedCheck(const RefState *S, const RefState *SPrev,
426                                      const Stmt *Stmt) {
427       // If the expression is not a call, and the state change is
428       // released -> allocated, it must be the realloc return value
429       // check. If we have to handle more cases here, it might be cleaner just
430       // to track this extra bit in the state itself.
431       return ((!Stmt || !isa<CallExpr>(Stmt)) &&
432               (S && S->isAllocated()) && (SPrev && !SPrev->isAllocated()));
433     }
434 
435     PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
436                                    const ExplodedNode *PrevN,
437                                    BugReporterContext &BRC,
438                                    BugReport &BR) override;
439 
440     std::unique_ptr<PathDiagnosticPiece>
441     getEndPath(BugReporterContext &BRC, const ExplodedNode *EndPathNode,
442                BugReport &BR) override {
443       if (!IsLeak)
444         return nullptr;
445 
446       PathDiagnosticLocation L =
447         PathDiagnosticLocation::createEndOfPath(EndPathNode,
448                                                 BRC.getSourceManager());
449       // Do not add the statement itself as a range in case of leak.
450       return llvm::make_unique<PathDiagnosticEventPiece>(L, BR.getDescription(),
451                                                          false);
452     }
453 
454   private:
455     class StackHintGeneratorForReallocationFailed
456         : public StackHintGeneratorForSymbol {
457     public:
458       StackHintGeneratorForReallocationFailed(SymbolRef S, StringRef M)
459         : StackHintGeneratorForSymbol(S, M) {}
460 
461       std::string getMessageForArg(const Expr *ArgE,
462                                    unsigned ArgIndex) override {
463         // Printed parameters start at 1, not 0.
464         ++ArgIndex;
465 
466         SmallString<200> buf;
467         llvm::raw_svector_ostream os(buf);
468 
469         os << "Reallocation of " << ArgIndex << llvm::getOrdinalSuffix(ArgIndex)
470            << " parameter failed";
471 
472         return os.str();
473       }
474 
475       std::string getMessageForReturn(const CallExpr *CallExpr) override {
476         return "Reallocation of returned value failed";
477       }
478     };
479   };
480 };
481 } // end anonymous namespace
482 
483 REGISTER_MAP_WITH_PROGRAMSTATE(RegionState, SymbolRef, RefState)
484 REGISTER_MAP_WITH_PROGRAMSTATE(ReallocPairs, SymbolRef, ReallocPair)
485 
486 // A map from the freed symbol to the symbol representing the return value of
487 // the free function.
488 REGISTER_MAP_WITH_PROGRAMSTATE(FreeReturnValue, SymbolRef, SymbolRef)
489 
490 namespace {
491 class StopTrackingCallback : public SymbolVisitor {
492   ProgramStateRef state;
493 public:
494   StopTrackingCallback(ProgramStateRef st) : state(st) {}
495   ProgramStateRef getState() const { return state; }
496 
497   bool VisitSymbol(SymbolRef sym) override {
498     state = state->remove<RegionState>(sym);
499     return true;
500   }
501 };
502 } // end anonymous namespace
503 
504 void MallocChecker::initIdentifierInfo(ASTContext &Ctx) const {
505   if (II_malloc)
506     return;
507   II_alloca = &Ctx.Idents.get("alloca");
508   II_malloc = &Ctx.Idents.get("malloc");
509   II_free = &Ctx.Idents.get("free");
510   II_realloc = &Ctx.Idents.get("realloc");
511   II_reallocf = &Ctx.Idents.get("reallocf");
512   II_calloc = &Ctx.Idents.get("calloc");
513   II_valloc = &Ctx.Idents.get("valloc");
514   II_strdup = &Ctx.Idents.get("strdup");
515   II_strndup = &Ctx.Idents.get("strndup");
516   II_kmalloc = &Ctx.Idents.get("kmalloc");
517   II_if_nameindex = &Ctx.Idents.get("if_nameindex");
518   II_if_freenameindex = &Ctx.Idents.get("if_freenameindex");
519 }
520 
521 bool MallocChecker::isMemFunction(const FunctionDecl *FD, ASTContext &C) const {
522   if (isCMemFunction(FD, C, AF_Malloc, MemoryOperationKind::MOK_Any))
523     return true;
524 
525   if (isCMemFunction(FD, C, AF_IfNameIndex, MemoryOperationKind::MOK_Any))
526     return true;
527 
528   if (isCMemFunction(FD, C, AF_Alloca, MemoryOperationKind::MOK_Any))
529     return true;
530 
531   if (isStandardNewDelete(FD, C))
532     return true;
533 
534   return false;
535 }
536 
537 bool MallocChecker::isCMemFunction(const FunctionDecl *FD,
538                                    ASTContext &C,
539                                    AllocationFamily Family,
540                                    MemoryOperationKind MemKind) const {
541   if (!FD)
542     return false;
543 
544   bool CheckFree = (MemKind == MemoryOperationKind::MOK_Any ||
545                     MemKind == MemoryOperationKind::MOK_Free);
546   bool CheckAlloc = (MemKind == MemoryOperationKind::MOK_Any ||
547                      MemKind == MemoryOperationKind::MOK_Allocate);
548 
549   if (FD->getKind() == Decl::Function) {
550     const IdentifierInfo *FunI = FD->getIdentifier();
551     initIdentifierInfo(C);
552 
553     if (Family == AF_Malloc && CheckFree) {
554       if (FunI == II_free || FunI == II_realloc || FunI == II_reallocf)
555         return true;
556     }
557 
558     if (Family == AF_Malloc && CheckAlloc) {
559       if (FunI == II_malloc || FunI == II_realloc || FunI == II_reallocf ||
560           FunI == II_calloc || FunI == II_valloc || FunI == II_strdup ||
561           FunI == II_strndup || FunI == II_kmalloc)
562         return true;
563     }
564 
565     if (Family == AF_IfNameIndex && CheckFree) {
566       if (FunI == II_if_freenameindex)
567         return true;
568     }
569 
570     if (Family == AF_IfNameIndex && CheckAlloc) {
571       if (FunI == II_if_nameindex)
572         return true;
573     }
574 
575     if (Family == AF_Alloca && CheckAlloc) {
576       if (FunI == II_alloca)
577         return true;
578     }
579   }
580 
581   if (Family != AF_Malloc)
582     return false;
583 
584   if (IsOptimistic && FD->hasAttrs()) {
585     for (const auto *I : FD->specific_attrs<OwnershipAttr>()) {
586       OwnershipAttr::OwnershipKind OwnKind = I->getOwnKind();
587       if(OwnKind == OwnershipAttr::Takes || OwnKind == OwnershipAttr::Holds) {
588         if (CheckFree)
589           return true;
590       } else if (OwnKind == OwnershipAttr::Returns) {
591         if (CheckAlloc)
592           return true;
593       }
594     }
595   }
596 
597   return false;
598 }
599 
600 // Tells if the callee is one of the following:
601 // 1) A global non-placement new/delete operator function.
602 // 2) A global placement operator function with the single placement argument
603 //    of type std::nothrow_t.
604 bool MallocChecker::isStandardNewDelete(const FunctionDecl *FD,
605                                         ASTContext &C) const {
606   if (!FD)
607     return false;
608 
609   OverloadedOperatorKind Kind = FD->getOverloadedOperator();
610   if (Kind != OO_New && Kind != OO_Array_New &&
611       Kind != OO_Delete && Kind != OO_Array_Delete)
612     return false;
613 
614   // Skip all operator new/delete methods.
615   if (isa<CXXMethodDecl>(FD))
616     return false;
617 
618   // Return true if tested operator is a standard placement nothrow operator.
619   if (FD->getNumParams() == 2) {
620     QualType T = FD->getParamDecl(1)->getType();
621     if (const IdentifierInfo *II = T.getBaseTypeIdentifier())
622       return II->getName().equals("nothrow_t");
623   }
624 
625   // Skip placement operators.
626   if (FD->getNumParams() != 1 || FD->isVariadic())
627     return false;
628 
629   // One of the standard new/new[]/delete/delete[] non-placement operators.
630   return true;
631 }
632 
633 llvm::Optional<ProgramStateRef> MallocChecker::performKernelMalloc(
634   const CallExpr *CE, CheckerContext &C, const ProgramStateRef &State) const {
635   // 3-argument malloc(), as commonly used in {Free,Net,Open}BSD Kernels:
636   //
637   // void *malloc(unsigned long size, struct malloc_type *mtp, int flags);
638   //
639   // One of the possible flags is M_ZERO, which means 'give me back an
640   // allocation which is already zeroed', like calloc.
641 
642   // 2-argument kmalloc(), as used in the Linux kernel:
643   //
644   // void *kmalloc(size_t size, gfp_t flags);
645   //
646   // Has the similar flag value __GFP_ZERO.
647 
648   // This logic is largely cloned from O_CREAT in UnixAPIChecker, maybe some
649   // code could be shared.
650 
651   ASTContext &Ctx = C.getASTContext();
652   llvm::Triple::OSType OS = Ctx.getTargetInfo().getTriple().getOS();
653 
654   if (!KernelZeroFlagVal.hasValue()) {
655     if (OS == llvm::Triple::FreeBSD)
656       KernelZeroFlagVal = 0x0100;
657     else if (OS == llvm::Triple::NetBSD)
658       KernelZeroFlagVal = 0x0002;
659     else if (OS == llvm::Triple::OpenBSD)
660       KernelZeroFlagVal = 0x0008;
661     else if (OS == llvm::Triple::Linux)
662       // __GFP_ZERO
663       KernelZeroFlagVal = 0x8000;
664     else
665       // FIXME: We need a more general way of getting the M_ZERO value.
666       // See also: O_CREAT in UnixAPIChecker.cpp.
667 
668       // Fall back to normal malloc behavior on platforms where we don't
669       // know M_ZERO.
670       return None;
671   }
672 
673   // We treat the last argument as the flags argument, and callers fall-back to
674   // normal malloc on a None return. This works for the FreeBSD kernel malloc
675   // as well as Linux kmalloc.
676   if (CE->getNumArgs() < 2)
677     return None;
678 
679   const Expr *FlagsEx = CE->getArg(CE->getNumArgs() - 1);
680   const SVal V = State->getSVal(FlagsEx, C.getLocationContext());
681   if (!V.getAs<NonLoc>()) {
682     // The case where 'V' can be a location can only be due to a bad header,
683     // so in this case bail out.
684     return None;
685   }
686 
687   NonLoc Flags = V.castAs<NonLoc>();
688   NonLoc ZeroFlag = C.getSValBuilder()
689       .makeIntVal(KernelZeroFlagVal.getValue(), FlagsEx->getType())
690       .castAs<NonLoc>();
691   SVal MaskedFlagsUC = C.getSValBuilder().evalBinOpNN(State, BO_And,
692                                                       Flags, ZeroFlag,
693                                                       FlagsEx->getType());
694   if (MaskedFlagsUC.isUnknownOrUndef())
695     return None;
696   DefinedSVal MaskedFlags = MaskedFlagsUC.castAs<DefinedSVal>();
697 
698   // Check if maskedFlags is non-zero.
699   ProgramStateRef TrueState, FalseState;
700   std::tie(TrueState, FalseState) = State->assume(MaskedFlags);
701 
702   // If M_ZERO is set, treat this like calloc (initialized).
703   if (TrueState && !FalseState) {
704     SVal ZeroVal = C.getSValBuilder().makeZeroVal(Ctx.CharTy);
705     return MallocMemAux(C, CE, CE->getArg(0), ZeroVal, TrueState);
706   }
707 
708   return None;
709 }
710 
711 void MallocChecker::checkPostStmt(const CallExpr *CE, CheckerContext &C) const {
712   if (C.wasInlined)
713     return;
714 
715   const FunctionDecl *FD = C.getCalleeDecl(CE);
716   if (!FD)
717     return;
718 
719   ProgramStateRef State = C.getState();
720   bool ReleasedAllocatedMemory = false;
721 
722   if (FD->getKind() == Decl::Function) {
723     initIdentifierInfo(C.getASTContext());
724     IdentifierInfo *FunI = FD->getIdentifier();
725 
726     if (FunI == II_malloc) {
727       if (CE->getNumArgs() < 1)
728         return;
729       if (CE->getNumArgs() < 3) {
730         State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State);
731       } else if (CE->getNumArgs() == 3) {
732         llvm::Optional<ProgramStateRef> MaybeState =
733           performKernelMalloc(CE, C, State);
734         if (MaybeState.hasValue())
735           State = MaybeState.getValue();
736         else
737           State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State);
738       }
739     } else if (FunI == II_kmalloc) {
740       llvm::Optional<ProgramStateRef> MaybeState =
741         performKernelMalloc(CE, C, State);
742       if (MaybeState.hasValue())
743         State = MaybeState.getValue();
744       else
745         State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State);
746     } else if (FunI == II_valloc) {
747       if (CE->getNumArgs() < 1)
748         return;
749       State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State);
750     } else if (FunI == II_realloc) {
751       State = ReallocMem(C, CE, false, State);
752     } else if (FunI == II_reallocf) {
753       State = ReallocMem(C, CE, true, State);
754     } else if (FunI == II_calloc) {
755       State = CallocMem(C, CE, State);
756     } else if (FunI == II_free) {
757       State = FreeMemAux(C, CE, State, 0, false, ReleasedAllocatedMemory);
758     } else if (FunI == II_strdup) {
759       State = MallocUpdateRefState(C, CE, State);
760     } else if (FunI == II_strndup) {
761       State = MallocUpdateRefState(C, CE, State);
762     } else if (FunI == II_alloca) {
763       State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State,
764                            AF_Alloca);
765     } else if (isStandardNewDelete(FD, C.getASTContext())) {
766       // Process direct calls to operator new/new[]/delete/delete[] functions
767       // as distinct from new/new[]/delete/delete[] expressions that are
768       // processed by the checkPostStmt callbacks for CXXNewExpr and
769       // CXXDeleteExpr.
770       OverloadedOperatorKind K = FD->getOverloadedOperator();
771       if (K == OO_New)
772         State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State,
773                              AF_CXXNew);
774       else if (K == OO_Array_New)
775         State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State,
776                              AF_CXXNewArray);
777       else if (K == OO_Delete || K == OO_Array_Delete)
778         State = FreeMemAux(C, CE, State, 0, false, ReleasedAllocatedMemory);
779       else
780         llvm_unreachable("not a new/delete operator");
781     } else if (FunI == II_if_nameindex) {
782       // Should we model this differently? We can allocate a fixed number of
783       // elements with zeros in the last one.
784       State = MallocMemAux(C, CE, UnknownVal(), UnknownVal(), State,
785                            AF_IfNameIndex);
786     } else if (FunI == II_if_freenameindex) {
787       State = FreeMemAux(C, CE, State, 0, false, ReleasedAllocatedMemory);
788     }
789   }
790 
791   if (IsOptimistic || ChecksEnabled[CK_MismatchedDeallocatorChecker]) {
792     // Check all the attributes, if there are any.
793     // There can be multiple of these attributes.
794     if (FD->hasAttrs())
795       for (const auto *I : FD->specific_attrs<OwnershipAttr>()) {
796         switch (I->getOwnKind()) {
797         case OwnershipAttr::Returns:
798           State = MallocMemReturnsAttr(C, CE, I, State);
799           break;
800         case OwnershipAttr::Takes:
801         case OwnershipAttr::Holds:
802           State = FreeMemAttr(C, CE, I, State);
803           break;
804         }
805       }
806   }
807   C.addTransition(State);
808 }
809 
810 static QualType getDeepPointeeType(QualType T) {
811   QualType Result = T, PointeeType = T->getPointeeType();
812   while (!PointeeType.isNull()) {
813     Result = PointeeType;
814     PointeeType = PointeeType->getPointeeType();
815   }
816   return Result;
817 }
818 
819 static bool treatUnusedNewEscaped(const CXXNewExpr *NE) {
820 
821   const CXXConstructExpr *ConstructE = NE->getConstructExpr();
822   if (!ConstructE)
823     return false;
824 
825   if (!NE->getAllocatedType()->getAsCXXRecordDecl())
826     return false;
827 
828   const CXXConstructorDecl *CtorD = ConstructE->getConstructor();
829 
830   // Iterate over the constructor parameters.
831   for (const auto *CtorParam : CtorD->params()) {
832 
833     QualType CtorParamPointeeT = CtorParam->getType()->getPointeeType();
834     if (CtorParamPointeeT.isNull())
835       continue;
836 
837     CtorParamPointeeT = getDeepPointeeType(CtorParamPointeeT);
838 
839     if (CtorParamPointeeT->getAsCXXRecordDecl())
840       return true;
841   }
842 
843   return false;
844 }
845 
846 void MallocChecker::checkPostStmt(const CXXNewExpr *NE,
847                                   CheckerContext &C) const {
848 
849   if (NE->getNumPlacementArgs())
850     for (CXXNewExpr::const_arg_iterator I = NE->placement_arg_begin(),
851          E = NE->placement_arg_end(); I != E; ++I)
852       if (SymbolRef Sym = C.getSVal(*I).getAsSymbol())
853         checkUseAfterFree(Sym, C, *I);
854 
855   if (!isStandardNewDelete(NE->getOperatorNew(), C.getASTContext()))
856     return;
857 
858   ParentMap &PM = C.getLocationContext()->getParentMap();
859   if (!PM.isConsumedExpr(NE) && treatUnusedNewEscaped(NE))
860     return;
861 
862   ProgramStateRef State = C.getState();
863   // The return value from operator new is bound to a specified initialization
864   // value (if any) and we don't want to loose this value. So we call
865   // MallocUpdateRefState() instead of MallocMemAux() which breakes the
866   // existing binding.
867   State = MallocUpdateRefState(C, NE, State, NE->isArray() ? AF_CXXNewArray
868                                                            : AF_CXXNew);
869   C.addTransition(State);
870 }
871 
872 void MallocChecker::checkPreStmt(const CXXDeleteExpr *DE,
873                                  CheckerContext &C) const {
874 
875   if (!ChecksEnabled[CK_NewDeleteChecker])
876     if (SymbolRef Sym = C.getSVal(DE->getArgument()).getAsSymbol())
877       checkUseAfterFree(Sym, C, DE->getArgument());
878 
879   if (!isStandardNewDelete(DE->getOperatorDelete(), C.getASTContext()))
880     return;
881 
882   ProgramStateRef State = C.getState();
883   bool ReleasedAllocated;
884   State = FreeMemAux(C, DE->getArgument(), DE, State,
885                      /*Hold*/false, ReleasedAllocated);
886 
887   C.addTransition(State);
888 }
889 
890 static bool isKnownDeallocObjCMethodName(const ObjCMethodCall &Call) {
891   // If the first selector piece is one of the names below, assume that the
892   // object takes ownership of the memory, promising to eventually deallocate it
893   // with free().
894   // Ex:  [NSData dataWithBytesNoCopy:bytes length:10];
895   // (...unless a 'freeWhenDone' parameter is false, but that's checked later.)
896   StringRef FirstSlot = Call.getSelector().getNameForSlot(0);
897   if (FirstSlot == "dataWithBytesNoCopy" ||
898       FirstSlot == "initWithBytesNoCopy" ||
899       FirstSlot == "initWithCharactersNoCopy")
900     return true;
901 
902   return false;
903 }
904 
905 static Optional<bool> getFreeWhenDoneArg(const ObjCMethodCall &Call) {
906   Selector S = Call.getSelector();
907 
908   // FIXME: We should not rely on fully-constrained symbols being folded.
909   for (unsigned i = 1; i < S.getNumArgs(); ++i)
910     if (S.getNameForSlot(i).equals("freeWhenDone"))
911       return !Call.getArgSVal(i).isZeroConstant();
912 
913   return None;
914 }
915 
916 void MallocChecker::checkPostObjCMessage(const ObjCMethodCall &Call,
917                                          CheckerContext &C) const {
918   if (C.wasInlined)
919     return;
920 
921   if (!isKnownDeallocObjCMethodName(Call))
922     return;
923 
924   if (Optional<bool> FreeWhenDone = getFreeWhenDoneArg(Call))
925     if (!*FreeWhenDone)
926       return;
927 
928   bool ReleasedAllocatedMemory;
929   ProgramStateRef State = FreeMemAux(C, Call.getArgExpr(0),
930                                      Call.getOriginExpr(), C.getState(),
931                                      /*Hold=*/true, ReleasedAllocatedMemory,
932                                      /*RetNullOnFailure=*/true);
933 
934   C.addTransition(State);
935 }
936 
937 ProgramStateRef
938 MallocChecker::MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,
939                                     const OwnershipAttr *Att,
940                                     ProgramStateRef State) const {
941   if (!State)
942     return nullptr;
943 
944   if (Att->getModule() != II_malloc)
945     return nullptr;
946 
947   OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
948   if (I != E) {
949     return MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), State);
950   }
951   return MallocMemAux(C, CE, UnknownVal(), UndefinedVal(), State);
952 }
953 
954 ProgramStateRef MallocChecker::MallocMemAux(CheckerContext &C,
955                                             const CallExpr *CE,
956                                             const Expr *SizeEx, SVal Init,
957                                             ProgramStateRef State,
958                                             AllocationFamily Family) {
959   if (!State)
960     return nullptr;
961 
962   return MallocMemAux(C, CE, State->getSVal(SizeEx, C.getLocationContext()),
963                       Init, State, Family);
964 }
965 
966 ProgramStateRef MallocChecker::MallocMemAux(CheckerContext &C,
967                                            const CallExpr *CE,
968                                            SVal Size, SVal Init,
969                                            ProgramStateRef State,
970                                            AllocationFamily Family) {
971   if (!State)
972     return nullptr;
973 
974   // We expect the malloc functions to return a pointer.
975   if (!Loc::isLocType(CE->getType()))
976     return nullptr;
977 
978   // Bind the return value to the symbolic value from the heap region.
979   // TODO: We could rewrite post visit to eval call; 'malloc' does not have
980   // side effects other than what we model here.
981   unsigned Count = C.blockCount();
982   SValBuilder &svalBuilder = C.getSValBuilder();
983   const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
984   DefinedSVal RetVal = svalBuilder.getConjuredHeapSymbolVal(CE, LCtx, Count)
985       .castAs<DefinedSVal>();
986   State = State->BindExpr(CE, C.getLocationContext(), RetVal);
987 
988   // Fill the region with the initialization value.
989   State = State->bindDefault(RetVal, Init);
990 
991   // Set the region's extent equal to the Size parameter.
992   const SymbolicRegion *R =
993       dyn_cast_or_null<SymbolicRegion>(RetVal.getAsRegion());
994   if (!R)
995     return nullptr;
996   if (Optional<DefinedOrUnknownSVal> DefinedSize =
997           Size.getAs<DefinedOrUnknownSVal>()) {
998     SValBuilder &svalBuilder = C.getSValBuilder();
999     DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
1000     DefinedOrUnknownSVal extentMatchesSize =
1001         svalBuilder.evalEQ(State, Extent, *DefinedSize);
1002 
1003     State = State->assume(extentMatchesSize, true);
1004     assert(State);
1005   }
1006 
1007   return MallocUpdateRefState(C, CE, State, Family);
1008 }
1009 
1010 ProgramStateRef MallocChecker::MallocUpdateRefState(CheckerContext &C,
1011                                                     const Expr *E,
1012                                                     ProgramStateRef State,
1013                                                     AllocationFamily Family) {
1014   if (!State)
1015     return nullptr;
1016 
1017   // Get the return value.
1018   SVal retVal = State->getSVal(E, C.getLocationContext());
1019 
1020   // We expect the malloc functions to return a pointer.
1021   if (!retVal.getAs<Loc>())
1022     return nullptr;
1023 
1024   SymbolRef Sym = retVal.getAsLocSymbol();
1025   assert(Sym);
1026 
1027   // Set the symbol's state to Allocated.
1028   return State->set<RegionState>(Sym, RefState::getAllocated(Family, E));
1029 }
1030 
1031 ProgramStateRef MallocChecker::FreeMemAttr(CheckerContext &C,
1032                                            const CallExpr *CE,
1033                                            const OwnershipAttr *Att,
1034                                            ProgramStateRef State) const {
1035   if (!State)
1036     return nullptr;
1037 
1038   if (Att->getModule() != II_malloc)
1039     return nullptr;
1040 
1041   bool ReleasedAllocated = false;
1042 
1043   for (const auto &Arg : Att->args()) {
1044     ProgramStateRef StateI = FreeMemAux(C, CE, State, Arg,
1045                                Att->getOwnKind() == OwnershipAttr::Holds,
1046                                ReleasedAllocated);
1047     if (StateI)
1048       State = StateI;
1049   }
1050   return State;
1051 }
1052 
1053 ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C,
1054                                           const CallExpr *CE,
1055                                           ProgramStateRef State,
1056                                           unsigned Num,
1057                                           bool Hold,
1058                                           bool &ReleasedAllocated,
1059                                           bool ReturnsNullOnFailure) const {
1060   if (!State)
1061     return nullptr;
1062 
1063   if (CE->getNumArgs() < (Num + 1))
1064     return nullptr;
1065 
1066   return FreeMemAux(C, CE->getArg(Num), CE, State, Hold,
1067                     ReleasedAllocated, ReturnsNullOnFailure);
1068 }
1069 
1070 /// Checks if the previous call to free on the given symbol failed - if free
1071 /// failed, returns true. Also, returns the corresponding return value symbol.
1072 static bool didPreviousFreeFail(ProgramStateRef State,
1073                                 SymbolRef Sym, SymbolRef &RetStatusSymbol) {
1074   const SymbolRef *Ret = State->get<FreeReturnValue>(Sym);
1075   if (Ret) {
1076     assert(*Ret && "We should not store the null return symbol");
1077     ConstraintManager &CMgr = State->getConstraintManager();
1078     ConditionTruthVal FreeFailed = CMgr.isNull(State, *Ret);
1079     RetStatusSymbol = *Ret;
1080     return FreeFailed.isConstrainedTrue();
1081   }
1082   return false;
1083 }
1084 
1085 AllocationFamily MallocChecker::getAllocationFamily(CheckerContext &C,
1086                                                     const Stmt *S) const {
1087   if (!S)
1088     return AF_None;
1089 
1090   if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
1091     const FunctionDecl *FD = C.getCalleeDecl(CE);
1092 
1093     if (!FD)
1094       FD = dyn_cast<FunctionDecl>(CE->getCalleeDecl());
1095 
1096     ASTContext &Ctx = C.getASTContext();
1097 
1098     if (isCMemFunction(FD, Ctx, AF_Malloc, MemoryOperationKind::MOK_Any))
1099       return AF_Malloc;
1100 
1101     if (isStandardNewDelete(FD, Ctx)) {
1102       OverloadedOperatorKind Kind = FD->getOverloadedOperator();
1103       if (Kind == OO_New || Kind == OO_Delete)
1104         return AF_CXXNew;
1105       else if (Kind == OO_Array_New || Kind == OO_Array_Delete)
1106         return AF_CXXNewArray;
1107     }
1108 
1109     if (isCMemFunction(FD, Ctx, AF_IfNameIndex, MemoryOperationKind::MOK_Any))
1110       return AF_IfNameIndex;
1111 
1112     if (isCMemFunction(FD, Ctx, AF_Alloca, MemoryOperationKind::MOK_Any))
1113       return AF_Alloca;
1114 
1115     return AF_None;
1116   }
1117 
1118   if (const CXXNewExpr *NE = dyn_cast<CXXNewExpr>(S))
1119     return NE->isArray() ? AF_CXXNewArray : AF_CXXNew;
1120 
1121   if (const CXXDeleteExpr *DE = dyn_cast<CXXDeleteExpr>(S))
1122     return DE->isArrayForm() ? AF_CXXNewArray : AF_CXXNew;
1123 
1124   if (isa<ObjCMessageExpr>(S))
1125     return AF_Malloc;
1126 
1127   return AF_None;
1128 }
1129 
1130 bool MallocChecker::printAllocDeallocName(raw_ostream &os, CheckerContext &C,
1131                                           const Expr *E) const {
1132   if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1133     // FIXME: This doesn't handle indirect calls.
1134     const FunctionDecl *FD = CE->getDirectCallee();
1135     if (!FD)
1136       return false;
1137 
1138     os << *FD;
1139     if (!FD->isOverloadedOperator())
1140       os << "()";
1141     return true;
1142   }
1143 
1144   if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) {
1145     if (Msg->isInstanceMessage())
1146       os << "-";
1147     else
1148       os << "+";
1149     Msg->getSelector().print(os);
1150     return true;
1151   }
1152 
1153   if (const CXXNewExpr *NE = dyn_cast<CXXNewExpr>(E)) {
1154     os << "'"
1155        << getOperatorSpelling(NE->getOperatorNew()->getOverloadedOperator())
1156        << "'";
1157     return true;
1158   }
1159 
1160   if (const CXXDeleteExpr *DE = dyn_cast<CXXDeleteExpr>(E)) {
1161     os << "'"
1162        << getOperatorSpelling(DE->getOperatorDelete()->getOverloadedOperator())
1163        << "'";
1164     return true;
1165   }
1166 
1167   return false;
1168 }
1169 
1170 void MallocChecker::printExpectedAllocName(raw_ostream &os, CheckerContext &C,
1171                                            const Expr *E) const {
1172   AllocationFamily Family = getAllocationFamily(C, E);
1173 
1174   switch(Family) {
1175     case AF_Malloc: os << "malloc()"; return;
1176     case AF_CXXNew: os << "'new'"; return;
1177     case AF_CXXNewArray: os << "'new[]'"; return;
1178     case AF_IfNameIndex: os << "'if_nameindex()'"; return;
1179     case AF_Alloca:
1180     case AF_None: llvm_unreachable("not a deallocation expression");
1181   }
1182 }
1183 
1184 void MallocChecker::printExpectedDeallocName(raw_ostream &os,
1185                                              AllocationFamily Family) const {
1186   switch(Family) {
1187     case AF_Malloc: os << "free()"; return;
1188     case AF_CXXNew: os << "'delete'"; return;
1189     case AF_CXXNewArray: os << "'delete[]'"; return;
1190     case AF_IfNameIndex: os << "'if_freenameindex()'"; return;
1191     case AF_Alloca:
1192     case AF_None: llvm_unreachable("suspicious argument");
1193   }
1194 }
1195 
1196 ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C,
1197                                           const Expr *ArgExpr,
1198                                           const Expr *ParentExpr,
1199                                           ProgramStateRef State,
1200                                           bool Hold,
1201                                           bool &ReleasedAllocated,
1202                                           bool ReturnsNullOnFailure) const {
1203 
1204   if (!State)
1205     return nullptr;
1206 
1207   SVal ArgVal = State->getSVal(ArgExpr, C.getLocationContext());
1208   if (!ArgVal.getAs<DefinedOrUnknownSVal>())
1209     return nullptr;
1210   DefinedOrUnknownSVal location = ArgVal.castAs<DefinedOrUnknownSVal>();
1211 
1212   // Check for null dereferences.
1213   if (!location.getAs<Loc>())
1214     return nullptr;
1215 
1216   // The explicit NULL case, no operation is performed.
1217   ProgramStateRef notNullState, nullState;
1218   std::tie(notNullState, nullState) = State->assume(location);
1219   if (nullState && !notNullState)
1220     return nullptr;
1221 
1222   // Unknown values could easily be okay
1223   // Undefined values are handled elsewhere
1224   if (ArgVal.isUnknownOrUndef())
1225     return nullptr;
1226 
1227   const MemRegion *R = ArgVal.getAsRegion();
1228 
1229   // Nonlocs can't be freed, of course.
1230   // Non-region locations (labels and fixed addresses) also shouldn't be freed.
1231   if (!R) {
1232     ReportBadFree(C, ArgVal, ArgExpr->getSourceRange(), ParentExpr);
1233     return nullptr;
1234   }
1235 
1236   R = R->StripCasts();
1237 
1238   // Blocks might show up as heap data, but should not be free()d
1239   if (isa<BlockDataRegion>(R)) {
1240     ReportBadFree(C, ArgVal, ArgExpr->getSourceRange(), ParentExpr);
1241     return nullptr;
1242   }
1243 
1244   const MemSpaceRegion *MS = R->getMemorySpace();
1245 
1246   // Parameters, locals, statics, globals, and memory returned by
1247   // __builtin_alloca() shouldn't be freed.
1248   if (!(isa<UnknownSpaceRegion>(MS) || isa<HeapSpaceRegion>(MS))) {
1249     // FIXME: at the time this code was written, malloc() regions were
1250     // represented by conjured symbols, which are all in UnknownSpaceRegion.
1251     // This means that there isn't actually anything from HeapSpaceRegion
1252     // that should be freed, even though we allow it here.
1253     // Of course, free() can work on memory allocated outside the current
1254     // function, so UnknownSpaceRegion is always a possibility.
1255     // False negatives are better than false positives.
1256 
1257     if (isa<AllocaRegion>(R))
1258       ReportFreeAlloca(C, ArgVal, ArgExpr->getSourceRange());
1259     else
1260       ReportBadFree(C, ArgVal, ArgExpr->getSourceRange(), ParentExpr);
1261 
1262     return nullptr;
1263   }
1264 
1265   const SymbolicRegion *SrBase = dyn_cast<SymbolicRegion>(R->getBaseRegion());
1266   // Various cases could lead to non-symbol values here.
1267   // For now, ignore them.
1268   if (!SrBase)
1269     return nullptr;
1270 
1271   SymbolRef SymBase = SrBase->getSymbol();
1272   const RefState *RsBase = State->get<RegionState>(SymBase);
1273   SymbolRef PreviousRetStatusSymbol = nullptr;
1274 
1275   if (RsBase) {
1276 
1277     // Memory returned by alloca() shouldn't be freed.
1278     if (RsBase->getAllocationFamily() == AF_Alloca) {
1279       ReportFreeAlloca(C, ArgVal, ArgExpr->getSourceRange());
1280       return nullptr;
1281     }
1282 
1283     // Check for double free first.
1284     if ((RsBase->isReleased() || RsBase->isRelinquished()) &&
1285         !didPreviousFreeFail(State, SymBase, PreviousRetStatusSymbol)) {
1286       ReportDoubleFree(C, ParentExpr->getSourceRange(), RsBase->isReleased(),
1287                        SymBase, PreviousRetStatusSymbol);
1288       return nullptr;
1289 
1290     // If the pointer is allocated or escaped, but we are now trying to free it,
1291     // check that the call to free is proper.
1292     } else if (RsBase->isAllocated() || RsBase->isEscaped()) {
1293 
1294       // Check if an expected deallocation function matches the real one.
1295       bool DeallocMatchesAlloc =
1296         RsBase->getAllocationFamily() == getAllocationFamily(C, ParentExpr);
1297       if (!DeallocMatchesAlloc) {
1298         ReportMismatchedDealloc(C, ArgExpr->getSourceRange(),
1299                                 ParentExpr, RsBase, SymBase, Hold);
1300         return nullptr;
1301       }
1302 
1303       // Check if the memory location being freed is the actual location
1304       // allocated, or an offset.
1305       RegionOffset Offset = R->getAsOffset();
1306       if (Offset.isValid() &&
1307           !Offset.hasSymbolicOffset() &&
1308           Offset.getOffset() != 0) {
1309         const Expr *AllocExpr = cast<Expr>(RsBase->getStmt());
1310         ReportOffsetFree(C, ArgVal, ArgExpr->getSourceRange(), ParentExpr,
1311                          AllocExpr);
1312         return nullptr;
1313       }
1314     }
1315   }
1316 
1317   ReleasedAllocated = (RsBase != nullptr) && RsBase->isAllocated();
1318 
1319   // Clean out the info on previous call to free return info.
1320   State = State->remove<FreeReturnValue>(SymBase);
1321 
1322   // Keep track of the return value. If it is NULL, we will know that free
1323   // failed.
1324   if (ReturnsNullOnFailure) {
1325     SVal RetVal = C.getSVal(ParentExpr);
1326     SymbolRef RetStatusSymbol = RetVal.getAsSymbol();
1327     if (RetStatusSymbol) {
1328       C.getSymbolManager().addSymbolDependency(SymBase, RetStatusSymbol);
1329       State = State->set<FreeReturnValue>(SymBase, RetStatusSymbol);
1330     }
1331   }
1332 
1333   AllocationFamily Family = RsBase ? RsBase->getAllocationFamily()
1334                                    : getAllocationFamily(C, ParentExpr);
1335   // Normal free.
1336   if (Hold)
1337     return State->set<RegionState>(SymBase,
1338                                    RefState::getRelinquished(Family,
1339                                                              ParentExpr));
1340 
1341   return State->set<RegionState>(SymBase,
1342                                  RefState::getReleased(Family, ParentExpr));
1343 }
1344 
1345 Optional<MallocChecker::CheckKind>
1346 MallocChecker::getCheckIfTracked(AllocationFamily Family) const {
1347   switch (Family) {
1348   case AF_Malloc:
1349   case AF_Alloca:
1350   case AF_IfNameIndex: {
1351     if (ChecksEnabled[CK_MallocChecker])
1352       return CK_MallocChecker;
1353 
1354     return Optional<MallocChecker::CheckKind>();
1355   }
1356   case AF_CXXNew:
1357   case AF_CXXNewArray: {
1358     if (ChecksEnabled[CK_NewDeleteChecker]) {
1359       return CK_NewDeleteChecker;
1360     }
1361     return Optional<MallocChecker::CheckKind>();
1362   }
1363   case AF_None: {
1364     llvm_unreachable("no family");
1365   }
1366   }
1367   llvm_unreachable("unhandled family");
1368 }
1369 
1370 Optional<MallocChecker::CheckKind>
1371 MallocChecker::getCheckIfTracked(CheckerContext &C,
1372                                  const Stmt *AllocDeallocStmt) const {
1373   return getCheckIfTracked(getAllocationFamily(C, AllocDeallocStmt));
1374 }
1375 
1376 Optional<MallocChecker::CheckKind>
1377 MallocChecker::getCheckIfTracked(CheckerContext &C, SymbolRef Sym) const {
1378 
1379   const RefState *RS = C.getState()->get<RegionState>(Sym);
1380   assert(RS);
1381   return getCheckIfTracked(RS->getAllocationFamily());
1382 }
1383 
1384 bool MallocChecker::SummarizeValue(raw_ostream &os, SVal V) {
1385   if (Optional<nonloc::ConcreteInt> IntVal = V.getAs<nonloc::ConcreteInt>())
1386     os << "an integer (" << IntVal->getValue() << ")";
1387   else if (Optional<loc::ConcreteInt> ConstAddr = V.getAs<loc::ConcreteInt>())
1388     os << "a constant address (" << ConstAddr->getValue() << ")";
1389   else if (Optional<loc::GotoLabel> Label = V.getAs<loc::GotoLabel>())
1390     os << "the address of the label '" << Label->getLabel()->getName() << "'";
1391   else
1392     return false;
1393 
1394   return true;
1395 }
1396 
1397 bool MallocChecker::SummarizeRegion(raw_ostream &os,
1398                                     const MemRegion *MR) {
1399   switch (MR->getKind()) {
1400   case MemRegion::FunctionTextRegionKind: {
1401     const NamedDecl *FD = cast<FunctionTextRegion>(MR)->getDecl();
1402     if (FD)
1403       os << "the address of the function '" << *FD << '\'';
1404     else
1405       os << "the address of a function";
1406     return true;
1407   }
1408   case MemRegion::BlockTextRegionKind:
1409     os << "block text";
1410     return true;
1411   case MemRegion::BlockDataRegionKind:
1412     // FIXME: where the block came from?
1413     os << "a block";
1414     return true;
1415   default: {
1416     const MemSpaceRegion *MS = MR->getMemorySpace();
1417 
1418     if (isa<StackLocalsSpaceRegion>(MS)) {
1419       const VarRegion *VR = dyn_cast<VarRegion>(MR);
1420       const VarDecl *VD;
1421       if (VR)
1422         VD = VR->getDecl();
1423       else
1424         VD = nullptr;
1425 
1426       if (VD)
1427         os << "the address of the local variable '" << VD->getName() << "'";
1428       else
1429         os << "the address of a local stack variable";
1430       return true;
1431     }
1432 
1433     if (isa<StackArgumentsSpaceRegion>(MS)) {
1434       const VarRegion *VR = dyn_cast<VarRegion>(MR);
1435       const VarDecl *VD;
1436       if (VR)
1437         VD = VR->getDecl();
1438       else
1439         VD = nullptr;
1440 
1441       if (VD)
1442         os << "the address of the parameter '" << VD->getName() << "'";
1443       else
1444         os << "the address of a parameter";
1445       return true;
1446     }
1447 
1448     if (isa<GlobalsSpaceRegion>(MS)) {
1449       const VarRegion *VR = dyn_cast<VarRegion>(MR);
1450       const VarDecl *VD;
1451       if (VR)
1452         VD = VR->getDecl();
1453       else
1454         VD = nullptr;
1455 
1456       if (VD) {
1457         if (VD->isStaticLocal())
1458           os << "the address of the static variable '" << VD->getName() << "'";
1459         else
1460           os << "the address of the global variable '" << VD->getName() << "'";
1461       } else
1462         os << "the address of a global variable";
1463       return true;
1464     }
1465 
1466     return false;
1467   }
1468   }
1469 }
1470 
1471 void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal,
1472                                   SourceRange Range,
1473                                   const Expr *DeallocExpr) const {
1474 
1475   if (!ChecksEnabled[CK_MallocChecker] &&
1476       !ChecksEnabled[CK_NewDeleteChecker])
1477     return;
1478 
1479   Optional<MallocChecker::CheckKind> CheckKind =
1480       getCheckIfTracked(C, DeallocExpr);
1481   if (!CheckKind.hasValue())
1482     return;
1483 
1484   if (ExplodedNode *N = C.generateSink()) {
1485     if (!BT_BadFree[*CheckKind])
1486       BT_BadFree[*CheckKind].reset(
1487           new BugType(CheckNames[*CheckKind], "Bad free", "Memory Error"));
1488 
1489     SmallString<100> buf;
1490     llvm::raw_svector_ostream os(buf);
1491 
1492     const MemRegion *MR = ArgVal.getAsRegion();
1493     while (const ElementRegion *ER = dyn_cast_or_null<ElementRegion>(MR))
1494       MR = ER->getSuperRegion();
1495 
1496     os << "Argument to ";
1497     if (!printAllocDeallocName(os, C, DeallocExpr))
1498       os << "deallocator";
1499 
1500     os << " is ";
1501     bool Summarized = MR ? SummarizeRegion(os, MR)
1502                          : SummarizeValue(os, ArgVal);
1503     if (Summarized)
1504       os << ", which is not memory allocated by ";
1505     else
1506       os << "not memory allocated by ";
1507 
1508     printExpectedAllocName(os, C, DeallocExpr);
1509 
1510     BugReport *R = new BugReport(*BT_BadFree[*CheckKind], os.str(), N);
1511     R->markInteresting(MR);
1512     R->addRange(Range);
1513     C.emitReport(R);
1514   }
1515 }
1516 
1517 void MallocChecker::ReportFreeAlloca(CheckerContext &C, SVal ArgVal,
1518                                      SourceRange Range) const {
1519 
1520   Optional<MallocChecker::CheckKind> CheckKind;
1521 
1522   if (ChecksEnabled[CK_MallocChecker])
1523     CheckKind = CK_MallocChecker;
1524   else if (ChecksEnabled[CK_MismatchedDeallocatorChecker])
1525     CheckKind = CK_MismatchedDeallocatorChecker;
1526   else
1527     return;
1528 
1529   if (ExplodedNode *N = C.generateSink()) {
1530     if (!BT_FreeAlloca[*CheckKind])
1531       BT_FreeAlloca[*CheckKind].reset(
1532           new BugType(CheckNames[*CheckKind], "Free alloca()", "Memory Error"));
1533 
1534     BugReport *R = new BugReport(*BT_FreeAlloca[*CheckKind],
1535                  "Memory allocated by alloca() should not be deallocated", N);
1536     R->markInteresting(ArgVal.getAsRegion());
1537     R->addRange(Range);
1538     C.emitReport(R);
1539   }
1540 }
1541 
1542 void MallocChecker::ReportMismatchedDealloc(CheckerContext &C,
1543                                             SourceRange Range,
1544                                             const Expr *DeallocExpr,
1545                                             const RefState *RS,
1546                                             SymbolRef Sym,
1547                                             bool OwnershipTransferred) const {
1548 
1549   if (!ChecksEnabled[CK_MismatchedDeallocatorChecker])
1550     return;
1551 
1552   if (ExplodedNode *N = C.generateSink()) {
1553     if (!BT_MismatchedDealloc)
1554       BT_MismatchedDealloc.reset(
1555           new BugType(CheckNames[CK_MismatchedDeallocatorChecker],
1556                       "Bad deallocator", "Memory Error"));
1557 
1558     SmallString<100> buf;
1559     llvm::raw_svector_ostream os(buf);
1560 
1561     const Expr *AllocExpr = cast<Expr>(RS->getStmt());
1562     SmallString<20> AllocBuf;
1563     llvm::raw_svector_ostream AllocOs(AllocBuf);
1564     SmallString<20> DeallocBuf;
1565     llvm::raw_svector_ostream DeallocOs(DeallocBuf);
1566 
1567     if (OwnershipTransferred) {
1568       if (printAllocDeallocName(DeallocOs, C, DeallocExpr))
1569         os << DeallocOs.str() << " cannot";
1570       else
1571         os << "Cannot";
1572 
1573       os << " take ownership of memory";
1574 
1575       if (printAllocDeallocName(AllocOs, C, AllocExpr))
1576         os << " allocated by " << AllocOs.str();
1577     } else {
1578       os << "Memory";
1579       if (printAllocDeallocName(AllocOs, C, AllocExpr))
1580         os << " allocated by " << AllocOs.str();
1581 
1582       os << " should be deallocated by ";
1583         printExpectedDeallocName(os, RS->getAllocationFamily());
1584 
1585       if (printAllocDeallocName(DeallocOs, C, DeallocExpr))
1586         os << ", not " << DeallocOs.str();
1587     }
1588 
1589     BugReport *R = new BugReport(*BT_MismatchedDealloc, os.str(), N);
1590     R->markInteresting(Sym);
1591     R->addRange(Range);
1592     R->addVisitor(llvm::make_unique<MallocBugVisitor>(Sym));
1593     C.emitReport(R);
1594   }
1595 }
1596 
1597 void MallocChecker::ReportOffsetFree(CheckerContext &C, SVal ArgVal,
1598                                      SourceRange Range, const Expr *DeallocExpr,
1599                                      const Expr *AllocExpr) const {
1600 
1601 
1602   if (!ChecksEnabled[CK_MallocChecker] &&
1603       !ChecksEnabled[CK_NewDeleteChecker])
1604     return;
1605 
1606   Optional<MallocChecker::CheckKind> CheckKind =
1607       getCheckIfTracked(C, AllocExpr);
1608   if (!CheckKind.hasValue())
1609     return;
1610 
1611   ExplodedNode *N = C.generateSink();
1612   if (!N)
1613     return;
1614 
1615   if (!BT_OffsetFree[*CheckKind])
1616     BT_OffsetFree[*CheckKind].reset(
1617         new BugType(CheckNames[*CheckKind], "Offset free", "Memory Error"));
1618 
1619   SmallString<100> buf;
1620   llvm::raw_svector_ostream os(buf);
1621   SmallString<20> AllocNameBuf;
1622   llvm::raw_svector_ostream AllocNameOs(AllocNameBuf);
1623 
1624   const MemRegion *MR = ArgVal.getAsRegion();
1625   assert(MR && "Only MemRegion based symbols can have offset free errors");
1626 
1627   RegionOffset Offset = MR->getAsOffset();
1628   assert((Offset.isValid() &&
1629           !Offset.hasSymbolicOffset() &&
1630           Offset.getOffset() != 0) &&
1631          "Only symbols with a valid offset can have offset free errors");
1632 
1633   int offsetBytes = Offset.getOffset() / C.getASTContext().getCharWidth();
1634 
1635   os << "Argument to ";
1636   if (!printAllocDeallocName(os, C, DeallocExpr))
1637     os << "deallocator";
1638   os << " is offset by "
1639      << offsetBytes
1640      << " "
1641      << ((abs(offsetBytes) > 1) ? "bytes" : "byte")
1642      << " from the start of ";
1643   if (AllocExpr && printAllocDeallocName(AllocNameOs, C, AllocExpr))
1644     os << "memory allocated by " << AllocNameOs.str();
1645   else
1646     os << "allocated memory";
1647 
1648   BugReport *R = new BugReport(*BT_OffsetFree[*CheckKind], os.str(), N);
1649   R->markInteresting(MR->getBaseRegion());
1650   R->addRange(Range);
1651   C.emitReport(R);
1652 }
1653 
1654 void MallocChecker::ReportUseAfterFree(CheckerContext &C, SourceRange Range,
1655                                        SymbolRef Sym) const {
1656 
1657   if (!ChecksEnabled[CK_MallocChecker] &&
1658       !ChecksEnabled[CK_NewDeleteChecker])
1659     return;
1660 
1661   Optional<MallocChecker::CheckKind> CheckKind = getCheckIfTracked(C, Sym);
1662   if (!CheckKind.hasValue())
1663     return;
1664 
1665   if (ExplodedNode *N = C.generateSink()) {
1666     if (!BT_UseFree[*CheckKind])
1667       BT_UseFree[*CheckKind].reset(new BugType(
1668           CheckNames[*CheckKind], "Use-after-free", "Memory Error"));
1669 
1670     BugReport *R = new BugReport(*BT_UseFree[*CheckKind],
1671                                  "Use of memory after it is freed", N);
1672 
1673     R->markInteresting(Sym);
1674     R->addRange(Range);
1675     R->addVisitor(llvm::make_unique<MallocBugVisitor>(Sym));
1676     C.emitReport(R);
1677   }
1678 }
1679 
1680 void MallocChecker::ReportDoubleFree(CheckerContext &C, SourceRange Range,
1681                                      bool Released, SymbolRef Sym,
1682                                      SymbolRef PrevSym) const {
1683 
1684   if (!ChecksEnabled[CK_MallocChecker] &&
1685       !ChecksEnabled[CK_NewDeleteChecker])
1686     return;
1687 
1688   Optional<MallocChecker::CheckKind> CheckKind = getCheckIfTracked(C, Sym);
1689   if (!CheckKind.hasValue())
1690     return;
1691 
1692   if (ExplodedNode *N = C.generateSink()) {
1693     if (!BT_DoubleFree[*CheckKind])
1694       BT_DoubleFree[*CheckKind].reset(
1695           new BugType(CheckNames[*CheckKind], "Double free", "Memory Error"));
1696 
1697     BugReport *R =
1698         new BugReport(*BT_DoubleFree[*CheckKind],
1699                       (Released ? "Attempt to free released memory"
1700                                 : "Attempt to free non-owned memory"),
1701                       N);
1702     R->addRange(Range);
1703     R->markInteresting(Sym);
1704     if (PrevSym)
1705       R->markInteresting(PrevSym);
1706     R->addVisitor(llvm::make_unique<MallocBugVisitor>(Sym));
1707     C.emitReport(R);
1708   }
1709 }
1710 
1711 void MallocChecker::ReportDoubleDelete(CheckerContext &C, SymbolRef Sym) const {
1712 
1713   if (!ChecksEnabled[CK_NewDeleteChecker])
1714     return;
1715 
1716   Optional<MallocChecker::CheckKind> CheckKind = getCheckIfTracked(C, Sym);
1717   if (!CheckKind.hasValue())
1718     return;
1719 
1720   if (ExplodedNode *N = C.generateSink()) {
1721     if (!BT_DoubleDelete)
1722       BT_DoubleDelete.reset(new BugType(CheckNames[CK_NewDeleteChecker],
1723                                         "Double delete", "Memory Error"));
1724 
1725     BugReport *R = new BugReport(*BT_DoubleDelete,
1726                                  "Attempt to delete released memory", N);
1727 
1728     R->markInteresting(Sym);
1729     R->addVisitor(llvm::make_unique<MallocBugVisitor>(Sym));
1730     C.emitReport(R);
1731   }
1732 }
1733 
1734 ProgramStateRef MallocChecker::ReallocMem(CheckerContext &C,
1735                                           const CallExpr *CE,
1736                                           bool FreesOnFail,
1737                                           ProgramStateRef State) const {
1738   if (!State)
1739     return nullptr;
1740 
1741   if (CE->getNumArgs() < 2)
1742     return nullptr;
1743 
1744   const Expr *arg0Expr = CE->getArg(0);
1745   const LocationContext *LCtx = C.getLocationContext();
1746   SVal Arg0Val = State->getSVal(arg0Expr, LCtx);
1747   if (!Arg0Val.getAs<DefinedOrUnknownSVal>())
1748     return nullptr;
1749   DefinedOrUnknownSVal arg0Val = Arg0Val.castAs<DefinedOrUnknownSVal>();
1750 
1751   SValBuilder &svalBuilder = C.getSValBuilder();
1752 
1753   DefinedOrUnknownSVal PtrEQ =
1754     svalBuilder.evalEQ(State, arg0Val, svalBuilder.makeNull());
1755 
1756   // Get the size argument. If there is no size arg then give up.
1757   const Expr *Arg1 = CE->getArg(1);
1758   if (!Arg1)
1759     return nullptr;
1760 
1761   // Get the value of the size argument.
1762   SVal Arg1ValG = State->getSVal(Arg1, LCtx);
1763   if (!Arg1ValG.getAs<DefinedOrUnknownSVal>())
1764     return nullptr;
1765   DefinedOrUnknownSVal Arg1Val = Arg1ValG.castAs<DefinedOrUnknownSVal>();
1766 
1767   // Compare the size argument to 0.
1768   DefinedOrUnknownSVal SizeZero =
1769     svalBuilder.evalEQ(State, Arg1Val,
1770                        svalBuilder.makeIntValWithPtrWidth(0, false));
1771 
1772   ProgramStateRef StatePtrIsNull, StatePtrNotNull;
1773   std::tie(StatePtrIsNull, StatePtrNotNull) = State->assume(PtrEQ);
1774   ProgramStateRef StateSizeIsZero, StateSizeNotZero;
1775   std::tie(StateSizeIsZero, StateSizeNotZero) = State->assume(SizeZero);
1776   // We only assume exceptional states if they are definitely true; if the
1777   // state is under-constrained, assume regular realloc behavior.
1778   bool PrtIsNull = StatePtrIsNull && !StatePtrNotNull;
1779   bool SizeIsZero = StateSizeIsZero && !StateSizeNotZero;
1780 
1781   // If the ptr is NULL and the size is not 0, the call is equivalent to
1782   // malloc(size).
1783   if ( PrtIsNull && !SizeIsZero) {
1784     ProgramStateRef stateMalloc = MallocMemAux(C, CE, CE->getArg(1),
1785                                                UndefinedVal(), StatePtrIsNull);
1786     return stateMalloc;
1787   }
1788 
1789   if (PrtIsNull && SizeIsZero)
1790     return nullptr;
1791 
1792   // Get the from and to pointer symbols as in toPtr = realloc(fromPtr, size).
1793   assert(!PrtIsNull);
1794   SymbolRef FromPtr = arg0Val.getAsSymbol();
1795   SVal RetVal = State->getSVal(CE, LCtx);
1796   SymbolRef ToPtr = RetVal.getAsSymbol();
1797   if (!FromPtr || !ToPtr)
1798     return nullptr;
1799 
1800   bool ReleasedAllocated = false;
1801 
1802   // If the size is 0, free the memory.
1803   if (SizeIsZero)
1804     if (ProgramStateRef stateFree = FreeMemAux(C, CE, StateSizeIsZero, 0,
1805                                                false, ReleasedAllocated)){
1806       // The semantics of the return value are:
1807       // If size was equal to 0, either NULL or a pointer suitable to be passed
1808       // to free() is returned. We just free the input pointer and do not add
1809       // any constrains on the output pointer.
1810       return stateFree;
1811     }
1812 
1813   // Default behavior.
1814   if (ProgramStateRef stateFree =
1815         FreeMemAux(C, CE, State, 0, false, ReleasedAllocated)) {
1816 
1817     ProgramStateRef stateRealloc = MallocMemAux(C, CE, CE->getArg(1),
1818                                                 UnknownVal(), stateFree);
1819     if (!stateRealloc)
1820       return nullptr;
1821 
1822     ReallocPairKind Kind = RPToBeFreedAfterFailure;
1823     if (FreesOnFail)
1824       Kind = RPIsFreeOnFailure;
1825     else if (!ReleasedAllocated)
1826       Kind = RPDoNotTrackAfterFailure;
1827 
1828     // Record the info about the reallocated symbol so that we could properly
1829     // process failed reallocation.
1830     stateRealloc = stateRealloc->set<ReallocPairs>(ToPtr,
1831                                                    ReallocPair(FromPtr, Kind));
1832     // The reallocated symbol should stay alive for as long as the new symbol.
1833     C.getSymbolManager().addSymbolDependency(ToPtr, FromPtr);
1834     return stateRealloc;
1835   }
1836   return nullptr;
1837 }
1838 
1839 ProgramStateRef MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE,
1840                                          ProgramStateRef State) {
1841   if (!State)
1842     return nullptr;
1843 
1844   if (CE->getNumArgs() < 2)
1845     return nullptr;
1846 
1847   SValBuilder &svalBuilder = C.getSValBuilder();
1848   const LocationContext *LCtx = C.getLocationContext();
1849   SVal count = State->getSVal(CE->getArg(0), LCtx);
1850   SVal elementSize = State->getSVal(CE->getArg(1), LCtx);
1851   SVal TotalSize = svalBuilder.evalBinOp(State, BO_Mul, count, elementSize,
1852                                         svalBuilder.getContext().getSizeType());
1853   SVal zeroVal = svalBuilder.makeZeroVal(svalBuilder.getContext().CharTy);
1854 
1855   return MallocMemAux(C, CE, TotalSize, zeroVal, State);
1856 }
1857 
1858 LeakInfo
1859 MallocChecker::getAllocationSite(const ExplodedNode *N, SymbolRef Sym,
1860                                  CheckerContext &C) const {
1861   const LocationContext *LeakContext = N->getLocationContext();
1862   // Walk the ExplodedGraph backwards and find the first node that referred to
1863   // the tracked symbol.
1864   const ExplodedNode *AllocNode = N;
1865   const MemRegion *ReferenceRegion = nullptr;
1866 
1867   while (N) {
1868     ProgramStateRef State = N->getState();
1869     if (!State->get<RegionState>(Sym))
1870       break;
1871 
1872     // Find the most recent expression bound to the symbol in the current
1873     // context.
1874       if (!ReferenceRegion) {
1875         if (const MemRegion *MR = C.getLocationRegionIfPostStore(N)) {
1876           SVal Val = State->getSVal(MR);
1877           if (Val.getAsLocSymbol() == Sym) {
1878             const VarRegion* VR = MR->getBaseRegion()->getAs<VarRegion>();
1879             // Do not show local variables belonging to a function other than
1880             // where the error is reported.
1881             if (!VR ||
1882                 (VR->getStackFrame() == LeakContext->getCurrentStackFrame()))
1883               ReferenceRegion = MR;
1884           }
1885         }
1886       }
1887 
1888     // Allocation node, is the last node in the current or parent context in
1889     // which the symbol was tracked.
1890     const LocationContext *NContext = N->getLocationContext();
1891     if (NContext == LeakContext ||
1892         NContext->isParentOf(LeakContext))
1893       AllocNode = N;
1894     N = N->pred_empty() ? nullptr : *(N->pred_begin());
1895   }
1896 
1897   return LeakInfo(AllocNode, ReferenceRegion);
1898 }
1899 
1900 void MallocChecker::reportLeak(SymbolRef Sym, ExplodedNode *N,
1901                                CheckerContext &C) const {
1902 
1903   if (!ChecksEnabled[CK_MallocChecker] &&
1904       !ChecksEnabled[CK_NewDeleteLeaksChecker])
1905     return;
1906 
1907   const RefState *RS = C.getState()->get<RegionState>(Sym);
1908   assert(RS && "cannot leak an untracked symbol");
1909   AllocationFamily Family = RS->getAllocationFamily();
1910   Optional<MallocChecker::CheckKind> CheckKind = getCheckIfTracked(Family);
1911   if (!CheckKind.hasValue())
1912     return;
1913 
1914   // Special case for new and new[]; these are controlled by a separate checker
1915   // flag so that they can be selectively disabled.
1916   if (Family == AF_CXXNew || Family == AF_CXXNewArray)
1917     if (!ChecksEnabled[CK_NewDeleteLeaksChecker])
1918       return;
1919 
1920   if (RS->getAllocationFamily() == AF_Alloca)
1921     return;
1922 
1923   assert(N);
1924   if (!BT_Leak[*CheckKind]) {
1925     BT_Leak[*CheckKind].reset(
1926         new BugType(CheckNames[*CheckKind], "Memory leak", "Memory Error"));
1927     // Leaks should not be reported if they are post-dominated by a sink:
1928     // (1) Sinks are higher importance bugs.
1929     // (2) NoReturnFunctionChecker uses sink nodes to represent paths ending
1930     //     with __noreturn functions such as assert() or exit(). We choose not
1931     //     to report leaks on such paths.
1932     BT_Leak[*CheckKind]->setSuppressOnSink(true);
1933   }
1934 
1935   // Most bug reports are cached at the location where they occurred.
1936   // With leaks, we want to unique them by the location where they were
1937   // allocated, and only report a single path.
1938   PathDiagnosticLocation LocUsedForUniqueing;
1939   const ExplodedNode *AllocNode = nullptr;
1940   const MemRegion *Region = nullptr;
1941   std::tie(AllocNode, Region) = getAllocationSite(N, Sym, C);
1942 
1943   ProgramPoint P = AllocNode->getLocation();
1944   const Stmt *AllocationStmt = nullptr;
1945   if (Optional<CallExitEnd> Exit = P.getAs<CallExitEnd>())
1946     AllocationStmt = Exit->getCalleeContext()->getCallSite();
1947   else if (Optional<StmtPoint> SP = P.getAs<StmtPoint>())
1948     AllocationStmt = SP->getStmt();
1949   if (AllocationStmt)
1950     LocUsedForUniqueing = PathDiagnosticLocation::createBegin(AllocationStmt,
1951                                               C.getSourceManager(),
1952                                               AllocNode->getLocationContext());
1953 
1954   SmallString<200> buf;
1955   llvm::raw_svector_ostream os(buf);
1956   if (Region && Region->canPrintPretty()) {
1957     os << "Potential leak of memory pointed to by ";
1958     Region->printPretty(os);
1959   } else {
1960     os << "Potential memory leak";
1961   }
1962 
1963   BugReport *R =
1964       new BugReport(*BT_Leak[*CheckKind], os.str(), N, LocUsedForUniqueing,
1965                     AllocNode->getLocationContext()->getDecl());
1966   R->markInteresting(Sym);
1967   R->addVisitor(llvm::make_unique<MallocBugVisitor>(Sym, true));
1968   C.emitReport(R);
1969 }
1970 
1971 void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper,
1972                                      CheckerContext &C) const
1973 {
1974   if (!SymReaper.hasDeadSymbols())
1975     return;
1976 
1977   ProgramStateRef state = C.getState();
1978   RegionStateTy RS = state->get<RegionState>();
1979   RegionStateTy::Factory &F = state->get_context<RegionState>();
1980 
1981   SmallVector<SymbolRef, 2> Errors;
1982   for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
1983     if (SymReaper.isDead(I->first)) {
1984       if (I->second.isAllocated())
1985         Errors.push_back(I->first);
1986       // Remove the dead symbol from the map.
1987       RS = F.remove(RS, I->first);
1988 
1989     }
1990   }
1991 
1992   // Cleanup the Realloc Pairs Map.
1993   ReallocPairsTy RP = state->get<ReallocPairs>();
1994   for (ReallocPairsTy::iterator I = RP.begin(), E = RP.end(); I != E; ++I) {
1995     if (SymReaper.isDead(I->first) ||
1996         SymReaper.isDead(I->second.ReallocatedSym)) {
1997       state = state->remove<ReallocPairs>(I->first);
1998     }
1999   }
2000 
2001   // Cleanup the FreeReturnValue Map.
2002   FreeReturnValueTy FR = state->get<FreeReturnValue>();
2003   for (FreeReturnValueTy::iterator I = FR.begin(), E = FR.end(); I != E; ++I) {
2004     if (SymReaper.isDead(I->first) ||
2005         SymReaper.isDead(I->second)) {
2006       state = state->remove<FreeReturnValue>(I->first);
2007     }
2008   }
2009 
2010   // Generate leak node.
2011   ExplodedNode *N = C.getPredecessor();
2012   if (!Errors.empty()) {
2013     static CheckerProgramPointTag Tag("MallocChecker", "DeadSymbolsLeak");
2014     N = C.addTransition(C.getState(), C.getPredecessor(), &Tag);
2015     for (SmallVectorImpl<SymbolRef>::iterator
2016            I = Errors.begin(), E = Errors.end(); I != E; ++I) {
2017       reportLeak(*I, N, C);
2018     }
2019   }
2020 
2021   C.addTransition(state->set<RegionState>(RS), N);
2022 }
2023 
2024 void MallocChecker::checkPreCall(const CallEvent &Call,
2025                                  CheckerContext &C) const {
2026 
2027   if (const CXXDestructorCall *DC = dyn_cast<CXXDestructorCall>(&Call)) {
2028     SymbolRef Sym = DC->getCXXThisVal().getAsSymbol();
2029     if (!Sym || checkDoubleDelete(Sym, C))
2030       return;
2031   }
2032 
2033   // We will check for double free in the post visit.
2034   if (const AnyFunctionCall *FC = dyn_cast<AnyFunctionCall>(&Call)) {
2035     const FunctionDecl *FD = FC->getDecl();
2036     if (!FD)
2037       return;
2038 
2039     ASTContext &Ctx = C.getASTContext();
2040     if (ChecksEnabled[CK_MallocChecker] &&
2041         (isCMemFunction(FD, Ctx, AF_Malloc, MemoryOperationKind::MOK_Free) ||
2042          isCMemFunction(FD, Ctx, AF_IfNameIndex,
2043                         MemoryOperationKind::MOK_Free)))
2044       return;
2045 
2046     if (ChecksEnabled[CK_NewDeleteChecker] &&
2047         isStandardNewDelete(FD, Ctx))
2048       return;
2049   }
2050 
2051   // Check if the callee of a method is deleted.
2052   if (const CXXInstanceCall *CC = dyn_cast<CXXInstanceCall>(&Call)) {
2053     SymbolRef Sym = CC->getCXXThisVal().getAsSymbol();
2054     if (!Sym || checkUseAfterFree(Sym, C, CC->getCXXThisExpr()))
2055       return;
2056   }
2057 
2058   // Check arguments for being used after free.
2059   for (unsigned I = 0, E = Call.getNumArgs(); I != E; ++I) {
2060     SVal ArgSVal = Call.getArgSVal(I);
2061     if (ArgSVal.getAs<Loc>()) {
2062       SymbolRef Sym = ArgSVal.getAsSymbol();
2063       if (!Sym)
2064         continue;
2065       if (checkUseAfterFree(Sym, C, Call.getArgExpr(I)))
2066         return;
2067     }
2068   }
2069 }
2070 
2071 void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
2072   const Expr *E = S->getRetValue();
2073   if (!E)
2074     return;
2075 
2076   // Check if we are returning a symbol.
2077   ProgramStateRef State = C.getState();
2078   SVal RetVal = State->getSVal(E, C.getLocationContext());
2079   SymbolRef Sym = RetVal.getAsSymbol();
2080   if (!Sym)
2081     // If we are returning a field of the allocated struct or an array element,
2082     // the callee could still free the memory.
2083     // TODO: This logic should be a part of generic symbol escape callback.
2084     if (const MemRegion *MR = RetVal.getAsRegion())
2085       if (isa<FieldRegion>(MR) || isa<ElementRegion>(MR))
2086         if (const SymbolicRegion *BMR =
2087               dyn_cast<SymbolicRegion>(MR->getBaseRegion()))
2088           Sym = BMR->getSymbol();
2089 
2090   // Check if we are returning freed memory.
2091   if (Sym)
2092     checkUseAfterFree(Sym, C, E);
2093 }
2094 
2095 // TODO: Blocks should be either inlined or should call invalidate regions
2096 // upon invocation. After that's in place, special casing here will not be
2097 // needed.
2098 void MallocChecker::checkPostStmt(const BlockExpr *BE,
2099                                   CheckerContext &C) const {
2100 
2101   // Scan the BlockDecRefExprs for any object the retain count checker
2102   // may be tracking.
2103   if (!BE->getBlockDecl()->hasCaptures())
2104     return;
2105 
2106   ProgramStateRef state = C.getState();
2107   const BlockDataRegion *R =
2108     cast<BlockDataRegion>(state->getSVal(BE,
2109                                          C.getLocationContext()).getAsRegion());
2110 
2111   BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
2112                                             E = R->referenced_vars_end();
2113 
2114   if (I == E)
2115     return;
2116 
2117   SmallVector<const MemRegion*, 10> Regions;
2118   const LocationContext *LC = C.getLocationContext();
2119   MemRegionManager &MemMgr = C.getSValBuilder().getRegionManager();
2120 
2121   for ( ; I != E; ++I) {
2122     const VarRegion *VR = I.getCapturedRegion();
2123     if (VR->getSuperRegion() == R) {
2124       VR = MemMgr.getVarRegion(VR->getDecl(), LC);
2125     }
2126     Regions.push_back(VR);
2127   }
2128 
2129   state =
2130     state->scanReachableSymbols<StopTrackingCallback>(Regions.data(),
2131                                     Regions.data() + Regions.size()).getState();
2132   C.addTransition(state);
2133 }
2134 
2135 bool MallocChecker::isReleased(SymbolRef Sym, CheckerContext &C) const {
2136   assert(Sym);
2137   const RefState *RS = C.getState()->get<RegionState>(Sym);
2138   return (RS && RS->isReleased());
2139 }
2140 
2141 bool MallocChecker::checkUseAfterFree(SymbolRef Sym, CheckerContext &C,
2142                                       const Stmt *S) const {
2143 
2144   if (isReleased(Sym, C)) {
2145     ReportUseAfterFree(C, S->getSourceRange(), Sym);
2146     return true;
2147   }
2148 
2149   return false;
2150 }
2151 
2152 bool MallocChecker::checkDoubleDelete(SymbolRef Sym, CheckerContext &C) const {
2153 
2154   if (isReleased(Sym, C)) {
2155     ReportDoubleDelete(C, Sym);
2156     return true;
2157   }
2158   return false;
2159 }
2160 
2161 // Check if the location is a freed symbolic region.
2162 void MallocChecker::checkLocation(SVal l, bool isLoad, const Stmt *S,
2163                                   CheckerContext &C) const {
2164   SymbolRef Sym = l.getLocSymbolInBase();
2165   if (Sym)
2166     checkUseAfterFree(Sym, C, S);
2167 }
2168 
2169 // If a symbolic region is assumed to NULL (or another constant), stop tracking
2170 // it - assuming that allocation failed on this path.
2171 ProgramStateRef MallocChecker::evalAssume(ProgramStateRef state,
2172                                               SVal Cond,
2173                                               bool Assumption) const {
2174   RegionStateTy RS = state->get<RegionState>();
2175   for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
2176     // If the symbol is assumed to be NULL, remove it from consideration.
2177     ConstraintManager &CMgr = state->getConstraintManager();
2178     ConditionTruthVal AllocFailed = CMgr.isNull(state, I.getKey());
2179     if (AllocFailed.isConstrainedTrue())
2180       state = state->remove<RegionState>(I.getKey());
2181   }
2182 
2183   // Realloc returns 0 when reallocation fails, which means that we should
2184   // restore the state of the pointer being reallocated.
2185   ReallocPairsTy RP = state->get<ReallocPairs>();
2186   for (ReallocPairsTy::iterator I = RP.begin(), E = RP.end(); I != E; ++I) {
2187     // If the symbol is assumed to be NULL, remove it from consideration.
2188     ConstraintManager &CMgr = state->getConstraintManager();
2189     ConditionTruthVal AllocFailed = CMgr.isNull(state, I.getKey());
2190     if (!AllocFailed.isConstrainedTrue())
2191       continue;
2192 
2193     SymbolRef ReallocSym = I.getData().ReallocatedSym;
2194     if (const RefState *RS = state->get<RegionState>(ReallocSym)) {
2195       if (RS->isReleased()) {
2196         if (I.getData().Kind == RPToBeFreedAfterFailure)
2197           state = state->set<RegionState>(ReallocSym,
2198               RefState::getAllocated(RS->getAllocationFamily(), RS->getStmt()));
2199         else if (I.getData().Kind == RPDoNotTrackAfterFailure)
2200           state = state->remove<RegionState>(ReallocSym);
2201         else
2202           assert(I.getData().Kind == RPIsFreeOnFailure);
2203       }
2204     }
2205     state = state->remove<ReallocPairs>(I.getKey());
2206   }
2207 
2208   return state;
2209 }
2210 
2211 bool MallocChecker::mayFreeAnyEscapedMemoryOrIsModeledExplicitly(
2212                                               const CallEvent *Call,
2213                                               ProgramStateRef State,
2214                                               SymbolRef &EscapingSymbol) const {
2215   assert(Call);
2216   EscapingSymbol = nullptr;
2217 
2218   // For now, assume that any C++ or block call can free memory.
2219   // TODO: If we want to be more optimistic here, we'll need to make sure that
2220   // regions escape to C++ containers. They seem to do that even now, but for
2221   // mysterious reasons.
2222   if (!(isa<SimpleFunctionCall>(Call) || isa<ObjCMethodCall>(Call)))
2223     return true;
2224 
2225   // Check Objective-C messages by selector name.
2226   if (const ObjCMethodCall *Msg = dyn_cast<ObjCMethodCall>(Call)) {
2227     // If it's not a framework call, or if it takes a callback, assume it
2228     // can free memory.
2229     if (!Call->isInSystemHeader() || Call->hasNonZeroCallbackArg())
2230       return true;
2231 
2232     // If it's a method we know about, handle it explicitly post-call.
2233     // This should happen before the "freeWhenDone" check below.
2234     if (isKnownDeallocObjCMethodName(*Msg))
2235       return false;
2236 
2237     // If there's a "freeWhenDone" parameter, but the method isn't one we know
2238     // about, we can't be sure that the object will use free() to deallocate the
2239     // memory, so we can't model it explicitly. The best we can do is use it to
2240     // decide whether the pointer escapes.
2241     if (Optional<bool> FreeWhenDone = getFreeWhenDoneArg(*Msg))
2242       return *FreeWhenDone;
2243 
2244     // If the first selector piece ends with "NoCopy", and there is no
2245     // "freeWhenDone" parameter set to zero, we know ownership is being
2246     // transferred. Again, though, we can't be sure that the object will use
2247     // free() to deallocate the memory, so we can't model it explicitly.
2248     StringRef FirstSlot = Msg->getSelector().getNameForSlot(0);
2249     if (FirstSlot.endswith("NoCopy"))
2250       return true;
2251 
2252     // If the first selector starts with addPointer, insertPointer,
2253     // or replacePointer, assume we are dealing with NSPointerArray or similar.
2254     // This is similar to C++ containers (vector); we still might want to check
2255     // that the pointers get freed by following the container itself.
2256     if (FirstSlot.startswith("addPointer") ||
2257         FirstSlot.startswith("insertPointer") ||
2258         FirstSlot.startswith("replacePointer") ||
2259         FirstSlot.equals("valueWithPointer")) {
2260       return true;
2261     }
2262 
2263     // We should escape receiver on call to 'init'. This is especially relevant
2264     // to the receiver, as the corresponding symbol is usually not referenced
2265     // after the call.
2266     if (Msg->getMethodFamily() == OMF_init) {
2267       EscapingSymbol = Msg->getReceiverSVal().getAsSymbol();
2268       return true;
2269     }
2270 
2271     // Otherwise, assume that the method does not free memory.
2272     // Most framework methods do not free memory.
2273     return false;
2274   }
2275 
2276   // At this point the only thing left to handle is straight function calls.
2277   const FunctionDecl *FD = cast<SimpleFunctionCall>(Call)->getDecl();
2278   if (!FD)
2279     return true;
2280 
2281   ASTContext &ASTC = State->getStateManager().getContext();
2282 
2283   // If it's one of the allocation functions we can reason about, we model
2284   // its behavior explicitly.
2285   if (isMemFunction(FD, ASTC))
2286     return false;
2287 
2288   // If it's not a system call, assume it frees memory.
2289   if (!Call->isInSystemHeader())
2290     return true;
2291 
2292   // White list the system functions whose arguments escape.
2293   const IdentifierInfo *II = FD->getIdentifier();
2294   if (!II)
2295     return true;
2296   StringRef FName = II->getName();
2297 
2298   // White list the 'XXXNoCopy' CoreFoundation functions.
2299   // We specifically check these before
2300   if (FName.endswith("NoCopy")) {
2301     // Look for the deallocator argument. We know that the memory ownership
2302     // is not transferred only if the deallocator argument is
2303     // 'kCFAllocatorNull'.
2304     for (unsigned i = 1; i < Call->getNumArgs(); ++i) {
2305       const Expr *ArgE = Call->getArgExpr(i)->IgnoreParenCasts();
2306       if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(ArgE)) {
2307         StringRef DeallocatorName = DE->getFoundDecl()->getName();
2308         if (DeallocatorName == "kCFAllocatorNull")
2309           return false;
2310       }
2311     }
2312     return true;
2313   }
2314 
2315   // Associating streams with malloced buffers. The pointer can escape if
2316   // 'closefn' is specified (and if that function does free memory),
2317   // but it will not if closefn is not specified.
2318   // Currently, we do not inspect the 'closefn' function (PR12101).
2319   if (FName == "funopen")
2320     if (Call->getNumArgs() >= 4 && Call->getArgSVal(4).isConstant(0))
2321       return false;
2322 
2323   // Do not warn on pointers passed to 'setbuf' when used with std streams,
2324   // these leaks might be intentional when setting the buffer for stdio.
2325   // http://stackoverflow.com/questions/2671151/who-frees-setvbuf-buffer
2326   if (FName == "setbuf" || FName =="setbuffer" ||
2327       FName == "setlinebuf" || FName == "setvbuf") {
2328     if (Call->getNumArgs() >= 1) {
2329       const Expr *ArgE = Call->getArgExpr(0)->IgnoreParenCasts();
2330       if (const DeclRefExpr *ArgDRE = dyn_cast<DeclRefExpr>(ArgE))
2331         if (const VarDecl *D = dyn_cast<VarDecl>(ArgDRE->getDecl()))
2332           if (D->getCanonicalDecl()->getName().find("std") != StringRef::npos)
2333             return true;
2334     }
2335   }
2336 
2337   // A bunch of other functions which either take ownership of a pointer or
2338   // wrap the result up in a struct or object, meaning it can be freed later.
2339   // (See RetainCountChecker.) Not all the parameters here are invalidated,
2340   // but the Malloc checker cannot differentiate between them. The right way
2341   // of doing this would be to implement a pointer escapes callback.
2342   if (FName == "CGBitmapContextCreate" ||
2343       FName == "CGBitmapContextCreateWithData" ||
2344       FName == "CVPixelBufferCreateWithBytes" ||
2345       FName == "CVPixelBufferCreateWithPlanarBytes" ||
2346       FName == "OSAtomicEnqueue") {
2347     return true;
2348   }
2349 
2350   // Handle cases where we know a buffer's /address/ can escape.
2351   // Note that the above checks handle some special cases where we know that
2352   // even though the address escapes, it's still our responsibility to free the
2353   // buffer.
2354   if (Call->argumentsMayEscape())
2355     return true;
2356 
2357   // Otherwise, assume that the function does not free memory.
2358   // Most system calls do not free the memory.
2359   return false;
2360 }
2361 
2362 static bool retTrue(const RefState *RS) {
2363   return true;
2364 }
2365 
2366 static bool checkIfNewOrNewArrayFamily(const RefState *RS) {
2367   return (RS->getAllocationFamily() == AF_CXXNewArray ||
2368           RS->getAllocationFamily() == AF_CXXNew);
2369 }
2370 
2371 ProgramStateRef MallocChecker::checkPointerEscape(ProgramStateRef State,
2372                                              const InvalidatedSymbols &Escaped,
2373                                              const CallEvent *Call,
2374                                              PointerEscapeKind Kind) const {
2375   return checkPointerEscapeAux(State, Escaped, Call, Kind, &retTrue);
2376 }
2377 
2378 ProgramStateRef MallocChecker::checkConstPointerEscape(ProgramStateRef State,
2379                                               const InvalidatedSymbols &Escaped,
2380                                               const CallEvent *Call,
2381                                               PointerEscapeKind Kind) const {
2382   return checkPointerEscapeAux(State, Escaped, Call, Kind,
2383                                &checkIfNewOrNewArrayFamily);
2384 }
2385 
2386 ProgramStateRef MallocChecker::checkPointerEscapeAux(ProgramStateRef State,
2387                                               const InvalidatedSymbols &Escaped,
2388                                               const CallEvent *Call,
2389                                               PointerEscapeKind Kind,
2390                                   bool(*CheckRefState)(const RefState*)) const {
2391   // If we know that the call does not free memory, or we want to process the
2392   // call later, keep tracking the top level arguments.
2393   SymbolRef EscapingSymbol = nullptr;
2394   if (Kind == PSK_DirectEscapeOnCall &&
2395       !mayFreeAnyEscapedMemoryOrIsModeledExplicitly(Call, State,
2396                                                     EscapingSymbol) &&
2397       !EscapingSymbol) {
2398     return State;
2399   }
2400 
2401   for (InvalidatedSymbols::const_iterator I = Escaped.begin(),
2402        E = Escaped.end();
2403        I != E; ++I) {
2404     SymbolRef sym = *I;
2405 
2406     if (EscapingSymbol && EscapingSymbol != sym)
2407       continue;
2408 
2409     if (const RefState *RS = State->get<RegionState>(sym)) {
2410       if (RS->isAllocated() && CheckRefState(RS)) {
2411         State = State->remove<RegionState>(sym);
2412         State = State->set<RegionState>(sym, RefState::getEscaped(RS));
2413       }
2414     }
2415   }
2416   return State;
2417 }
2418 
2419 static SymbolRef findFailedReallocSymbol(ProgramStateRef currState,
2420                                          ProgramStateRef prevState) {
2421   ReallocPairsTy currMap = currState->get<ReallocPairs>();
2422   ReallocPairsTy prevMap = prevState->get<ReallocPairs>();
2423 
2424   for (ReallocPairsTy::iterator I = prevMap.begin(), E = prevMap.end();
2425        I != E; ++I) {
2426     SymbolRef sym = I.getKey();
2427     if (!currMap.lookup(sym))
2428       return sym;
2429   }
2430 
2431   return nullptr;
2432 }
2433 
2434 PathDiagnosticPiece *
2435 MallocChecker::MallocBugVisitor::VisitNode(const ExplodedNode *N,
2436                                            const ExplodedNode *PrevN,
2437                                            BugReporterContext &BRC,
2438                                            BugReport &BR) {
2439   ProgramStateRef state = N->getState();
2440   ProgramStateRef statePrev = PrevN->getState();
2441 
2442   const RefState *RS = state->get<RegionState>(Sym);
2443   const RefState *RSPrev = statePrev->get<RegionState>(Sym);
2444   if (!RS)
2445     return nullptr;
2446 
2447   const Stmt *S = nullptr;
2448   const char *Msg = nullptr;
2449   StackHintGeneratorForSymbol *StackHint = nullptr;
2450 
2451   // Retrieve the associated statement.
2452   ProgramPoint ProgLoc = N->getLocation();
2453   if (Optional<StmtPoint> SP = ProgLoc.getAs<StmtPoint>()) {
2454     S = SP->getStmt();
2455   } else if (Optional<CallExitEnd> Exit = ProgLoc.getAs<CallExitEnd>()) {
2456     S = Exit->getCalleeContext()->getCallSite();
2457   } else if (Optional<BlockEdge> Edge = ProgLoc.getAs<BlockEdge>()) {
2458     // If an assumption was made on a branch, it should be caught
2459     // here by looking at the state transition.
2460     S = Edge->getSrc()->getTerminator();
2461   }
2462 
2463   if (!S)
2464     return nullptr;
2465 
2466   // FIXME: We will eventually need to handle non-statement-based events
2467   // (__attribute__((cleanup))).
2468 
2469   // Find out if this is an interesting point and what is the kind.
2470   if (Mode == Normal) {
2471     if (isAllocated(RS, RSPrev, S)) {
2472       Msg = "Memory is allocated";
2473       StackHint = new StackHintGeneratorForSymbol(Sym,
2474                                                   "Returned allocated memory");
2475     } else if (isReleased(RS, RSPrev, S)) {
2476       Msg = "Memory is released";
2477       StackHint = new StackHintGeneratorForSymbol(Sym,
2478                                              "Returning; memory was released");
2479     } else if (isRelinquished(RS, RSPrev, S)) {
2480       Msg = "Memory ownership is transferred";
2481       StackHint = new StackHintGeneratorForSymbol(Sym, "");
2482     } else if (isReallocFailedCheck(RS, RSPrev, S)) {
2483       Mode = ReallocationFailed;
2484       Msg = "Reallocation failed";
2485       StackHint = new StackHintGeneratorForReallocationFailed(Sym,
2486                                                        "Reallocation failed");
2487 
2488       if (SymbolRef sym = findFailedReallocSymbol(state, statePrev)) {
2489         // Is it possible to fail two reallocs WITHOUT testing in between?
2490         assert((!FailedReallocSymbol || FailedReallocSymbol == sym) &&
2491           "We only support one failed realloc at a time.");
2492         BR.markInteresting(sym);
2493         FailedReallocSymbol = sym;
2494       }
2495     }
2496 
2497   // We are in a special mode if a reallocation failed later in the path.
2498   } else if (Mode == ReallocationFailed) {
2499     assert(FailedReallocSymbol && "No symbol to look for.");
2500 
2501     // Is this is the first appearance of the reallocated symbol?
2502     if (!statePrev->get<RegionState>(FailedReallocSymbol)) {
2503       // We're at the reallocation point.
2504       Msg = "Attempt to reallocate memory";
2505       StackHint = new StackHintGeneratorForSymbol(Sym,
2506                                                  "Returned reallocated memory");
2507       FailedReallocSymbol = nullptr;
2508       Mode = Normal;
2509     }
2510   }
2511 
2512   if (!Msg)
2513     return nullptr;
2514   assert(StackHint);
2515 
2516   // Generate the extra diagnostic.
2517   PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
2518                              N->getLocationContext());
2519   return new PathDiagnosticEventPiece(Pos, Msg, true, StackHint);
2520 }
2521 
2522 void MallocChecker::printState(raw_ostream &Out, ProgramStateRef State,
2523                                const char *NL, const char *Sep) const {
2524 
2525   RegionStateTy RS = State->get<RegionState>();
2526 
2527   if (!RS.isEmpty()) {
2528     Out << Sep << "MallocChecker :" << NL;
2529     for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
2530       const RefState *RefS = State->get<RegionState>(I.getKey());
2531       AllocationFamily Family = RefS->getAllocationFamily();
2532       Optional<MallocChecker::CheckKind> CheckKind = getCheckIfTracked(Family);
2533 
2534       I.getKey()->dumpToStream(Out);
2535       Out << " : ";
2536       I.getData().dump(Out);
2537       if (CheckKind.hasValue())
2538         Out << " (" << CheckNames[*CheckKind].getName() << ")";
2539       Out << NL;
2540     }
2541   }
2542 }
2543 
2544 void ento::registerNewDeleteLeaksChecker(CheckerManager &mgr) {
2545   registerCStringCheckerBasic(mgr);
2546   MallocChecker *checker = mgr.registerChecker<MallocChecker>();
2547   checker->IsOptimistic = mgr.getAnalyzerOptions().getBooleanOption(
2548       "Optimistic", false, checker);
2549   checker->ChecksEnabled[MallocChecker::CK_NewDeleteLeaksChecker] = true;
2550   checker->CheckNames[MallocChecker::CK_NewDeleteLeaksChecker] =
2551       mgr.getCurrentCheckName();
2552   // We currently treat NewDeleteLeaks checker as a subchecker of NewDelete
2553   // checker.
2554   if (!checker->ChecksEnabled[MallocChecker::CK_NewDeleteChecker])
2555     checker->ChecksEnabled[MallocChecker::CK_NewDeleteChecker] = true;
2556 }
2557 
2558 #define REGISTER_CHECKER(name)                                                 \
2559   void ento::register##name(CheckerManager &mgr) {                             \
2560     registerCStringCheckerBasic(mgr);                                          \
2561     MallocChecker *checker = mgr.registerChecker<MallocChecker>();             \
2562     checker->IsOptimistic = mgr.getAnalyzerOptions().getBooleanOption(         \
2563         "Optimistic", false, checker);                                         \
2564     checker->ChecksEnabled[MallocChecker::CK_##name] = true;                   \
2565     checker->CheckNames[MallocChecker::CK_##name] = mgr.getCurrentCheckName(); \
2566   }
2567 
2568 REGISTER_CHECKER(MallocChecker)
2569 REGISTER_CHECKER(NewDeleteChecker)
2570 REGISTER_CHECKER(MismatchedDeallocatorChecker)
2571