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