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