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