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