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