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