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