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