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