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