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       if (CE->getNumArgs() < 1)
782         return;
783       llvm::Optional<ProgramStateRef> MaybeState =
784         performKernelMalloc(CE, C, State);
785       if (MaybeState.hasValue())
786         State = MaybeState.getValue();
787       else
788         State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State);
789     } else if (FunI == II_valloc) {
790       if (CE->getNumArgs() < 1)
791         return;
792       State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State);
793       State = ProcessZeroAllocation(C, CE, 0, State);
794     } else if (FunI == II_realloc) {
795       State = ReallocMem(C, CE, false, State);
796       State = ProcessZeroAllocation(C, CE, 1, State);
797     } else if (FunI == II_reallocf) {
798       State = ReallocMem(C, CE, true, State);
799       State = ProcessZeroAllocation(C, CE, 1, State);
800     } else if (FunI == II_calloc) {
801       State = CallocMem(C, CE, State);
802       State = ProcessZeroAllocation(C, CE, 0, State);
803       State = ProcessZeroAllocation(C, CE, 1, State);
804     } else if (FunI == II_free) {
805       State = FreeMemAux(C, CE, State, 0, false, ReleasedAllocatedMemory);
806     } else if (FunI == II_strdup || FunI == II_win_strdup ||
807                FunI == II_wcsdup || FunI == II_win_wcsdup) {
808       State = MallocUpdateRefState(C, CE, State);
809     } else if (FunI == II_strndup) {
810       State = MallocUpdateRefState(C, CE, State);
811     } else if (FunI == II_alloca || FunI == II_win_alloca) {
812       if (CE->getNumArgs() < 1)
813         return;
814       State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State,
815                            AF_Alloca);
816       State = ProcessZeroAllocation(C, CE, 0, State);
817     } else if (isStandardNewDelete(FD, C.getASTContext())) {
818       // Process direct calls to operator new/new[]/delete/delete[] functions
819       // as distinct from new/new[]/delete/delete[] expressions that are
820       // processed by the checkPostStmt callbacks for CXXNewExpr and
821       // CXXDeleteExpr.
822       OverloadedOperatorKind K = FD->getOverloadedOperator();
823       if (K == OO_New) {
824         State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State,
825                              AF_CXXNew);
826         State = ProcessZeroAllocation(C, CE, 0, State);
827       }
828       else if (K == OO_Array_New) {
829         State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State,
830                              AF_CXXNewArray);
831         State = ProcessZeroAllocation(C, CE, 0, State);
832       }
833       else if (K == OO_Delete || K == OO_Array_Delete)
834         State = FreeMemAux(C, CE, State, 0, false, ReleasedAllocatedMemory);
835       else
836         llvm_unreachable("not a new/delete operator");
837     } else if (FunI == II_if_nameindex) {
838       // Should we model this differently? We can allocate a fixed number of
839       // elements with zeros in the last one.
840       State = MallocMemAux(C, CE, UnknownVal(), UnknownVal(), State,
841                            AF_IfNameIndex);
842     } else if (FunI == II_if_freenameindex) {
843       State = FreeMemAux(C, CE, State, 0, false, ReleasedAllocatedMemory);
844     }
845   }
846 
847   if (IsOptimistic || ChecksEnabled[CK_MismatchedDeallocatorChecker]) {
848     // Check all the attributes, if there are any.
849     // There can be multiple of these attributes.
850     if (FD->hasAttrs())
851       for (const auto *I : FD->specific_attrs<OwnershipAttr>()) {
852         switch (I->getOwnKind()) {
853         case OwnershipAttr::Returns:
854           State = MallocMemReturnsAttr(C, CE, I, State);
855           break;
856         case OwnershipAttr::Takes:
857         case OwnershipAttr::Holds:
858           State = FreeMemAttr(C, CE, I, State);
859           break;
860         }
861       }
862   }
863   C.addTransition(State);
864 }
865 
866 // Performs a 0-sized allocations check.
867 ProgramStateRef MallocChecker::ProcessZeroAllocation(CheckerContext &C,
868                                                const Expr *E,
869                                                const unsigned AllocationSizeArg,
870                                                ProgramStateRef State) const {
871   if (!State)
872     return nullptr;
873 
874   const Expr *Arg = nullptr;
875 
876   if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
877     Arg = CE->getArg(AllocationSizeArg);
878   }
879   else if (const CXXNewExpr *NE = dyn_cast<CXXNewExpr>(E)) {
880     if (NE->isArray())
881       Arg = NE->getArraySize();
882     else
883       return State;
884   }
885   else
886     llvm_unreachable("not a CallExpr or CXXNewExpr");
887 
888   assert(Arg);
889 
890   Optional<DefinedSVal> DefArgVal =
891       State->getSVal(Arg, C.getLocationContext()).getAs<DefinedSVal>();
892 
893   if (!DefArgVal)
894     return State;
895 
896   // Check if the allocation size is 0.
897   ProgramStateRef TrueState, FalseState;
898   SValBuilder &SvalBuilder = C.getSValBuilder();
899   DefinedSVal Zero =
900       SvalBuilder.makeZeroVal(Arg->getType()).castAs<DefinedSVal>();
901 
902   std::tie(TrueState, FalseState) =
903       State->assume(SvalBuilder.evalEQ(State, *DefArgVal, Zero));
904 
905   if (TrueState && !FalseState) {
906     SVal retVal = State->getSVal(E, C.getLocationContext());
907     SymbolRef Sym = retVal.getAsLocSymbol();
908     if (!Sym)
909       return State;
910 
911     const RefState *RS = State->get<RegionState>(Sym);
912     if (RS) {
913       if (RS->isAllocated())
914         return TrueState->set<RegionState>(Sym,
915                                           RefState::getAllocatedOfSizeZero(RS));
916       else
917         return State;
918     } else {
919       // Case of zero-size realloc. Historically 'realloc(ptr, 0)' is treated as
920       // 'free(ptr)' and the returned value from 'realloc(ptr, 0)' is not
921       // tracked. Add zero-reallocated Sym to the state to catch references
922       // to zero-allocated memory.
923       return TrueState->add<ReallocSizeZeroSymbols>(Sym);
924     }
925   }
926 
927   // Assume the value is non-zero going forward.
928   assert(FalseState);
929   return FalseState;
930 }
931 
932 static QualType getDeepPointeeType(QualType T) {
933   QualType Result = T, PointeeType = T->getPointeeType();
934   while (!PointeeType.isNull()) {
935     Result = PointeeType;
936     PointeeType = PointeeType->getPointeeType();
937   }
938   return Result;
939 }
940 
941 static bool treatUnusedNewEscaped(const CXXNewExpr *NE) {
942 
943   const CXXConstructExpr *ConstructE = NE->getConstructExpr();
944   if (!ConstructE)
945     return false;
946 
947   if (!NE->getAllocatedType()->getAsCXXRecordDecl())
948     return false;
949 
950   const CXXConstructorDecl *CtorD = ConstructE->getConstructor();
951 
952   // Iterate over the constructor parameters.
953   for (const auto *CtorParam : CtorD->parameters()) {
954 
955     QualType CtorParamPointeeT = CtorParam->getType()->getPointeeType();
956     if (CtorParamPointeeT.isNull())
957       continue;
958 
959     CtorParamPointeeT = getDeepPointeeType(CtorParamPointeeT);
960 
961     if (CtorParamPointeeT->getAsCXXRecordDecl())
962       return true;
963   }
964 
965   return false;
966 }
967 
968 void MallocChecker::checkPostStmt(const CXXNewExpr *NE,
969                                   CheckerContext &C) const {
970 
971   if (NE->getNumPlacementArgs())
972     for (CXXNewExpr::const_arg_iterator I = NE->placement_arg_begin(),
973          E = NE->placement_arg_end(); I != E; ++I)
974       if (SymbolRef Sym = C.getSVal(*I).getAsSymbol())
975         checkUseAfterFree(Sym, C, *I);
976 
977   if (!isStandardNewDelete(NE->getOperatorNew(), C.getASTContext()))
978     return;
979 
980   ParentMap &PM = C.getLocationContext()->getParentMap();
981   if (!PM.isConsumedExpr(NE) && treatUnusedNewEscaped(NE))
982     return;
983 
984   ProgramStateRef State = C.getState();
985   // The return value from operator new is bound to a specified initialization
986   // value (if any) and we don't want to loose this value. So we call
987   // MallocUpdateRefState() instead of MallocMemAux() which breakes the
988   // existing binding.
989   State = MallocUpdateRefState(C, NE, State, NE->isArray() ? AF_CXXNewArray
990                                                            : AF_CXXNew);
991   State = addExtentSize(C, NE, State);
992   State = ProcessZeroAllocation(C, NE, 0, State);
993   C.addTransition(State);
994 }
995 
996 // Sets the extent value of the MemRegion allocated by
997 // new expression NE to its size in Bytes.
998 //
999 ProgramStateRef MallocChecker::addExtentSize(CheckerContext &C,
1000                                              const CXXNewExpr *NE,
1001                                              ProgramStateRef State) {
1002   if (!State)
1003     return nullptr;
1004   SValBuilder &svalBuilder = C.getSValBuilder();
1005   SVal ElementCount;
1006   const LocationContext *LCtx = C.getLocationContext();
1007   const SubRegion *Region;
1008   if (NE->isArray()) {
1009     const Expr *SizeExpr = NE->getArraySize();
1010     ElementCount = State->getSVal(SizeExpr, C.getLocationContext());
1011     // Store the extent size for the (symbolic)region
1012     // containing the elements.
1013     Region = (State->getSVal(NE, LCtx))
1014                  .getAsRegion()
1015                  ->getAs<SubRegion>()
1016                  ->getSuperRegion()
1017                  ->getAs<SubRegion>();
1018   } else {
1019     ElementCount = svalBuilder.makeIntVal(1, true);
1020     Region = (State->getSVal(NE, LCtx)).getAsRegion()->getAs<SubRegion>();
1021   }
1022   assert(Region);
1023 
1024   // Set the region's extent equal to the Size in Bytes.
1025   QualType ElementType = NE->getAllocatedType();
1026   ASTContext &AstContext = C.getASTContext();
1027   CharUnits TypeSize = AstContext.getTypeSizeInChars(ElementType);
1028 
1029   if (Optional<DefinedOrUnknownSVal> DefinedSize =
1030           ElementCount.getAs<DefinedOrUnknownSVal>()) {
1031     DefinedOrUnknownSVal Extent = Region->getExtent(svalBuilder);
1032     // size in Bytes = ElementCount*TypeSize
1033     SVal SizeInBytes = svalBuilder.evalBinOpNN(
1034         State, BO_Mul, ElementCount.castAs<NonLoc>(),
1035         svalBuilder.makeArrayIndex(TypeSize.getQuantity()),
1036         svalBuilder.getArrayIndexType());
1037     DefinedOrUnknownSVal extentMatchesSize = svalBuilder.evalEQ(
1038         State, Extent, SizeInBytes.castAs<DefinedOrUnknownSVal>());
1039     State = State->assume(extentMatchesSize, true);
1040   }
1041   return State;
1042 }
1043 
1044 void MallocChecker::checkPreStmt(const CXXDeleteExpr *DE,
1045                                  CheckerContext &C) const {
1046 
1047   if (!ChecksEnabled[CK_NewDeleteChecker])
1048     if (SymbolRef Sym = C.getSVal(DE->getArgument()).getAsSymbol())
1049       checkUseAfterFree(Sym, C, DE->getArgument());
1050 
1051   if (!isStandardNewDelete(DE->getOperatorDelete(), C.getASTContext()))
1052     return;
1053 
1054   ProgramStateRef State = C.getState();
1055   bool ReleasedAllocated;
1056   State = FreeMemAux(C, DE->getArgument(), DE, State,
1057                      /*Hold*/false, ReleasedAllocated);
1058 
1059   C.addTransition(State);
1060 }
1061 
1062 static bool isKnownDeallocObjCMethodName(const ObjCMethodCall &Call) {
1063   // If the first selector piece is one of the names below, assume that the
1064   // object takes ownership of the memory, promising to eventually deallocate it
1065   // with free().
1066   // Ex:  [NSData dataWithBytesNoCopy:bytes length:10];
1067   // (...unless a 'freeWhenDone' parameter is false, but that's checked later.)
1068   StringRef FirstSlot = Call.getSelector().getNameForSlot(0);
1069   return FirstSlot == "dataWithBytesNoCopy" ||
1070          FirstSlot == "initWithBytesNoCopy" ||
1071          FirstSlot == "initWithCharactersNoCopy";
1072 }
1073 
1074 static Optional<bool> getFreeWhenDoneArg(const ObjCMethodCall &Call) {
1075   Selector S = Call.getSelector();
1076 
1077   // FIXME: We should not rely on fully-constrained symbols being folded.
1078   for (unsigned i = 1; i < S.getNumArgs(); ++i)
1079     if (S.getNameForSlot(i).equals("freeWhenDone"))
1080       return !Call.getArgSVal(i).isZeroConstant();
1081 
1082   return None;
1083 }
1084 
1085 void MallocChecker::checkPostObjCMessage(const ObjCMethodCall &Call,
1086                                          CheckerContext &C) const {
1087   if (C.wasInlined)
1088     return;
1089 
1090   if (!isKnownDeallocObjCMethodName(Call))
1091     return;
1092 
1093   if (Optional<bool> FreeWhenDone = getFreeWhenDoneArg(Call))
1094     if (!*FreeWhenDone)
1095       return;
1096 
1097   bool ReleasedAllocatedMemory;
1098   ProgramStateRef State = FreeMemAux(C, Call.getArgExpr(0),
1099                                      Call.getOriginExpr(), C.getState(),
1100                                      /*Hold=*/true, ReleasedAllocatedMemory,
1101                                      /*RetNullOnFailure=*/true);
1102 
1103   C.addTransition(State);
1104 }
1105 
1106 ProgramStateRef
1107 MallocChecker::MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,
1108                                     const OwnershipAttr *Att,
1109                                     ProgramStateRef State) const {
1110   if (!State)
1111     return nullptr;
1112 
1113   if (Att->getModule() != II_malloc)
1114     return nullptr;
1115 
1116   OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
1117   if (I != E) {
1118     return MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), State);
1119   }
1120   return MallocMemAux(C, CE, UnknownVal(), UndefinedVal(), State);
1121 }
1122 
1123 ProgramStateRef MallocChecker::MallocMemAux(CheckerContext &C,
1124                                             const CallExpr *CE,
1125                                             const Expr *SizeEx, SVal Init,
1126                                             ProgramStateRef State,
1127                                             AllocationFamily Family) {
1128   if (!State)
1129     return nullptr;
1130 
1131   return MallocMemAux(C, CE, State->getSVal(SizeEx, C.getLocationContext()),
1132                       Init, State, Family);
1133 }
1134 
1135 ProgramStateRef MallocChecker::MallocMemAux(CheckerContext &C,
1136                                            const CallExpr *CE,
1137                                            SVal Size, SVal Init,
1138                                            ProgramStateRef State,
1139                                            AllocationFamily Family) {
1140   if (!State)
1141     return nullptr;
1142 
1143   // We expect the malloc functions to return a pointer.
1144   if (!Loc::isLocType(CE->getType()))
1145     return nullptr;
1146 
1147   // Bind the return value to the symbolic value from the heap region.
1148   // TODO: We could rewrite post visit to eval call; 'malloc' does not have
1149   // side effects other than what we model here.
1150   unsigned Count = C.blockCount();
1151   SValBuilder &svalBuilder = C.getSValBuilder();
1152   const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
1153   DefinedSVal RetVal = svalBuilder.getConjuredHeapSymbolVal(CE, LCtx, Count)
1154       .castAs<DefinedSVal>();
1155   State = State->BindExpr(CE, C.getLocationContext(), RetVal);
1156 
1157   // Fill the region with the initialization value.
1158   State = State->bindDefault(RetVal, Init);
1159 
1160   // Set the region's extent equal to the Size parameter.
1161   const SymbolicRegion *R =
1162       dyn_cast_or_null<SymbolicRegion>(RetVal.getAsRegion());
1163   if (!R)
1164     return nullptr;
1165   if (Optional<DefinedOrUnknownSVal> DefinedSize =
1166           Size.getAs<DefinedOrUnknownSVal>()) {
1167     SValBuilder &svalBuilder = C.getSValBuilder();
1168     DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
1169     DefinedOrUnknownSVal extentMatchesSize =
1170         svalBuilder.evalEQ(State, Extent, *DefinedSize);
1171 
1172     State = State->assume(extentMatchesSize, true);
1173     assert(State);
1174   }
1175 
1176   return MallocUpdateRefState(C, CE, State, Family);
1177 }
1178 
1179 ProgramStateRef MallocChecker::MallocUpdateRefState(CheckerContext &C,
1180                                                     const Expr *E,
1181                                                     ProgramStateRef State,
1182                                                     AllocationFamily Family) {
1183   if (!State)
1184     return nullptr;
1185 
1186   // Get the return value.
1187   SVal retVal = State->getSVal(E, C.getLocationContext());
1188 
1189   // We expect the malloc functions to return a pointer.
1190   if (!retVal.getAs<Loc>())
1191     return nullptr;
1192 
1193   SymbolRef Sym = retVal.getAsLocSymbol();
1194   assert(Sym);
1195 
1196   // Set the symbol's state to Allocated.
1197   return State->set<RegionState>(Sym, RefState::getAllocated(Family, E));
1198 }
1199 
1200 ProgramStateRef MallocChecker::FreeMemAttr(CheckerContext &C,
1201                                            const CallExpr *CE,
1202                                            const OwnershipAttr *Att,
1203                                            ProgramStateRef State) const {
1204   if (!State)
1205     return nullptr;
1206 
1207   if (Att->getModule() != II_malloc)
1208     return nullptr;
1209 
1210   bool ReleasedAllocated = false;
1211 
1212   for (const auto &Arg : Att->args()) {
1213     ProgramStateRef StateI = FreeMemAux(C, CE, State, Arg,
1214                                Att->getOwnKind() == OwnershipAttr::Holds,
1215                                ReleasedAllocated);
1216     if (StateI)
1217       State = StateI;
1218   }
1219   return State;
1220 }
1221 
1222 ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C,
1223                                           const CallExpr *CE,
1224                                           ProgramStateRef State,
1225                                           unsigned Num,
1226                                           bool Hold,
1227                                           bool &ReleasedAllocated,
1228                                           bool ReturnsNullOnFailure) const {
1229   if (!State)
1230     return nullptr;
1231 
1232   if (CE->getNumArgs() < (Num + 1))
1233     return nullptr;
1234 
1235   return FreeMemAux(C, CE->getArg(Num), CE, State, Hold,
1236                     ReleasedAllocated, ReturnsNullOnFailure);
1237 }
1238 
1239 /// Checks if the previous call to free on the given symbol failed - if free
1240 /// failed, returns true. Also, returns the corresponding return value symbol.
1241 static bool didPreviousFreeFail(ProgramStateRef State,
1242                                 SymbolRef Sym, SymbolRef &RetStatusSymbol) {
1243   const SymbolRef *Ret = State->get<FreeReturnValue>(Sym);
1244   if (Ret) {
1245     assert(*Ret && "We should not store the null return symbol");
1246     ConstraintManager &CMgr = State->getConstraintManager();
1247     ConditionTruthVal FreeFailed = CMgr.isNull(State, *Ret);
1248     RetStatusSymbol = *Ret;
1249     return FreeFailed.isConstrainedTrue();
1250   }
1251   return false;
1252 }
1253 
1254 AllocationFamily MallocChecker::getAllocationFamily(CheckerContext &C,
1255                                                     const Stmt *S) const {
1256   if (!S)
1257     return AF_None;
1258 
1259   if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
1260     const FunctionDecl *FD = C.getCalleeDecl(CE);
1261 
1262     if (!FD)
1263       FD = dyn_cast<FunctionDecl>(CE->getCalleeDecl());
1264 
1265     ASTContext &Ctx = C.getASTContext();
1266 
1267     if (isCMemFunction(FD, Ctx, AF_Malloc, MemoryOperationKind::MOK_Any))
1268       return AF_Malloc;
1269 
1270     if (isStandardNewDelete(FD, Ctx)) {
1271       OverloadedOperatorKind Kind = FD->getOverloadedOperator();
1272       if (Kind == OO_New || Kind == OO_Delete)
1273         return AF_CXXNew;
1274       else if (Kind == OO_Array_New || Kind == OO_Array_Delete)
1275         return AF_CXXNewArray;
1276     }
1277 
1278     if (isCMemFunction(FD, Ctx, AF_IfNameIndex, MemoryOperationKind::MOK_Any))
1279       return AF_IfNameIndex;
1280 
1281     if (isCMemFunction(FD, Ctx, AF_Alloca, MemoryOperationKind::MOK_Any))
1282       return AF_Alloca;
1283 
1284     return AF_None;
1285   }
1286 
1287   if (const CXXNewExpr *NE = dyn_cast<CXXNewExpr>(S))
1288     return NE->isArray() ? AF_CXXNewArray : AF_CXXNew;
1289 
1290   if (const CXXDeleteExpr *DE = dyn_cast<CXXDeleteExpr>(S))
1291     return DE->isArrayForm() ? AF_CXXNewArray : AF_CXXNew;
1292 
1293   if (isa<ObjCMessageExpr>(S))
1294     return AF_Malloc;
1295 
1296   return AF_None;
1297 }
1298 
1299 bool MallocChecker::printAllocDeallocName(raw_ostream &os, CheckerContext &C,
1300                                           const Expr *E) const {
1301   if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1302     // FIXME: This doesn't handle indirect calls.
1303     const FunctionDecl *FD = CE->getDirectCallee();
1304     if (!FD)
1305       return false;
1306 
1307     os << *FD;
1308     if (!FD->isOverloadedOperator())
1309       os << "()";
1310     return true;
1311   }
1312 
1313   if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) {
1314     if (Msg->isInstanceMessage())
1315       os << "-";
1316     else
1317       os << "+";
1318     Msg->getSelector().print(os);
1319     return true;
1320   }
1321 
1322   if (const CXXNewExpr *NE = dyn_cast<CXXNewExpr>(E)) {
1323     os << "'"
1324        << getOperatorSpelling(NE->getOperatorNew()->getOverloadedOperator())
1325        << "'";
1326     return true;
1327   }
1328 
1329   if (const CXXDeleteExpr *DE = dyn_cast<CXXDeleteExpr>(E)) {
1330     os << "'"
1331        << getOperatorSpelling(DE->getOperatorDelete()->getOverloadedOperator())
1332        << "'";
1333     return true;
1334   }
1335 
1336   return false;
1337 }
1338 
1339 void MallocChecker::printExpectedAllocName(raw_ostream &os, CheckerContext &C,
1340                                            const Expr *E) const {
1341   AllocationFamily Family = getAllocationFamily(C, E);
1342 
1343   switch(Family) {
1344     case AF_Malloc: os << "malloc()"; return;
1345     case AF_CXXNew: os << "'new'"; return;
1346     case AF_CXXNewArray: os << "'new[]'"; return;
1347     case AF_IfNameIndex: os << "'if_nameindex()'"; return;
1348     case AF_Alloca:
1349     case AF_None: llvm_unreachable("not a deallocation expression");
1350   }
1351 }
1352 
1353 void MallocChecker::printExpectedDeallocName(raw_ostream &os,
1354                                              AllocationFamily Family) const {
1355   switch(Family) {
1356     case AF_Malloc: os << "free()"; return;
1357     case AF_CXXNew: os << "'delete'"; return;
1358     case AF_CXXNewArray: os << "'delete[]'"; return;
1359     case AF_IfNameIndex: os << "'if_freenameindex()'"; return;
1360     case AF_Alloca:
1361     case AF_None: llvm_unreachable("suspicious argument");
1362   }
1363 }
1364 
1365 ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C,
1366                                           const Expr *ArgExpr,
1367                                           const Expr *ParentExpr,
1368                                           ProgramStateRef State,
1369                                           bool Hold,
1370                                           bool &ReleasedAllocated,
1371                                           bool ReturnsNullOnFailure) const {
1372 
1373   if (!State)
1374     return nullptr;
1375 
1376   SVal ArgVal = State->getSVal(ArgExpr, C.getLocationContext());
1377   if (!ArgVal.getAs<DefinedOrUnknownSVal>())
1378     return nullptr;
1379   DefinedOrUnknownSVal location = ArgVal.castAs<DefinedOrUnknownSVal>();
1380 
1381   // Check for null dereferences.
1382   if (!location.getAs<Loc>())
1383     return nullptr;
1384 
1385   // The explicit NULL case, no operation is performed.
1386   ProgramStateRef notNullState, nullState;
1387   std::tie(notNullState, nullState) = State->assume(location);
1388   if (nullState && !notNullState)
1389     return nullptr;
1390 
1391   // Unknown values could easily be okay
1392   // Undefined values are handled elsewhere
1393   if (ArgVal.isUnknownOrUndef())
1394     return nullptr;
1395 
1396   const MemRegion *R = ArgVal.getAsRegion();
1397 
1398   // Nonlocs can't be freed, of course.
1399   // Non-region locations (labels and fixed addresses) also shouldn't be freed.
1400   if (!R) {
1401     ReportBadFree(C, ArgVal, ArgExpr->getSourceRange(), ParentExpr);
1402     return nullptr;
1403   }
1404 
1405   R = R->StripCasts();
1406 
1407   // Blocks might show up as heap data, but should not be free()d
1408   if (isa<BlockDataRegion>(R)) {
1409     ReportBadFree(C, ArgVal, ArgExpr->getSourceRange(), ParentExpr);
1410     return nullptr;
1411   }
1412 
1413   const MemSpaceRegion *MS = R->getMemorySpace();
1414 
1415   // Parameters, locals, statics, globals, and memory returned by
1416   // __builtin_alloca() shouldn't be freed.
1417   if (!(isa<UnknownSpaceRegion>(MS) || isa<HeapSpaceRegion>(MS))) {
1418     // FIXME: at the time this code was written, malloc() regions were
1419     // represented by conjured symbols, which are all in UnknownSpaceRegion.
1420     // This means that there isn't actually anything from HeapSpaceRegion
1421     // that should be freed, even though we allow it here.
1422     // Of course, free() can work on memory allocated outside the current
1423     // function, so UnknownSpaceRegion is always a possibility.
1424     // False negatives are better than false positives.
1425 
1426     if (isa<AllocaRegion>(R))
1427       ReportFreeAlloca(C, ArgVal, ArgExpr->getSourceRange());
1428     else
1429       ReportBadFree(C, ArgVal, ArgExpr->getSourceRange(), ParentExpr);
1430 
1431     return nullptr;
1432   }
1433 
1434   const SymbolicRegion *SrBase = dyn_cast<SymbolicRegion>(R->getBaseRegion());
1435   // Various cases could lead to non-symbol values here.
1436   // For now, ignore them.
1437   if (!SrBase)
1438     return nullptr;
1439 
1440   SymbolRef SymBase = SrBase->getSymbol();
1441   const RefState *RsBase = State->get<RegionState>(SymBase);
1442   SymbolRef PreviousRetStatusSymbol = nullptr;
1443 
1444   if (RsBase) {
1445 
1446     // Memory returned by alloca() shouldn't be freed.
1447     if (RsBase->getAllocationFamily() == AF_Alloca) {
1448       ReportFreeAlloca(C, ArgVal, ArgExpr->getSourceRange());
1449       return nullptr;
1450     }
1451 
1452     // Check for double free first.
1453     if ((RsBase->isReleased() || RsBase->isRelinquished()) &&
1454         !didPreviousFreeFail(State, SymBase, PreviousRetStatusSymbol)) {
1455       ReportDoubleFree(C, ParentExpr->getSourceRange(), RsBase->isReleased(),
1456                        SymBase, PreviousRetStatusSymbol);
1457       return nullptr;
1458 
1459     // If the pointer is allocated or escaped, but we are now trying to free it,
1460     // check that the call to free is proper.
1461     } else if (RsBase->isAllocated() || RsBase->isAllocatedOfSizeZero() ||
1462                RsBase->isEscaped()) {
1463 
1464       // Check if an expected deallocation function matches the real one.
1465       bool DeallocMatchesAlloc =
1466         RsBase->getAllocationFamily() == getAllocationFamily(C, ParentExpr);
1467       if (!DeallocMatchesAlloc) {
1468         ReportMismatchedDealloc(C, ArgExpr->getSourceRange(),
1469                                 ParentExpr, RsBase, SymBase, Hold);
1470         return nullptr;
1471       }
1472 
1473       // Check if the memory location being freed is the actual location
1474       // allocated, or an offset.
1475       RegionOffset Offset = R->getAsOffset();
1476       if (Offset.isValid() &&
1477           !Offset.hasSymbolicOffset() &&
1478           Offset.getOffset() != 0) {
1479         const Expr *AllocExpr = cast<Expr>(RsBase->getStmt());
1480         ReportOffsetFree(C, ArgVal, ArgExpr->getSourceRange(), ParentExpr,
1481                          AllocExpr);
1482         return nullptr;
1483       }
1484     }
1485   }
1486 
1487   ReleasedAllocated = (RsBase != nullptr) && (RsBase->isAllocated() ||
1488                                               RsBase->isAllocatedOfSizeZero());
1489 
1490   // Clean out the info on previous call to free return info.
1491   State = State->remove<FreeReturnValue>(SymBase);
1492 
1493   // Keep track of the return value. If it is NULL, we will know that free
1494   // failed.
1495   if (ReturnsNullOnFailure) {
1496     SVal RetVal = C.getSVal(ParentExpr);
1497     SymbolRef RetStatusSymbol = RetVal.getAsSymbol();
1498     if (RetStatusSymbol) {
1499       C.getSymbolManager().addSymbolDependency(SymBase, RetStatusSymbol);
1500       State = State->set<FreeReturnValue>(SymBase, RetStatusSymbol);
1501     }
1502   }
1503 
1504   AllocationFamily Family = RsBase ? RsBase->getAllocationFamily()
1505                                    : getAllocationFamily(C, ParentExpr);
1506   // Normal free.
1507   if (Hold)
1508     return State->set<RegionState>(SymBase,
1509                                    RefState::getRelinquished(Family,
1510                                                              ParentExpr));
1511 
1512   return State->set<RegionState>(SymBase,
1513                                  RefState::getReleased(Family, ParentExpr));
1514 }
1515 
1516 Optional<MallocChecker::CheckKind>
1517 MallocChecker::getCheckIfTracked(AllocationFamily Family,
1518                                  bool IsALeakCheck) const {
1519   switch (Family) {
1520   case AF_Malloc:
1521   case AF_Alloca:
1522   case AF_IfNameIndex: {
1523     if (ChecksEnabled[CK_MallocChecker])
1524       return CK_MallocChecker;
1525 
1526     return Optional<MallocChecker::CheckKind>();
1527   }
1528   case AF_CXXNew:
1529   case AF_CXXNewArray: {
1530     if (IsALeakCheck) {
1531       if (ChecksEnabled[CK_NewDeleteLeaksChecker])
1532         return CK_NewDeleteLeaksChecker;
1533     }
1534     else {
1535       if (ChecksEnabled[CK_NewDeleteChecker])
1536         return CK_NewDeleteChecker;
1537     }
1538     return Optional<MallocChecker::CheckKind>();
1539   }
1540   case AF_None: {
1541     llvm_unreachable("no family");
1542   }
1543   }
1544   llvm_unreachable("unhandled family");
1545 }
1546 
1547 Optional<MallocChecker::CheckKind>
1548 MallocChecker::getCheckIfTracked(CheckerContext &C,
1549                                  const Stmt *AllocDeallocStmt,
1550                                  bool IsALeakCheck) const {
1551   return getCheckIfTracked(getAllocationFamily(C, AllocDeallocStmt),
1552                            IsALeakCheck);
1553 }
1554 
1555 Optional<MallocChecker::CheckKind>
1556 MallocChecker::getCheckIfTracked(CheckerContext &C, SymbolRef Sym,
1557                                  bool IsALeakCheck) const {
1558   if (C.getState()->contains<ReallocSizeZeroSymbols>(Sym))
1559     return CK_MallocChecker;
1560 
1561   const RefState *RS = C.getState()->get<RegionState>(Sym);
1562   assert(RS);
1563   return getCheckIfTracked(RS->getAllocationFamily(), IsALeakCheck);
1564 }
1565 
1566 bool MallocChecker::SummarizeValue(raw_ostream &os, SVal V) {
1567   if (Optional<nonloc::ConcreteInt> IntVal = V.getAs<nonloc::ConcreteInt>())
1568     os << "an integer (" << IntVal->getValue() << ")";
1569   else if (Optional<loc::ConcreteInt> ConstAddr = V.getAs<loc::ConcreteInt>())
1570     os << "a constant address (" << ConstAddr->getValue() << ")";
1571   else if (Optional<loc::GotoLabel> Label = V.getAs<loc::GotoLabel>())
1572     os << "the address of the label '" << Label->getLabel()->getName() << "'";
1573   else
1574     return false;
1575 
1576   return true;
1577 }
1578 
1579 bool MallocChecker::SummarizeRegion(raw_ostream &os,
1580                                     const MemRegion *MR) {
1581   switch (MR->getKind()) {
1582   case MemRegion::FunctionCodeRegionKind: {
1583     const NamedDecl *FD = cast<FunctionCodeRegion>(MR)->getDecl();
1584     if (FD)
1585       os << "the address of the function '" << *FD << '\'';
1586     else
1587       os << "the address of a function";
1588     return true;
1589   }
1590   case MemRegion::BlockCodeRegionKind:
1591     os << "block text";
1592     return true;
1593   case MemRegion::BlockDataRegionKind:
1594     // FIXME: where the block came from?
1595     os << "a block";
1596     return true;
1597   default: {
1598     const MemSpaceRegion *MS = MR->getMemorySpace();
1599 
1600     if (isa<StackLocalsSpaceRegion>(MS)) {
1601       const VarRegion *VR = dyn_cast<VarRegion>(MR);
1602       const VarDecl *VD;
1603       if (VR)
1604         VD = VR->getDecl();
1605       else
1606         VD = nullptr;
1607 
1608       if (VD)
1609         os << "the address of the local variable '" << VD->getName() << "'";
1610       else
1611         os << "the address of a local stack variable";
1612       return true;
1613     }
1614 
1615     if (isa<StackArgumentsSpaceRegion>(MS)) {
1616       const VarRegion *VR = dyn_cast<VarRegion>(MR);
1617       const VarDecl *VD;
1618       if (VR)
1619         VD = VR->getDecl();
1620       else
1621         VD = nullptr;
1622 
1623       if (VD)
1624         os << "the address of the parameter '" << VD->getName() << "'";
1625       else
1626         os << "the address of a parameter";
1627       return true;
1628     }
1629 
1630     if (isa<GlobalsSpaceRegion>(MS)) {
1631       const VarRegion *VR = dyn_cast<VarRegion>(MR);
1632       const VarDecl *VD;
1633       if (VR)
1634         VD = VR->getDecl();
1635       else
1636         VD = nullptr;
1637 
1638       if (VD) {
1639         if (VD->isStaticLocal())
1640           os << "the address of the static variable '" << VD->getName() << "'";
1641         else
1642           os << "the address of the global variable '" << VD->getName() << "'";
1643       } else
1644         os << "the address of a global variable";
1645       return true;
1646     }
1647 
1648     return false;
1649   }
1650   }
1651 }
1652 
1653 void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal,
1654                                   SourceRange Range,
1655                                   const Expr *DeallocExpr) const {
1656 
1657   if (!ChecksEnabled[CK_MallocChecker] &&
1658       !ChecksEnabled[CK_NewDeleteChecker])
1659     return;
1660 
1661   Optional<MallocChecker::CheckKind> CheckKind =
1662       getCheckIfTracked(C, DeallocExpr);
1663   if (!CheckKind.hasValue())
1664     return;
1665 
1666   if (ExplodedNode *N = C.generateErrorNode()) {
1667     if (!BT_BadFree[*CheckKind])
1668       BT_BadFree[*CheckKind].reset(
1669           new BugType(CheckNames[*CheckKind], "Bad free", "Memory Error"));
1670 
1671     SmallString<100> buf;
1672     llvm::raw_svector_ostream os(buf);
1673 
1674     const MemRegion *MR = ArgVal.getAsRegion();
1675     while (const ElementRegion *ER = dyn_cast_or_null<ElementRegion>(MR))
1676       MR = ER->getSuperRegion();
1677 
1678     os << "Argument to ";
1679     if (!printAllocDeallocName(os, C, DeallocExpr))
1680       os << "deallocator";
1681 
1682     os << " is ";
1683     bool Summarized = MR ? SummarizeRegion(os, MR)
1684                          : SummarizeValue(os, ArgVal);
1685     if (Summarized)
1686       os << ", which is not memory allocated by ";
1687     else
1688       os << "not memory allocated by ";
1689 
1690     printExpectedAllocName(os, C, DeallocExpr);
1691 
1692     auto R = llvm::make_unique<BugReport>(*BT_BadFree[*CheckKind], os.str(), N);
1693     R->markInteresting(MR);
1694     R->addRange(Range);
1695     C.emitReport(std::move(R));
1696   }
1697 }
1698 
1699 void MallocChecker::ReportFreeAlloca(CheckerContext &C, SVal ArgVal,
1700                                      SourceRange Range) const {
1701 
1702   Optional<MallocChecker::CheckKind> CheckKind;
1703 
1704   if (ChecksEnabled[CK_MallocChecker])
1705     CheckKind = CK_MallocChecker;
1706   else if (ChecksEnabled[CK_MismatchedDeallocatorChecker])
1707     CheckKind = CK_MismatchedDeallocatorChecker;
1708   else
1709     return;
1710 
1711   if (ExplodedNode *N = C.generateErrorNode()) {
1712     if (!BT_FreeAlloca[*CheckKind])
1713       BT_FreeAlloca[*CheckKind].reset(
1714           new BugType(CheckNames[*CheckKind], "Free alloca()", "Memory Error"));
1715 
1716     auto R = llvm::make_unique<BugReport>(
1717         *BT_FreeAlloca[*CheckKind],
1718         "Memory allocated by alloca() should not be deallocated", N);
1719     R->markInteresting(ArgVal.getAsRegion());
1720     R->addRange(Range);
1721     C.emitReport(std::move(R));
1722   }
1723 }
1724 
1725 void MallocChecker::ReportMismatchedDealloc(CheckerContext &C,
1726                                             SourceRange Range,
1727                                             const Expr *DeallocExpr,
1728                                             const RefState *RS,
1729                                             SymbolRef Sym,
1730                                             bool OwnershipTransferred) const {
1731 
1732   if (!ChecksEnabled[CK_MismatchedDeallocatorChecker])
1733     return;
1734 
1735   if (ExplodedNode *N = C.generateErrorNode()) {
1736     if (!BT_MismatchedDealloc)
1737       BT_MismatchedDealloc.reset(
1738           new BugType(CheckNames[CK_MismatchedDeallocatorChecker],
1739                       "Bad deallocator", "Memory Error"));
1740 
1741     SmallString<100> buf;
1742     llvm::raw_svector_ostream os(buf);
1743 
1744     const Expr *AllocExpr = cast<Expr>(RS->getStmt());
1745     SmallString<20> AllocBuf;
1746     llvm::raw_svector_ostream AllocOs(AllocBuf);
1747     SmallString<20> DeallocBuf;
1748     llvm::raw_svector_ostream DeallocOs(DeallocBuf);
1749 
1750     if (OwnershipTransferred) {
1751       if (printAllocDeallocName(DeallocOs, C, DeallocExpr))
1752         os << DeallocOs.str() << " cannot";
1753       else
1754         os << "Cannot";
1755 
1756       os << " take ownership of memory";
1757 
1758       if (printAllocDeallocName(AllocOs, C, AllocExpr))
1759         os << " allocated by " << AllocOs.str();
1760     } else {
1761       os << "Memory";
1762       if (printAllocDeallocName(AllocOs, C, AllocExpr))
1763         os << " allocated by " << AllocOs.str();
1764 
1765       os << " should be deallocated by ";
1766         printExpectedDeallocName(os, RS->getAllocationFamily());
1767 
1768       if (printAllocDeallocName(DeallocOs, C, DeallocExpr))
1769         os << ", not " << DeallocOs.str();
1770     }
1771 
1772     auto R = llvm::make_unique<BugReport>(*BT_MismatchedDealloc, os.str(), N);
1773     R->markInteresting(Sym);
1774     R->addRange(Range);
1775     R->addVisitor(llvm::make_unique<MallocBugVisitor>(Sym));
1776     C.emitReport(std::move(R));
1777   }
1778 }
1779 
1780 void MallocChecker::ReportOffsetFree(CheckerContext &C, SVal ArgVal,
1781                                      SourceRange Range, const Expr *DeallocExpr,
1782                                      const Expr *AllocExpr) const {
1783 
1784 
1785   if (!ChecksEnabled[CK_MallocChecker] &&
1786       !ChecksEnabled[CK_NewDeleteChecker])
1787     return;
1788 
1789   Optional<MallocChecker::CheckKind> CheckKind =
1790       getCheckIfTracked(C, AllocExpr);
1791   if (!CheckKind.hasValue())
1792     return;
1793 
1794   ExplodedNode *N = C.generateErrorNode();
1795   if (!N)
1796     return;
1797 
1798   if (!BT_OffsetFree[*CheckKind])
1799     BT_OffsetFree[*CheckKind].reset(
1800         new BugType(CheckNames[*CheckKind], "Offset free", "Memory Error"));
1801 
1802   SmallString<100> buf;
1803   llvm::raw_svector_ostream os(buf);
1804   SmallString<20> AllocNameBuf;
1805   llvm::raw_svector_ostream AllocNameOs(AllocNameBuf);
1806 
1807   const MemRegion *MR = ArgVal.getAsRegion();
1808   assert(MR && "Only MemRegion based symbols can have offset free errors");
1809 
1810   RegionOffset Offset = MR->getAsOffset();
1811   assert((Offset.isValid() &&
1812           !Offset.hasSymbolicOffset() &&
1813           Offset.getOffset() != 0) &&
1814          "Only symbols with a valid offset can have offset free errors");
1815 
1816   int offsetBytes = Offset.getOffset() / C.getASTContext().getCharWidth();
1817 
1818   os << "Argument to ";
1819   if (!printAllocDeallocName(os, C, DeallocExpr))
1820     os << "deallocator";
1821   os << " is offset by "
1822      << offsetBytes
1823      << " "
1824      << ((abs(offsetBytes) > 1) ? "bytes" : "byte")
1825      << " from the start of ";
1826   if (AllocExpr && printAllocDeallocName(AllocNameOs, C, AllocExpr))
1827     os << "memory allocated by " << AllocNameOs.str();
1828   else
1829     os << "allocated memory";
1830 
1831   auto R = llvm::make_unique<BugReport>(*BT_OffsetFree[*CheckKind], os.str(), N);
1832   R->markInteresting(MR->getBaseRegion());
1833   R->addRange(Range);
1834   C.emitReport(std::move(R));
1835 }
1836 
1837 void MallocChecker::ReportUseAfterFree(CheckerContext &C, SourceRange Range,
1838                                        SymbolRef Sym) const {
1839 
1840   if (!ChecksEnabled[CK_MallocChecker] &&
1841       !ChecksEnabled[CK_NewDeleteChecker])
1842     return;
1843 
1844   Optional<MallocChecker::CheckKind> CheckKind = getCheckIfTracked(C, Sym);
1845   if (!CheckKind.hasValue())
1846     return;
1847 
1848   if (ExplodedNode *N = C.generateErrorNode()) {
1849     if (!BT_UseFree[*CheckKind])
1850       BT_UseFree[*CheckKind].reset(new BugType(
1851           CheckNames[*CheckKind], "Use-after-free", "Memory Error"));
1852 
1853     auto R = llvm::make_unique<BugReport>(*BT_UseFree[*CheckKind],
1854                                          "Use of memory after it is freed", N);
1855 
1856     R->markInteresting(Sym);
1857     R->addRange(Range);
1858     R->addVisitor(llvm::make_unique<MallocBugVisitor>(Sym));
1859     C.emitReport(std::move(R));
1860   }
1861 }
1862 
1863 void MallocChecker::ReportDoubleFree(CheckerContext &C, SourceRange Range,
1864                                      bool Released, SymbolRef Sym,
1865                                      SymbolRef PrevSym) const {
1866 
1867   if (!ChecksEnabled[CK_MallocChecker] &&
1868       !ChecksEnabled[CK_NewDeleteChecker])
1869     return;
1870 
1871   Optional<MallocChecker::CheckKind> CheckKind = getCheckIfTracked(C, Sym);
1872   if (!CheckKind.hasValue())
1873     return;
1874 
1875   if (ExplodedNode *N = C.generateErrorNode()) {
1876     if (!BT_DoubleFree[*CheckKind])
1877       BT_DoubleFree[*CheckKind].reset(
1878           new BugType(CheckNames[*CheckKind], "Double free", "Memory Error"));
1879 
1880     auto R = llvm::make_unique<BugReport>(
1881         *BT_DoubleFree[*CheckKind],
1882         (Released ? "Attempt to free released memory"
1883                   : "Attempt to free non-owned memory"),
1884         N);
1885     R->addRange(Range);
1886     R->markInteresting(Sym);
1887     if (PrevSym)
1888       R->markInteresting(PrevSym);
1889     R->addVisitor(llvm::make_unique<MallocBugVisitor>(Sym));
1890     C.emitReport(std::move(R));
1891   }
1892 }
1893 
1894 void MallocChecker::ReportDoubleDelete(CheckerContext &C, SymbolRef Sym) const {
1895 
1896   if (!ChecksEnabled[CK_NewDeleteChecker])
1897     return;
1898 
1899   Optional<MallocChecker::CheckKind> CheckKind = getCheckIfTracked(C, Sym);
1900   if (!CheckKind.hasValue())
1901     return;
1902 
1903   if (ExplodedNode *N = C.generateErrorNode()) {
1904     if (!BT_DoubleDelete)
1905       BT_DoubleDelete.reset(new BugType(CheckNames[CK_NewDeleteChecker],
1906                                         "Double delete", "Memory Error"));
1907 
1908     auto R = llvm::make_unique<BugReport>(
1909         *BT_DoubleDelete, "Attempt to delete released memory", N);
1910 
1911     R->markInteresting(Sym);
1912     R->addVisitor(llvm::make_unique<MallocBugVisitor>(Sym));
1913     C.emitReport(std::move(R));
1914   }
1915 }
1916 
1917 void MallocChecker::ReportUseZeroAllocated(CheckerContext &C,
1918                                            SourceRange Range,
1919                                            SymbolRef Sym) const {
1920 
1921   if (!ChecksEnabled[CK_MallocChecker] &&
1922       !ChecksEnabled[CK_NewDeleteChecker])
1923     return;
1924 
1925   Optional<MallocChecker::CheckKind> CheckKind = getCheckIfTracked(C, Sym);
1926 
1927   if (!CheckKind.hasValue())
1928     return;
1929 
1930   if (ExplodedNode *N = C.generateErrorNode()) {
1931     if (!BT_UseZerroAllocated[*CheckKind])
1932       BT_UseZerroAllocated[*CheckKind].reset(new BugType(
1933           CheckNames[*CheckKind], "Use of zero allocated", "Memory Error"));
1934 
1935     auto R = llvm::make_unique<BugReport>(*BT_UseZerroAllocated[*CheckKind],
1936                                          "Use of zero-allocated memory", N);
1937 
1938     R->addRange(Range);
1939     if (Sym) {
1940       R->markInteresting(Sym);
1941       R->addVisitor(llvm::make_unique<MallocBugVisitor>(Sym));
1942     }
1943     C.emitReport(std::move(R));
1944   }
1945 }
1946 
1947 ProgramStateRef MallocChecker::ReallocMem(CheckerContext &C,
1948                                           const CallExpr *CE,
1949                                           bool FreesOnFail,
1950                                           ProgramStateRef State) const {
1951   if (!State)
1952     return nullptr;
1953 
1954   if (CE->getNumArgs() < 2)
1955     return nullptr;
1956 
1957   const Expr *arg0Expr = CE->getArg(0);
1958   const LocationContext *LCtx = C.getLocationContext();
1959   SVal Arg0Val = State->getSVal(arg0Expr, LCtx);
1960   if (!Arg0Val.getAs<DefinedOrUnknownSVal>())
1961     return nullptr;
1962   DefinedOrUnknownSVal arg0Val = Arg0Val.castAs<DefinedOrUnknownSVal>();
1963 
1964   SValBuilder &svalBuilder = C.getSValBuilder();
1965 
1966   DefinedOrUnknownSVal PtrEQ =
1967     svalBuilder.evalEQ(State, arg0Val, svalBuilder.makeNull());
1968 
1969   // Get the size argument. If there is no size arg then give up.
1970   const Expr *Arg1 = CE->getArg(1);
1971   if (!Arg1)
1972     return nullptr;
1973 
1974   // Get the value of the size argument.
1975   SVal Arg1ValG = State->getSVal(Arg1, LCtx);
1976   if (!Arg1ValG.getAs<DefinedOrUnknownSVal>())
1977     return nullptr;
1978   DefinedOrUnknownSVal Arg1Val = Arg1ValG.castAs<DefinedOrUnknownSVal>();
1979 
1980   // Compare the size argument to 0.
1981   DefinedOrUnknownSVal SizeZero =
1982     svalBuilder.evalEQ(State, Arg1Val,
1983                        svalBuilder.makeIntValWithPtrWidth(0, false));
1984 
1985   ProgramStateRef StatePtrIsNull, StatePtrNotNull;
1986   std::tie(StatePtrIsNull, StatePtrNotNull) = State->assume(PtrEQ);
1987   ProgramStateRef StateSizeIsZero, StateSizeNotZero;
1988   std::tie(StateSizeIsZero, StateSizeNotZero) = State->assume(SizeZero);
1989   // We only assume exceptional states if they are definitely true; if the
1990   // state is under-constrained, assume regular realloc behavior.
1991   bool PrtIsNull = StatePtrIsNull && !StatePtrNotNull;
1992   bool SizeIsZero = StateSizeIsZero && !StateSizeNotZero;
1993 
1994   // If the ptr is NULL and the size is not 0, the call is equivalent to
1995   // malloc(size).
1996   if ( PrtIsNull && !SizeIsZero) {
1997     ProgramStateRef stateMalloc = MallocMemAux(C, CE, CE->getArg(1),
1998                                                UndefinedVal(), StatePtrIsNull);
1999     return stateMalloc;
2000   }
2001 
2002   if (PrtIsNull && SizeIsZero)
2003     return State;
2004 
2005   // Get the from and to pointer symbols as in toPtr = realloc(fromPtr, size).
2006   assert(!PrtIsNull);
2007   SymbolRef FromPtr = arg0Val.getAsSymbol();
2008   SVal RetVal = State->getSVal(CE, LCtx);
2009   SymbolRef ToPtr = RetVal.getAsSymbol();
2010   if (!FromPtr || !ToPtr)
2011     return nullptr;
2012 
2013   bool ReleasedAllocated = false;
2014 
2015   // If the size is 0, free the memory.
2016   if (SizeIsZero)
2017     if (ProgramStateRef stateFree = FreeMemAux(C, CE, StateSizeIsZero, 0,
2018                                                false, ReleasedAllocated)){
2019       // The semantics of the return value are:
2020       // If size was equal to 0, either NULL or a pointer suitable to be passed
2021       // to free() is returned. We just free the input pointer and do not add
2022       // any constrains on the output pointer.
2023       return stateFree;
2024     }
2025 
2026   // Default behavior.
2027   if (ProgramStateRef stateFree =
2028         FreeMemAux(C, CE, State, 0, false, ReleasedAllocated)) {
2029 
2030     ProgramStateRef stateRealloc = MallocMemAux(C, CE, CE->getArg(1),
2031                                                 UnknownVal(), stateFree);
2032     if (!stateRealloc)
2033       return nullptr;
2034 
2035     ReallocPairKind Kind = RPToBeFreedAfterFailure;
2036     if (FreesOnFail)
2037       Kind = RPIsFreeOnFailure;
2038     else if (!ReleasedAllocated)
2039       Kind = RPDoNotTrackAfterFailure;
2040 
2041     // Record the info about the reallocated symbol so that we could properly
2042     // process failed reallocation.
2043     stateRealloc = stateRealloc->set<ReallocPairs>(ToPtr,
2044                                                    ReallocPair(FromPtr, Kind));
2045     // The reallocated symbol should stay alive for as long as the new symbol.
2046     C.getSymbolManager().addSymbolDependency(ToPtr, FromPtr);
2047     return stateRealloc;
2048   }
2049   return nullptr;
2050 }
2051 
2052 ProgramStateRef MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE,
2053                                          ProgramStateRef State) {
2054   if (!State)
2055     return nullptr;
2056 
2057   if (CE->getNumArgs() < 2)
2058     return nullptr;
2059 
2060   SValBuilder &svalBuilder = C.getSValBuilder();
2061   const LocationContext *LCtx = C.getLocationContext();
2062   SVal count = State->getSVal(CE->getArg(0), LCtx);
2063   SVal elementSize = State->getSVal(CE->getArg(1), LCtx);
2064   SVal TotalSize = svalBuilder.evalBinOp(State, BO_Mul, count, elementSize,
2065                                         svalBuilder.getContext().getSizeType());
2066   SVal zeroVal = svalBuilder.makeZeroVal(svalBuilder.getContext().CharTy);
2067 
2068   return MallocMemAux(C, CE, TotalSize, zeroVal, State);
2069 }
2070 
2071 LeakInfo
2072 MallocChecker::getAllocationSite(const ExplodedNode *N, SymbolRef Sym,
2073                                  CheckerContext &C) const {
2074   const LocationContext *LeakContext = N->getLocationContext();
2075   // Walk the ExplodedGraph backwards and find the first node that referred to
2076   // the tracked symbol.
2077   const ExplodedNode *AllocNode = N;
2078   const MemRegion *ReferenceRegion = nullptr;
2079 
2080   while (N) {
2081     ProgramStateRef State = N->getState();
2082     if (!State->get<RegionState>(Sym))
2083       break;
2084 
2085     // Find the most recent expression bound to the symbol in the current
2086     // context.
2087       if (!ReferenceRegion) {
2088         if (const MemRegion *MR = C.getLocationRegionIfPostStore(N)) {
2089           SVal Val = State->getSVal(MR);
2090           if (Val.getAsLocSymbol() == Sym) {
2091             const VarRegion* VR = MR->getBaseRegion()->getAs<VarRegion>();
2092             // Do not show local variables belonging to a function other than
2093             // where the error is reported.
2094             if (!VR ||
2095                 (VR->getStackFrame() == LeakContext->getCurrentStackFrame()))
2096               ReferenceRegion = MR;
2097           }
2098         }
2099       }
2100 
2101     // Allocation node, is the last node in the current or parent context in
2102     // which the symbol was tracked.
2103     const LocationContext *NContext = N->getLocationContext();
2104     if (NContext == LeakContext ||
2105         NContext->isParentOf(LeakContext))
2106       AllocNode = N;
2107     N = N->pred_empty() ? nullptr : *(N->pred_begin());
2108   }
2109 
2110   return LeakInfo(AllocNode, ReferenceRegion);
2111 }
2112 
2113 void MallocChecker::reportLeak(SymbolRef Sym, ExplodedNode *N,
2114                                CheckerContext &C) const {
2115 
2116   if (!ChecksEnabled[CK_MallocChecker] &&
2117       !ChecksEnabled[CK_NewDeleteLeaksChecker])
2118     return;
2119 
2120   const RefState *RS = C.getState()->get<RegionState>(Sym);
2121   assert(RS && "cannot leak an untracked symbol");
2122   AllocationFamily Family = RS->getAllocationFamily();
2123 
2124   if (Family == AF_Alloca)
2125     return;
2126 
2127   Optional<MallocChecker::CheckKind>
2128       CheckKind = getCheckIfTracked(Family, true);
2129 
2130   if (!CheckKind.hasValue())
2131     return;
2132 
2133   assert(N);
2134   if (!BT_Leak[*CheckKind]) {
2135     BT_Leak[*CheckKind].reset(
2136         new BugType(CheckNames[*CheckKind], "Memory leak", "Memory Error"));
2137     // Leaks should not be reported if they are post-dominated by a sink:
2138     // (1) Sinks are higher importance bugs.
2139     // (2) NoReturnFunctionChecker uses sink nodes to represent paths ending
2140     //     with __noreturn functions such as assert() or exit(). We choose not
2141     //     to report leaks on such paths.
2142     BT_Leak[*CheckKind]->setSuppressOnSink(true);
2143   }
2144 
2145   // Most bug reports are cached at the location where they occurred.
2146   // With leaks, we want to unique them by the location where they were
2147   // allocated, and only report a single path.
2148   PathDiagnosticLocation LocUsedForUniqueing;
2149   const ExplodedNode *AllocNode = nullptr;
2150   const MemRegion *Region = nullptr;
2151   std::tie(AllocNode, Region) = getAllocationSite(N, Sym, C);
2152 
2153   const Stmt *AllocationStmt = PathDiagnosticLocation::getStmt(AllocNode);
2154   if (AllocationStmt)
2155     LocUsedForUniqueing = PathDiagnosticLocation::createBegin(AllocationStmt,
2156                                               C.getSourceManager(),
2157                                               AllocNode->getLocationContext());
2158 
2159   SmallString<200> buf;
2160   llvm::raw_svector_ostream os(buf);
2161   if (Region && Region->canPrintPretty()) {
2162     os << "Potential leak of memory pointed to by ";
2163     Region->printPretty(os);
2164   } else {
2165     os << "Potential memory leak";
2166   }
2167 
2168   auto R = llvm::make_unique<BugReport>(
2169       *BT_Leak[*CheckKind], os.str(), N, LocUsedForUniqueing,
2170       AllocNode->getLocationContext()->getDecl());
2171   R->markInteresting(Sym);
2172   R->addVisitor(llvm::make_unique<MallocBugVisitor>(Sym, true));
2173   C.emitReport(std::move(R));
2174 }
2175 
2176 void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper,
2177                                      CheckerContext &C) const
2178 {
2179   if (!SymReaper.hasDeadSymbols())
2180     return;
2181 
2182   ProgramStateRef state = C.getState();
2183   RegionStateTy RS = state->get<RegionState>();
2184   RegionStateTy::Factory &F = state->get_context<RegionState>();
2185 
2186   SmallVector<SymbolRef, 2> Errors;
2187   for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
2188     if (SymReaper.isDead(I->first)) {
2189       if (I->second.isAllocated() || I->second.isAllocatedOfSizeZero())
2190         Errors.push_back(I->first);
2191       // Remove the dead symbol from the map.
2192       RS = F.remove(RS, I->first);
2193 
2194     }
2195   }
2196 
2197   // Cleanup the Realloc Pairs Map.
2198   ReallocPairsTy RP = state->get<ReallocPairs>();
2199   for (ReallocPairsTy::iterator I = RP.begin(), E = RP.end(); I != E; ++I) {
2200     if (SymReaper.isDead(I->first) ||
2201         SymReaper.isDead(I->second.ReallocatedSym)) {
2202       state = state->remove<ReallocPairs>(I->first);
2203     }
2204   }
2205 
2206   // Cleanup the FreeReturnValue Map.
2207   FreeReturnValueTy FR = state->get<FreeReturnValue>();
2208   for (FreeReturnValueTy::iterator I = FR.begin(), E = FR.end(); I != E; ++I) {
2209     if (SymReaper.isDead(I->first) ||
2210         SymReaper.isDead(I->second)) {
2211       state = state->remove<FreeReturnValue>(I->first);
2212     }
2213   }
2214 
2215   // Generate leak node.
2216   ExplodedNode *N = C.getPredecessor();
2217   if (!Errors.empty()) {
2218     static CheckerProgramPointTag Tag("MallocChecker", "DeadSymbolsLeak");
2219     N = C.generateNonFatalErrorNode(C.getState(), &Tag);
2220     if (N) {
2221       for (SmallVectorImpl<SymbolRef>::iterator
2222            I = Errors.begin(), E = Errors.end(); I != E; ++I) {
2223         reportLeak(*I, N, C);
2224       }
2225     }
2226   }
2227 
2228   C.addTransition(state->set<RegionState>(RS), N);
2229 }
2230 
2231 void MallocChecker::checkPreCall(const CallEvent &Call,
2232                                  CheckerContext &C) const {
2233 
2234   if (const CXXDestructorCall *DC = dyn_cast<CXXDestructorCall>(&Call)) {
2235     SymbolRef Sym = DC->getCXXThisVal().getAsSymbol();
2236     if (!Sym || checkDoubleDelete(Sym, C))
2237       return;
2238   }
2239 
2240   // We will check for double free in the post visit.
2241   if (const AnyFunctionCall *FC = dyn_cast<AnyFunctionCall>(&Call)) {
2242     const FunctionDecl *FD = FC->getDecl();
2243     if (!FD)
2244       return;
2245 
2246     ASTContext &Ctx = C.getASTContext();
2247     if (ChecksEnabled[CK_MallocChecker] &&
2248         (isCMemFunction(FD, Ctx, AF_Malloc, MemoryOperationKind::MOK_Free) ||
2249          isCMemFunction(FD, Ctx, AF_IfNameIndex,
2250                         MemoryOperationKind::MOK_Free)))
2251       return;
2252 
2253     if (ChecksEnabled[CK_NewDeleteChecker] &&
2254         isStandardNewDelete(FD, Ctx))
2255       return;
2256   }
2257 
2258   // Check if the callee of a method is deleted.
2259   if (const CXXInstanceCall *CC = dyn_cast<CXXInstanceCall>(&Call)) {
2260     SymbolRef Sym = CC->getCXXThisVal().getAsSymbol();
2261     if (!Sym || checkUseAfterFree(Sym, C, CC->getCXXThisExpr()))
2262       return;
2263   }
2264 
2265   // Check arguments for being used after free.
2266   for (unsigned I = 0, E = Call.getNumArgs(); I != E; ++I) {
2267     SVal ArgSVal = Call.getArgSVal(I);
2268     if (ArgSVal.getAs<Loc>()) {
2269       SymbolRef Sym = ArgSVal.getAsSymbol();
2270       if (!Sym)
2271         continue;
2272       if (checkUseAfterFree(Sym, C, Call.getArgExpr(I)))
2273         return;
2274     }
2275   }
2276 }
2277 
2278 void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
2279   const Expr *E = S->getRetValue();
2280   if (!E)
2281     return;
2282 
2283   // Check if we are returning a symbol.
2284   ProgramStateRef State = C.getState();
2285   SVal RetVal = State->getSVal(E, C.getLocationContext());
2286   SymbolRef Sym = RetVal.getAsSymbol();
2287   if (!Sym)
2288     // If we are returning a field of the allocated struct or an array element,
2289     // the callee could still free the memory.
2290     // TODO: This logic should be a part of generic symbol escape callback.
2291     if (const MemRegion *MR = RetVal.getAsRegion())
2292       if (isa<FieldRegion>(MR) || isa<ElementRegion>(MR))
2293         if (const SymbolicRegion *BMR =
2294               dyn_cast<SymbolicRegion>(MR->getBaseRegion()))
2295           Sym = BMR->getSymbol();
2296 
2297   // Check if we are returning freed memory.
2298   if (Sym)
2299     checkUseAfterFree(Sym, C, E);
2300 }
2301 
2302 // TODO: Blocks should be either inlined or should call invalidate regions
2303 // upon invocation. After that's in place, special casing here will not be
2304 // needed.
2305 void MallocChecker::checkPostStmt(const BlockExpr *BE,
2306                                   CheckerContext &C) const {
2307 
2308   // Scan the BlockDecRefExprs for any object the retain count checker
2309   // may be tracking.
2310   if (!BE->getBlockDecl()->hasCaptures())
2311     return;
2312 
2313   ProgramStateRef state = C.getState();
2314   const BlockDataRegion *R =
2315     cast<BlockDataRegion>(state->getSVal(BE,
2316                                          C.getLocationContext()).getAsRegion());
2317 
2318   BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
2319                                             E = R->referenced_vars_end();
2320 
2321   if (I == E)
2322     return;
2323 
2324   SmallVector<const MemRegion*, 10> Regions;
2325   const LocationContext *LC = C.getLocationContext();
2326   MemRegionManager &MemMgr = C.getSValBuilder().getRegionManager();
2327 
2328   for ( ; I != E; ++I) {
2329     const VarRegion *VR = I.getCapturedRegion();
2330     if (VR->getSuperRegion() == R) {
2331       VR = MemMgr.getVarRegion(VR->getDecl(), LC);
2332     }
2333     Regions.push_back(VR);
2334   }
2335 
2336   state =
2337     state->scanReachableSymbols<StopTrackingCallback>(Regions.data(),
2338                                     Regions.data() + Regions.size()).getState();
2339   C.addTransition(state);
2340 }
2341 
2342 bool MallocChecker::isReleased(SymbolRef Sym, CheckerContext &C) const {
2343   assert(Sym);
2344   const RefState *RS = C.getState()->get<RegionState>(Sym);
2345   return (RS && RS->isReleased());
2346 }
2347 
2348 bool MallocChecker::checkUseAfterFree(SymbolRef Sym, CheckerContext &C,
2349                                       const Stmt *S) const {
2350 
2351   if (isReleased(Sym, C)) {
2352     ReportUseAfterFree(C, S->getSourceRange(), Sym);
2353     return true;
2354   }
2355 
2356   return false;
2357 }
2358 
2359 void MallocChecker::checkUseZeroAllocated(SymbolRef Sym, CheckerContext &C,
2360                                           const Stmt *S) const {
2361   assert(Sym);
2362 
2363   if (const RefState *RS = C.getState()->get<RegionState>(Sym)) {
2364     if (RS->isAllocatedOfSizeZero())
2365       ReportUseZeroAllocated(C, RS->getStmt()->getSourceRange(), Sym);
2366   }
2367   else if (C.getState()->contains<ReallocSizeZeroSymbols>(Sym)) {
2368     ReportUseZeroAllocated(C, S->getSourceRange(), Sym);
2369   }
2370 }
2371 
2372 bool MallocChecker::checkDoubleDelete(SymbolRef Sym, CheckerContext &C) const {
2373 
2374   if (isReleased(Sym, C)) {
2375     ReportDoubleDelete(C, Sym);
2376     return true;
2377   }
2378   return false;
2379 }
2380 
2381 // Check if the location is a freed symbolic region.
2382 void MallocChecker::checkLocation(SVal l, bool isLoad, const Stmt *S,
2383                                   CheckerContext &C) const {
2384   SymbolRef Sym = l.getLocSymbolInBase();
2385   if (Sym) {
2386     checkUseAfterFree(Sym, C, S);
2387     checkUseZeroAllocated(Sym, C, S);
2388   }
2389 }
2390 
2391 // If a symbolic region is assumed to NULL (or another constant), stop tracking
2392 // it - assuming that allocation failed on this path.
2393 ProgramStateRef MallocChecker::evalAssume(ProgramStateRef state,
2394                                               SVal Cond,
2395                                               bool Assumption) const {
2396   RegionStateTy RS = state->get<RegionState>();
2397   for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
2398     // If the symbol is assumed to be NULL, remove it from consideration.
2399     ConstraintManager &CMgr = state->getConstraintManager();
2400     ConditionTruthVal AllocFailed = CMgr.isNull(state, I.getKey());
2401     if (AllocFailed.isConstrainedTrue())
2402       state = state->remove<RegionState>(I.getKey());
2403   }
2404 
2405   // Realloc returns 0 when reallocation fails, which means that we should
2406   // restore the state of the pointer being reallocated.
2407   ReallocPairsTy RP = state->get<ReallocPairs>();
2408   for (ReallocPairsTy::iterator I = RP.begin(), E = RP.end(); I != E; ++I) {
2409     // If the symbol is assumed to be NULL, remove it from consideration.
2410     ConstraintManager &CMgr = state->getConstraintManager();
2411     ConditionTruthVal AllocFailed = CMgr.isNull(state, I.getKey());
2412     if (!AllocFailed.isConstrainedTrue())
2413       continue;
2414 
2415     SymbolRef ReallocSym = I.getData().ReallocatedSym;
2416     if (const RefState *RS = state->get<RegionState>(ReallocSym)) {
2417       if (RS->isReleased()) {
2418         if (I.getData().Kind == RPToBeFreedAfterFailure)
2419           state = state->set<RegionState>(ReallocSym,
2420               RefState::getAllocated(RS->getAllocationFamily(), RS->getStmt()));
2421         else if (I.getData().Kind == RPDoNotTrackAfterFailure)
2422           state = state->remove<RegionState>(ReallocSym);
2423         else
2424           assert(I.getData().Kind == RPIsFreeOnFailure);
2425       }
2426     }
2427     state = state->remove<ReallocPairs>(I.getKey());
2428   }
2429 
2430   return state;
2431 }
2432 
2433 bool MallocChecker::mayFreeAnyEscapedMemoryOrIsModeledExplicitly(
2434                                               const CallEvent *Call,
2435                                               ProgramStateRef State,
2436                                               SymbolRef &EscapingSymbol) const {
2437   assert(Call);
2438   EscapingSymbol = nullptr;
2439 
2440   // For now, assume that any C++ or block call can free memory.
2441   // TODO: If we want to be more optimistic here, we'll need to make sure that
2442   // regions escape to C++ containers. They seem to do that even now, but for
2443   // mysterious reasons.
2444   if (!(isa<SimpleFunctionCall>(Call) || isa<ObjCMethodCall>(Call)))
2445     return true;
2446 
2447   // Check Objective-C messages by selector name.
2448   if (const ObjCMethodCall *Msg = dyn_cast<ObjCMethodCall>(Call)) {
2449     // If it's not a framework call, or if it takes a callback, assume it
2450     // can free memory.
2451     if (!Call->isInSystemHeader() || Call->argumentsMayEscape())
2452       return true;
2453 
2454     // If it's a method we know about, handle it explicitly post-call.
2455     // This should happen before the "freeWhenDone" check below.
2456     if (isKnownDeallocObjCMethodName(*Msg))
2457       return false;
2458 
2459     // If there's a "freeWhenDone" parameter, but the method isn't one we know
2460     // about, we can't be sure that the object will use free() to deallocate the
2461     // memory, so we can't model it explicitly. The best we can do is use it to
2462     // decide whether the pointer escapes.
2463     if (Optional<bool> FreeWhenDone = getFreeWhenDoneArg(*Msg))
2464       return *FreeWhenDone;
2465 
2466     // If the first selector piece ends with "NoCopy", and there is no
2467     // "freeWhenDone" parameter set to zero, we know ownership is being
2468     // transferred. Again, though, we can't be sure that the object will use
2469     // free() to deallocate the memory, so we can't model it explicitly.
2470     StringRef FirstSlot = Msg->getSelector().getNameForSlot(0);
2471     if (FirstSlot.endswith("NoCopy"))
2472       return true;
2473 
2474     // If the first selector starts with addPointer, insertPointer,
2475     // or replacePointer, assume we are dealing with NSPointerArray or similar.
2476     // This is similar to C++ containers (vector); we still might want to check
2477     // that the pointers get freed by following the container itself.
2478     if (FirstSlot.startswith("addPointer") ||
2479         FirstSlot.startswith("insertPointer") ||
2480         FirstSlot.startswith("replacePointer") ||
2481         FirstSlot.equals("valueWithPointer")) {
2482       return true;
2483     }
2484 
2485     // We should escape receiver on call to 'init'. This is especially relevant
2486     // to the receiver, as the corresponding symbol is usually not referenced
2487     // after the call.
2488     if (Msg->getMethodFamily() == OMF_init) {
2489       EscapingSymbol = Msg->getReceiverSVal().getAsSymbol();
2490       return true;
2491     }
2492 
2493     // Otherwise, assume that the method does not free memory.
2494     // Most framework methods do not free memory.
2495     return false;
2496   }
2497 
2498   // At this point the only thing left to handle is straight function calls.
2499   const FunctionDecl *FD = cast<SimpleFunctionCall>(Call)->getDecl();
2500   if (!FD)
2501     return true;
2502 
2503   ASTContext &ASTC = State->getStateManager().getContext();
2504 
2505   // If it's one of the allocation functions we can reason about, we model
2506   // its behavior explicitly.
2507   if (isMemFunction(FD, ASTC))
2508     return false;
2509 
2510   // If it's not a system call, assume it frees memory.
2511   if (!Call->isInSystemHeader())
2512     return true;
2513 
2514   // White list the system functions whose arguments escape.
2515   const IdentifierInfo *II = FD->getIdentifier();
2516   if (!II)
2517     return true;
2518   StringRef FName = II->getName();
2519 
2520   // White list the 'XXXNoCopy' CoreFoundation functions.
2521   // We specifically check these before
2522   if (FName.endswith("NoCopy")) {
2523     // Look for the deallocator argument. We know that the memory ownership
2524     // is not transferred only if the deallocator argument is
2525     // 'kCFAllocatorNull'.
2526     for (unsigned i = 1; i < Call->getNumArgs(); ++i) {
2527       const Expr *ArgE = Call->getArgExpr(i)->IgnoreParenCasts();
2528       if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(ArgE)) {
2529         StringRef DeallocatorName = DE->getFoundDecl()->getName();
2530         if (DeallocatorName == "kCFAllocatorNull")
2531           return false;
2532       }
2533     }
2534     return true;
2535   }
2536 
2537   // Associating streams with malloced buffers. The pointer can escape if
2538   // 'closefn' is specified (and if that function does free memory),
2539   // but it will not if closefn is not specified.
2540   // Currently, we do not inspect the 'closefn' function (PR12101).
2541   if (FName == "funopen")
2542     if (Call->getNumArgs() >= 4 && Call->getArgSVal(4).isConstant(0))
2543       return false;
2544 
2545   // Do not warn on pointers passed to 'setbuf' when used with std streams,
2546   // these leaks might be intentional when setting the buffer for stdio.
2547   // http://stackoverflow.com/questions/2671151/who-frees-setvbuf-buffer
2548   if (FName == "setbuf" || FName =="setbuffer" ||
2549       FName == "setlinebuf" || FName == "setvbuf") {
2550     if (Call->getNumArgs() >= 1) {
2551       const Expr *ArgE = Call->getArgExpr(0)->IgnoreParenCasts();
2552       if (const DeclRefExpr *ArgDRE = dyn_cast<DeclRefExpr>(ArgE))
2553         if (const VarDecl *D = dyn_cast<VarDecl>(ArgDRE->getDecl()))
2554           if (D->getCanonicalDecl()->getName().find("std") != StringRef::npos)
2555             return true;
2556     }
2557   }
2558 
2559   // A bunch of other functions which either take ownership of a pointer or
2560   // wrap the result up in a struct or object, meaning it can be freed later.
2561   // (See RetainCountChecker.) Not all the parameters here are invalidated,
2562   // but the Malloc checker cannot differentiate between them. The right way
2563   // of doing this would be to implement a pointer escapes callback.
2564   if (FName == "CGBitmapContextCreate" ||
2565       FName == "CGBitmapContextCreateWithData" ||
2566       FName == "CVPixelBufferCreateWithBytes" ||
2567       FName == "CVPixelBufferCreateWithPlanarBytes" ||
2568       FName == "OSAtomicEnqueue") {
2569     return true;
2570   }
2571 
2572   if (FName == "postEvent" &&
2573       FD->getQualifiedNameAsString() == "QCoreApplication::postEvent") {
2574     return true;
2575   }
2576 
2577   if (FName == "postEvent" &&
2578       FD->getQualifiedNameAsString() == "QCoreApplication::postEvent") {
2579     return true;
2580   }
2581 
2582   // Handle cases where we know a buffer's /address/ can escape.
2583   // Note that the above checks handle some special cases where we know that
2584   // even though the address escapes, it's still our responsibility to free the
2585   // buffer.
2586   if (Call->argumentsMayEscape())
2587     return true;
2588 
2589   // Otherwise, assume that the function does not free memory.
2590   // Most system calls do not free the memory.
2591   return false;
2592 }
2593 
2594 static bool retTrue(const RefState *RS) {
2595   return true;
2596 }
2597 
2598 static bool checkIfNewOrNewArrayFamily(const RefState *RS) {
2599   return (RS->getAllocationFamily() == AF_CXXNewArray ||
2600           RS->getAllocationFamily() == AF_CXXNew);
2601 }
2602 
2603 ProgramStateRef MallocChecker::checkPointerEscape(ProgramStateRef State,
2604                                              const InvalidatedSymbols &Escaped,
2605                                              const CallEvent *Call,
2606                                              PointerEscapeKind Kind) const {
2607   return checkPointerEscapeAux(State, Escaped, Call, Kind, &retTrue);
2608 }
2609 
2610 ProgramStateRef MallocChecker::checkConstPointerEscape(ProgramStateRef State,
2611                                               const InvalidatedSymbols &Escaped,
2612                                               const CallEvent *Call,
2613                                               PointerEscapeKind Kind) const {
2614   return checkPointerEscapeAux(State, Escaped, Call, Kind,
2615                                &checkIfNewOrNewArrayFamily);
2616 }
2617 
2618 ProgramStateRef MallocChecker::checkPointerEscapeAux(ProgramStateRef State,
2619                                               const InvalidatedSymbols &Escaped,
2620                                               const CallEvent *Call,
2621                                               PointerEscapeKind Kind,
2622                                   bool(*CheckRefState)(const RefState*)) const {
2623   // If we know that the call does not free memory, or we want to process the
2624   // call later, keep tracking the top level arguments.
2625   SymbolRef EscapingSymbol = nullptr;
2626   if (Kind == PSK_DirectEscapeOnCall &&
2627       !mayFreeAnyEscapedMemoryOrIsModeledExplicitly(Call, State,
2628                                                     EscapingSymbol) &&
2629       !EscapingSymbol) {
2630     return State;
2631   }
2632 
2633   for (InvalidatedSymbols::const_iterator I = Escaped.begin(),
2634        E = Escaped.end();
2635        I != E; ++I) {
2636     SymbolRef sym = *I;
2637 
2638     if (EscapingSymbol && EscapingSymbol != sym)
2639       continue;
2640 
2641     if (const RefState *RS = State->get<RegionState>(sym)) {
2642       if ((RS->isAllocated() || RS->isAllocatedOfSizeZero()) &&
2643           CheckRefState(RS)) {
2644         State = State->remove<RegionState>(sym);
2645         State = State->set<RegionState>(sym, RefState::getEscaped(RS));
2646       }
2647     }
2648   }
2649   return State;
2650 }
2651 
2652 static SymbolRef findFailedReallocSymbol(ProgramStateRef currState,
2653                                          ProgramStateRef prevState) {
2654   ReallocPairsTy currMap = currState->get<ReallocPairs>();
2655   ReallocPairsTy prevMap = prevState->get<ReallocPairs>();
2656 
2657   for (ReallocPairsTy::iterator I = prevMap.begin(), E = prevMap.end();
2658        I != E; ++I) {
2659     SymbolRef sym = I.getKey();
2660     if (!currMap.lookup(sym))
2661       return sym;
2662   }
2663 
2664   return nullptr;
2665 }
2666 
2667 PathDiagnosticPiece *
2668 MallocChecker::MallocBugVisitor::VisitNode(const ExplodedNode *N,
2669                                            const ExplodedNode *PrevN,
2670                                            BugReporterContext &BRC,
2671                                            BugReport &BR) {
2672   ProgramStateRef state = N->getState();
2673   ProgramStateRef statePrev = PrevN->getState();
2674 
2675   const RefState *RS = state->get<RegionState>(Sym);
2676   const RefState *RSPrev = statePrev->get<RegionState>(Sym);
2677   if (!RS)
2678     return nullptr;
2679 
2680   const Stmt *S = PathDiagnosticLocation::getStmt(N);
2681   if (!S)
2682     return nullptr;
2683 
2684   // FIXME: We will eventually need to handle non-statement-based events
2685   // (__attribute__((cleanup))).
2686 
2687   // Find out if this is an interesting point and what is the kind.
2688   const char *Msg = nullptr;
2689   StackHintGeneratorForSymbol *StackHint = nullptr;
2690   if (Mode == Normal) {
2691     if (isAllocated(RS, RSPrev, S)) {
2692       Msg = "Memory is allocated";
2693       StackHint = new StackHintGeneratorForSymbol(Sym,
2694                                                   "Returned allocated memory");
2695     } else if (isReleased(RS, RSPrev, S)) {
2696       Msg = "Memory is released";
2697       StackHint = new StackHintGeneratorForSymbol(Sym,
2698                                              "Returning; memory was released");
2699     } else if (isRelinquished(RS, RSPrev, S)) {
2700       Msg = "Memory ownership is transferred";
2701       StackHint = new StackHintGeneratorForSymbol(Sym, "");
2702     } else if (isReallocFailedCheck(RS, RSPrev, S)) {
2703       Mode = ReallocationFailed;
2704       Msg = "Reallocation failed";
2705       StackHint = new StackHintGeneratorForReallocationFailed(Sym,
2706                                                        "Reallocation failed");
2707 
2708       if (SymbolRef sym = findFailedReallocSymbol(state, statePrev)) {
2709         // Is it possible to fail two reallocs WITHOUT testing in between?
2710         assert((!FailedReallocSymbol || FailedReallocSymbol == sym) &&
2711           "We only support one failed realloc at a time.");
2712         BR.markInteresting(sym);
2713         FailedReallocSymbol = sym;
2714       }
2715     }
2716 
2717   // We are in a special mode if a reallocation failed later in the path.
2718   } else if (Mode == ReallocationFailed) {
2719     assert(FailedReallocSymbol && "No symbol to look for.");
2720 
2721     // Is this is the first appearance of the reallocated symbol?
2722     if (!statePrev->get<RegionState>(FailedReallocSymbol)) {
2723       // We're at the reallocation point.
2724       Msg = "Attempt to reallocate memory";
2725       StackHint = new StackHintGeneratorForSymbol(Sym,
2726                                                  "Returned reallocated memory");
2727       FailedReallocSymbol = nullptr;
2728       Mode = Normal;
2729     }
2730   }
2731 
2732   if (!Msg)
2733     return nullptr;
2734   assert(StackHint);
2735 
2736   // Generate the extra diagnostic.
2737   PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
2738                              N->getLocationContext());
2739   return new PathDiagnosticEventPiece(Pos, Msg, true, StackHint);
2740 }
2741 
2742 void MallocChecker::printState(raw_ostream &Out, ProgramStateRef State,
2743                                const char *NL, const char *Sep) const {
2744 
2745   RegionStateTy RS = State->get<RegionState>();
2746 
2747   if (!RS.isEmpty()) {
2748     Out << Sep << "MallocChecker :" << NL;
2749     for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
2750       const RefState *RefS = State->get<RegionState>(I.getKey());
2751       AllocationFamily Family = RefS->getAllocationFamily();
2752       Optional<MallocChecker::CheckKind> CheckKind = getCheckIfTracked(Family);
2753       if (!CheckKind.hasValue())
2754          CheckKind = getCheckIfTracked(Family, true);
2755 
2756       I.getKey()->dumpToStream(Out);
2757       Out << " : ";
2758       I.getData().dump(Out);
2759       if (CheckKind.hasValue())
2760         Out << " (" << CheckNames[*CheckKind].getName() << ")";
2761       Out << NL;
2762     }
2763   }
2764 }
2765 
2766 void ento::registerNewDeleteLeaksChecker(CheckerManager &mgr) {
2767   registerCStringCheckerBasic(mgr);
2768   MallocChecker *checker = mgr.registerChecker<MallocChecker>();
2769   checker->IsOptimistic = mgr.getAnalyzerOptions().getBooleanOption(
2770       "Optimistic", false, checker);
2771   checker->ChecksEnabled[MallocChecker::CK_NewDeleteLeaksChecker] = true;
2772   checker->CheckNames[MallocChecker::CK_NewDeleteLeaksChecker] =
2773       mgr.getCurrentCheckName();
2774   // We currently treat NewDeleteLeaks checker as a subchecker of NewDelete
2775   // checker.
2776   if (!checker->ChecksEnabled[MallocChecker::CK_NewDeleteChecker])
2777     checker->ChecksEnabled[MallocChecker::CK_NewDeleteChecker] = true;
2778 }
2779 
2780 #define REGISTER_CHECKER(name)                                                 \
2781   void ento::register##name(CheckerManager &mgr) {                             \
2782     registerCStringCheckerBasic(mgr);                                          \
2783     MallocChecker *checker = mgr.registerChecker<MallocChecker>();             \
2784     checker->IsOptimistic = mgr.getAnalyzerOptions().getBooleanOption(         \
2785         "Optimistic", false, checker);                                         \
2786     checker->ChecksEnabled[MallocChecker::CK_##name] = true;                   \
2787     checker->CheckNames[MallocChecker::CK_##name] = mgr.getCurrentCheckName(); \
2788   }
2789 
2790 REGISTER_CHECKER(MallocChecker)
2791 REGISTER_CHECKER(NewDeleteChecker)
2792 REGISTER_CHECKER(MismatchedDeallocatorChecker)
2793