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