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