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