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