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