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