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 | |
Zhongxing Xu | 7fb1464 | 2009-12-11 00:55:44 +0000 | [diff] [blame] | 38 | class RefState { |
Anna Zaks | 050cdd7 | 2012-06-20 20:57:46 +0000 | [diff] [blame] | 39 | enum Kind { // Reference to allocated memory. |
| 40 | Allocated, |
| 41 | // Reference to released/freed memory. |
| 42 | Released, |
Anna Zaks | 050cdd7 | 2012-06-20 20:57:46 +0000 | [diff] [blame] | 43 | // The responsibility for freeing resources has transfered from |
| 44 | // this reference. A relinquished symbol should not be freed. |
Ted Kremenek | dde201b | 2010-08-06 21:12:55 +0000 | [diff] [blame] | 45 | Relinquished } K; |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 46 | const Stmt *S; |
| 47 | |
Zhongxing Xu | 7fb1464 | 2009-12-11 00:55:44 +0000 | [diff] [blame] | 48 | public: |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 49 | RefState(Kind k, const Stmt *s) : K(k), S(s) {} |
| 50 | |
Anna Zaks | 050cdd7 | 2012-06-20 20:57:46 +0000 | [diff] [blame] | 51 | bool isAllocated() const { return K == Allocated; } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 52 | bool isReleased() const { return K == Released; } |
Anna Zaks | 050cdd7 | 2012-06-20 20:57:46 +0000 | [diff] [blame] | 53 | bool isRelinquished() const { return K == Relinquished; } |
Anna Zaks | ca23eb2 | 2012-02-29 18:42:47 +0000 | [diff] [blame] | 54 | |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 55 | const Stmt *getStmt() const { return S; } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 56 | |
| 57 | bool operator==(const RefState &X) const { |
| 58 | return K == X.K && S == X.S; |
| 59 | } |
| 60 | |
Anna Zaks | 050cdd7 | 2012-06-20 20:57:46 +0000 | [diff] [blame] | 61 | static RefState getAllocated(const Stmt *s) { |
| 62 | return RefState(Allocated, s); |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 63 | } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 64 | static RefState getReleased(const Stmt *s) { return RefState(Released, s); } |
Ted Kremenek | dde201b | 2010-08-06 21:12:55 +0000 | [diff] [blame] | 65 | static RefState getRelinquished(const Stmt *s) { |
| 66 | return RefState(Relinquished, s); |
| 67 | } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 68 | |
| 69 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 70 | ID.AddInteger(K); |
| 71 | ID.AddPointer(S); |
| 72 | } |
Ted Kremenek | c37fad6 | 2013-01-03 01:30:12 +0000 | [diff] [blame] | 73 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 74 | void dump(raw_ostream &OS) const { |
Ted Kremenek | c37fad6 | 2013-01-03 01:30:12 +0000 | [diff] [blame] | 75 | static const char *Table[] = { |
| 76 | "Allocated", |
| 77 | "Released", |
| 78 | "Relinquished" |
| 79 | }; |
| 80 | OS << Table[(unsigned) K]; |
| 81 | } |
| 82 | |
| 83 | LLVM_ATTRIBUTE_USED void dump() const { |
| 84 | dump(llvm::errs()); |
| 85 | } |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Anna Zaks | 9dc298b | 2012-09-12 22:57:34 +0000 | [diff] [blame] | 88 | enum ReallocPairKind { |
| 89 | RPToBeFreedAfterFailure, |
| 90 | // The symbol has been freed when reallocation failed. |
| 91 | RPIsFreeOnFailure, |
| 92 | // The symbol does not need to be freed after reallocation fails. |
| 93 | RPDoNotTrackAfterFailure |
| 94 | }; |
| 95 | |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 96 | /// \class ReallocPair |
| 97 | /// \brief Stores information about the symbol being reallocated by a call to |
| 98 | /// 'realloc' to allow modeling failed reallocation later in the path. |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 99 | struct ReallocPair { |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 100 | // \brief The symbol which realloc reallocated. |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 101 | SymbolRef ReallocatedSym; |
Anna Zaks | 9dc298b | 2012-09-12 22:57:34 +0000 | [diff] [blame] | 102 | ReallocPairKind Kind; |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 103 | |
Anna Zaks | 9dc298b | 2012-09-12 22:57:34 +0000 | [diff] [blame] | 104 | ReallocPair(SymbolRef S, ReallocPairKind K) : |
| 105 | ReallocatedSym(S), Kind(K) {} |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 106 | void Profile(llvm::FoldingSetNodeID &ID) const { |
Anna Zaks | 9dc298b | 2012-09-12 22:57:34 +0000 | [diff] [blame] | 107 | ID.AddInteger(Kind); |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 108 | ID.AddPointer(ReallocatedSym); |
| 109 | } |
| 110 | bool operator==(const ReallocPair &X) const { |
| 111 | return ReallocatedSym == X.ReallocatedSym && |
Anna Zaks | 9dc298b | 2012-09-12 22:57:34 +0000 | [diff] [blame] | 112 | Kind == X.Kind; |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 113 | } |
| 114 | }; |
| 115 | |
Anna Zaks | 97bfb55 | 2013-01-08 00:25:29 +0000 | [diff] [blame] | 116 | typedef std::pair<const ExplodedNode*, const MemRegion*> LeakInfo; |
Anna Zaks | 3d7c44e | 2012-03-21 19:45:08 +0000 | [diff] [blame] | 117 | |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 118 | class MallocChecker : public Checker<check::DeadSymbols, |
Anna Zaks | bf53dfa | 2012-12-20 00:38:25 +0000 | [diff] [blame] | 119 | check::PointerEscape, |
Ted Kremenek | e3659a7 | 2012-01-04 23:48:37 +0000 | [diff] [blame] | 120 | check::PreStmt<ReturnStmt>, |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 121 | check::PreStmt<CallExpr>, |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 122 | check::PostStmt<CallExpr>, |
Anna Zaks | f5aa3f5 | 2012-03-22 00:57:20 +0000 | [diff] [blame] | 123 | check::PostStmt<BlockExpr>, |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 124 | check::PostObjCMessage, |
Ted Kremenek | e3659a7 | 2012-01-04 23:48:37 +0000 | [diff] [blame] | 125 | check::Location, |
Anna Zaks | bf53dfa | 2012-12-20 00:38:25 +0000 | [diff] [blame] | 126 | eval::Assume> |
Ted Kremenek | e3659a7 | 2012-01-04 23:48:37 +0000 | [diff] [blame] | 127 | { |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 128 | mutable OwningPtr<BugType> BT_DoubleFree; |
| 129 | mutable OwningPtr<BugType> BT_Leak; |
| 130 | mutable OwningPtr<BugType> BT_UseFree; |
| 131 | mutable OwningPtr<BugType> BT_BadFree; |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 132 | mutable OwningPtr<BugType> BT_OffsetFree; |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 133 | mutable IdentifierInfo *II_malloc, *II_free, *II_realloc, *II_calloc, |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 134 | *II_valloc, *II_reallocf, *II_strndup, *II_strdup; |
| 135 | |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 136 | public: |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 137 | 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] | 138 | II_valloc(0), II_reallocf(0), II_strndup(0), II_strdup(0) {} |
Anna Zaks | 231361a | 2012-02-08 23:16:52 +0000 | [diff] [blame] | 139 | |
| 140 | /// In pessimistic mode, the checker assumes that it does not know which |
| 141 | /// functions might free the memory. |
| 142 | struct ChecksFilter { |
| 143 | DefaultBool CMallocPessimistic; |
| 144 | DefaultBool CMallocOptimistic; |
| 145 | }; |
| 146 | |
| 147 | ChecksFilter Filter; |
| 148 | |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 149 | void checkPreStmt(const CallExpr *S, CheckerContext &C) const; |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 150 | void checkPostStmt(const CallExpr *CE, CheckerContext &C) const; |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 151 | void checkPostObjCMessage(const ObjCMethodCall &Call, CheckerContext &C) const; |
Anna Zaks | f5aa3f5 | 2012-03-22 00:57:20 +0000 | [diff] [blame] | 152 | void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const; |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 153 | void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const; |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 154 | void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 155 | ProgramStateRef evalAssume(ProgramStateRef state, SVal Cond, |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 156 | bool Assumption) const; |
Anna Zaks | 390909c | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 157 | void checkLocation(SVal l, bool isLoad, const Stmt *S, |
| 158 | CheckerContext &C) const; |
Anna Zaks | bf53dfa | 2012-12-20 00:38:25 +0000 | [diff] [blame] | 159 | |
| 160 | ProgramStateRef checkPointerEscape(ProgramStateRef State, |
| 161 | const InvalidatedSymbols &Escaped, |
Anna Zaks | 233e26a | 2013-02-07 23:05:43 +0000 | [diff] [blame] | 162 | const CallEvent *Call, |
| 163 | PointerEscapeKind Kind) const; |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 164 | |
Anna Zaks | 93c5a24 | 2012-05-02 00:05:20 +0000 | [diff] [blame] | 165 | void printState(raw_ostream &Out, ProgramStateRef State, |
| 166 | const char *NL, const char *Sep) const; |
| 167 | |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 168 | private: |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 169 | void initIdentifierInfo(ASTContext &C) const; |
| 170 | |
| 171 | /// Check if this is one of the functions which can allocate/reallocate memory |
| 172 | /// pointed to by one of its arguments. |
| 173 | bool isMemFunction(const FunctionDecl *FD, ASTContext &C) const; |
Anna Zaks | 1434518 | 2012-05-18 01:16:10 +0000 | [diff] [blame] | 174 | bool isFreeFunction(const FunctionDecl *FD, ASTContext &C) const; |
| 175 | bool isAllocationFunction(const FunctionDecl *FD, ASTContext &C) const; |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 176 | |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 177 | static ProgramStateRef MallocMemReturnsAttr(CheckerContext &C, |
| 178 | const CallExpr *CE, |
| 179 | const OwnershipAttr* Att); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 180 | static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE, |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 181 | const Expr *SizeEx, SVal Init, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 182 | ProgramStateRef state) { |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 183 | return MallocMemAux(C, CE, |
| 184 | state->getSVal(SizeEx, C.getLocationContext()), |
| 185 | Init, state); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 186 | } |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 188 | static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE, |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 189 | SVal SizeEx, SVal Init, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 190 | ProgramStateRef state); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 191 | |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 192 | /// Update the RefState to reflect the new memory allocation. |
| 193 | static ProgramStateRef MallocUpdateRefState(CheckerContext &C, |
| 194 | const CallExpr *CE, |
| 195 | ProgramStateRef state); |
| 196 | |
| 197 | ProgramStateRef FreeMemAttr(CheckerContext &C, const CallExpr *CE, |
| 198 | const OwnershipAttr* Att) const; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 199 | ProgramStateRef FreeMemAux(CheckerContext &C, const CallExpr *CE, |
Anna Zaks | 5b7aa34 | 2012-06-22 02:04:31 +0000 | [diff] [blame] | 200 | ProgramStateRef state, unsigned Num, |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 201 | bool Hold, |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 202 | bool &ReleasedAllocated, |
| 203 | bool ReturnsNullOnFailure = false) const; |
Anna Zaks | 5b7aa34 | 2012-06-22 02:04:31 +0000 | [diff] [blame] | 204 | ProgramStateRef FreeMemAux(CheckerContext &C, const Expr *Arg, |
| 205 | const Expr *ParentExpr, |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 206 | ProgramStateRef State, |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 207 | bool Hold, |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 208 | bool &ReleasedAllocated, |
| 209 | bool ReturnsNullOnFailure = false) const; |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 210 | |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 211 | ProgramStateRef ReallocMem(CheckerContext &C, const CallExpr *CE, |
| 212 | bool FreesMemOnFailure) const; |
| 213 | static ProgramStateRef CallocMem(CheckerContext &C, const CallExpr *CE); |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 214 | |
Anna Zaks | 1434518 | 2012-05-18 01:16:10 +0000 | [diff] [blame] | 215 | ///\brief Check if the memory associated with this symbol was released. |
| 216 | bool isReleased(SymbolRef Sym, CheckerContext &C) const; |
| 217 | |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 218 | bool checkUseAfterFree(SymbolRef Sym, CheckerContext &C, |
| 219 | const Stmt *S = 0) const; |
| 220 | |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 221 | /// Check if the function is not known to us. So, for example, we could |
| 222 | /// conservatively assume it can free/reallocate it's pointer arguments. |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 223 | bool doesNotFreeMemory(const CallEvent *Call, |
Anna Zaks | 3cd89ad | 2012-02-24 23:56:53 +0000 | [diff] [blame] | 224 | ProgramStateRef State) const; |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 225 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 226 | static bool SummarizeValue(raw_ostream &os, SVal V); |
| 227 | static bool SummarizeRegion(raw_ostream &os, const MemRegion *MR); |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 228 | void ReportBadFree(CheckerContext &C, SVal ArgVal, SourceRange range) const; |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 229 | void ReportOffsetFree(CheckerContext &C, SVal ArgVal, SourceRange Range)const; |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 230 | |
Anna Zaks | ca8e36e | 2012-02-23 21:38:21 +0000 | [diff] [blame] | 231 | /// Find the location of the allocation for Sym on the path leading to the |
| 232 | /// exploded node N. |
Anna Zaks | 3d7c44e | 2012-03-21 19:45:08 +0000 | [diff] [blame] | 233 | LeakInfo getAllocationSite(const ExplodedNode *N, SymbolRef Sym, |
| 234 | CheckerContext &C) const; |
Anna Zaks | ca8e36e | 2012-02-23 21:38:21 +0000 | [diff] [blame] | 235 | |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 236 | void reportLeak(SymbolRef Sym, ExplodedNode *N, CheckerContext &C) const; |
| 237 | |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 238 | /// The bug visitor which allows us to print extra diagnostics along the |
| 239 | /// BugReport path. For example, showing the allocation site of the leaked |
| 240 | /// region. |
Jordy Rose | 0115349 | 2012-03-24 02:45:35 +0000 | [diff] [blame] | 241 | class MallocBugVisitor : public BugReporterVisitorImpl<MallocBugVisitor> { |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 242 | protected: |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 243 | enum NotificationMode { |
| 244 | Normal, |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 245 | ReallocationFailed |
| 246 | }; |
| 247 | |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 248 | // The allocated region symbol tracked by the main analysis. |
| 249 | SymbolRef Sym; |
| 250 | |
Anna Zaks | 88feba0 | 2012-05-10 01:37:40 +0000 | [diff] [blame] | 251 | // The mode we are in, i.e. what kind of diagnostics will be emitted. |
| 252 | NotificationMode Mode; |
Jordy Rose | b000fb5 | 2012-03-24 03:15:09 +0000 | [diff] [blame] | 253 | |
Anna Zaks | 88feba0 | 2012-05-10 01:37:40 +0000 | [diff] [blame] | 254 | // A symbol from when the primary region should have been reallocated. |
| 255 | SymbolRef FailedReallocSymbol; |
Jordy Rose | b000fb5 | 2012-03-24 03:15:09 +0000 | [diff] [blame] | 256 | |
Anna Zaks | 88feba0 | 2012-05-10 01:37:40 +0000 | [diff] [blame] | 257 | bool IsLeak; |
| 258 | |
| 259 | public: |
| 260 | MallocBugVisitor(SymbolRef S, bool isLeak = false) |
| 261 | : Sym(S), Mode(Normal), FailedReallocSymbol(0), IsLeak(isLeak) {} |
Jordy Rose | b000fb5 | 2012-03-24 03:15:09 +0000 | [diff] [blame] | 262 | |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 263 | virtual ~MallocBugVisitor() {} |
| 264 | |
| 265 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 266 | static int X = 0; |
| 267 | ID.AddPointer(&X); |
| 268 | ID.AddPointer(Sym); |
| 269 | } |
| 270 | |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 271 | inline bool isAllocated(const RefState *S, const RefState *SPrev, |
| 272 | const Stmt *Stmt) { |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 273 | // Did not track -> allocated. Other state (released) -> allocated. |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 274 | return (Stmt && isa<CallExpr>(Stmt) && |
| 275 | (S && S->isAllocated()) && (!SPrev || !SPrev->isAllocated())); |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 278 | inline bool isReleased(const RefState *S, const RefState *SPrev, |
| 279 | const Stmt *Stmt) { |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 280 | // Did not track -> released. Other state (allocated) -> released. |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 281 | return (Stmt && isa<CallExpr>(Stmt) && |
| 282 | (S && S->isReleased()) && (!SPrev || !SPrev->isReleased())); |
| 283 | } |
| 284 | |
Anna Zaks | 5b7aa34 | 2012-06-22 02:04:31 +0000 | [diff] [blame] | 285 | inline bool isRelinquished(const RefState *S, const RefState *SPrev, |
| 286 | const Stmt *Stmt) { |
| 287 | // Did not track -> relinquished. Other state (allocated) -> relinquished. |
| 288 | return (Stmt && (isa<CallExpr>(Stmt) || isa<ObjCMessageExpr>(Stmt) || |
| 289 | isa<ObjCPropertyRefExpr>(Stmt)) && |
| 290 | (S && S->isRelinquished()) && |
| 291 | (!SPrev || !SPrev->isRelinquished())); |
| 292 | } |
| 293 | |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 294 | inline bool isReallocFailedCheck(const RefState *S, const RefState *SPrev, |
| 295 | const Stmt *Stmt) { |
| 296 | // If the expression is not a call, and the state change is |
| 297 | // released -> allocated, it must be the realloc return value |
| 298 | // check. If we have to handle more cases here, it might be cleaner just |
| 299 | // to track this extra bit in the state itself. |
| 300 | return ((!Stmt || !isa<CallExpr>(Stmt)) && |
| 301 | (S && S->isAllocated()) && (SPrev && !SPrev->isAllocated())); |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | PathDiagnosticPiece *VisitNode(const ExplodedNode *N, |
| 305 | const ExplodedNode *PrevN, |
| 306 | BugReporterContext &BRC, |
| 307 | BugReport &BR); |
Anna Zaks | 88feba0 | 2012-05-10 01:37:40 +0000 | [diff] [blame] | 308 | |
| 309 | PathDiagnosticPiece* getEndPath(BugReporterContext &BRC, |
| 310 | const ExplodedNode *EndPathNode, |
| 311 | BugReport &BR) { |
| 312 | if (!IsLeak) |
| 313 | return 0; |
| 314 | |
| 315 | PathDiagnosticLocation L = |
| 316 | PathDiagnosticLocation::createEndOfPath(EndPathNode, |
| 317 | BRC.getSourceManager()); |
| 318 | // Do not add the statement itself as a range in case of leak. |
| 319 | return new PathDiagnosticEventPiece(L, BR.getDescription(), false); |
| 320 | } |
| 321 | |
Anna Zaks | 56a938f | 2012-03-16 23:24:20 +0000 | [diff] [blame] | 322 | private: |
| 323 | class StackHintGeneratorForReallocationFailed |
| 324 | : public StackHintGeneratorForSymbol { |
| 325 | public: |
| 326 | StackHintGeneratorForReallocationFailed(SymbolRef S, StringRef M) |
| 327 | : StackHintGeneratorForSymbol(S, M) {} |
| 328 | |
| 329 | virtual std::string getMessageForArg(const Expr *ArgE, unsigned ArgIndex) { |
Jordan Rose | 615a092 | 2012-09-22 01:24:42 +0000 | [diff] [blame] | 330 | // Printed parameters start at 1, not 0. |
| 331 | ++ArgIndex; |
| 332 | |
Anna Zaks | 56a938f | 2012-03-16 23:24:20 +0000 | [diff] [blame] | 333 | SmallString<200> buf; |
| 334 | llvm::raw_svector_ostream os(buf); |
| 335 | |
Jordan Rose | 615a092 | 2012-09-22 01:24:42 +0000 | [diff] [blame] | 336 | os << "Reallocation of " << ArgIndex << llvm::getOrdinalSuffix(ArgIndex) |
| 337 | << " parameter failed"; |
Anna Zaks | 56a938f | 2012-03-16 23:24:20 +0000 | [diff] [blame] | 338 | |
| 339 | return os.str(); |
| 340 | } |
| 341 | |
| 342 | virtual std::string getMessageForReturn(const CallExpr *CallExpr) { |
Anna Zaks | fbd5874 | 2012-03-16 23:44:28 +0000 | [diff] [blame] | 343 | return "Reallocation of returned value failed"; |
Anna Zaks | 56a938f | 2012-03-16 23:24:20 +0000 | [diff] [blame] | 344 | } |
| 345 | }; |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 346 | }; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 347 | }; |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 348 | } // end anonymous namespace |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 349 | |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 350 | REGISTER_MAP_WITH_PROGRAMSTATE(RegionState, SymbolRef, RefState) |
| 351 | REGISTER_MAP_WITH_PROGRAMSTATE(ReallocPairs, SymbolRef, ReallocPair) |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 352 | |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 353 | // A map from the freed symbol to the symbol representing the return value of |
| 354 | // the free function. |
| 355 | REGISTER_MAP_WITH_PROGRAMSTATE(FreeReturnValue, SymbolRef, SymbolRef) |
| 356 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 357 | namespace { |
| 358 | class StopTrackingCallback : public SymbolVisitor { |
| 359 | ProgramStateRef state; |
| 360 | public: |
| 361 | StopTrackingCallback(ProgramStateRef st) : state(st) {} |
| 362 | ProgramStateRef getState() const { return state; } |
| 363 | |
| 364 | bool VisitSymbol(SymbolRef sym) { |
| 365 | state = state->remove<RegionState>(sym); |
| 366 | return true; |
| 367 | } |
| 368 | }; |
| 369 | } // end anonymous namespace |
| 370 | |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 371 | void MallocChecker::initIdentifierInfo(ASTContext &Ctx) const { |
Anna Zaks | a38cb2c | 2012-05-18 22:47:40 +0000 | [diff] [blame] | 372 | if (II_malloc) |
| 373 | return; |
| 374 | II_malloc = &Ctx.Idents.get("malloc"); |
| 375 | II_free = &Ctx.Idents.get("free"); |
| 376 | II_realloc = &Ctx.Idents.get("realloc"); |
| 377 | II_reallocf = &Ctx.Idents.get("reallocf"); |
| 378 | II_calloc = &Ctx.Idents.get("calloc"); |
| 379 | II_valloc = &Ctx.Idents.get("valloc"); |
| 380 | II_strdup = &Ctx.Idents.get("strdup"); |
| 381 | II_strndup = &Ctx.Idents.get("strndup"); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 384 | bool MallocChecker::isMemFunction(const FunctionDecl *FD, ASTContext &C) const { |
Anna Zaks | 1434518 | 2012-05-18 01:16:10 +0000 | [diff] [blame] | 385 | if (isFreeFunction(FD, C)) |
| 386 | return true; |
| 387 | |
| 388 | if (isAllocationFunction(FD, C)) |
| 389 | return true; |
| 390 | |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | bool MallocChecker::isAllocationFunction(const FunctionDecl *FD, |
| 395 | ASTContext &C) const { |
Anna Zaks | 1d6cc6a | 2012-02-15 02:12:00 +0000 | [diff] [blame] | 396 | if (!FD) |
| 397 | return false; |
Anna Zaks | 1434518 | 2012-05-18 01:16:10 +0000 | [diff] [blame] | 398 | |
Jordan Rose | 5ef6e94 | 2012-07-10 23:13:01 +0000 | [diff] [blame] | 399 | if (FD->getKind() == Decl::Function) { |
| 400 | IdentifierInfo *FunI = FD->getIdentifier(); |
| 401 | initIdentifierInfo(C); |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 402 | |
Jordan Rose | 5ef6e94 | 2012-07-10 23:13:01 +0000 | [diff] [blame] | 403 | if (FunI == II_malloc || FunI == II_realloc || |
| 404 | FunI == II_reallocf || FunI == II_calloc || FunI == II_valloc || |
| 405 | FunI == II_strdup || FunI == II_strndup) |
| 406 | return true; |
| 407 | } |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 408 | |
Anna Zaks | 1434518 | 2012-05-18 01:16:10 +0000 | [diff] [blame] | 409 | if (Filter.CMallocOptimistic && FD->hasAttrs()) |
| 410 | for (specific_attr_iterator<OwnershipAttr> |
| 411 | i = FD->specific_attr_begin<OwnershipAttr>(), |
| 412 | e = FD->specific_attr_end<OwnershipAttr>(); |
| 413 | i != e; ++i) |
| 414 | if ((*i)->getOwnKind() == OwnershipAttr::Returns) |
| 415 | return true; |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | bool MallocChecker::isFreeFunction(const FunctionDecl *FD, ASTContext &C) const { |
| 420 | if (!FD) |
| 421 | return false; |
| 422 | |
Jordan Rose | 5ef6e94 | 2012-07-10 23:13:01 +0000 | [diff] [blame] | 423 | if (FD->getKind() == Decl::Function) { |
| 424 | IdentifierInfo *FunI = FD->getIdentifier(); |
| 425 | initIdentifierInfo(C); |
Anna Zaks | 1434518 | 2012-05-18 01:16:10 +0000 | [diff] [blame] | 426 | |
Jordan Rose | 5ef6e94 | 2012-07-10 23:13:01 +0000 | [diff] [blame] | 427 | if (FunI == II_free || FunI == II_realloc || FunI == II_reallocf) |
| 428 | return true; |
| 429 | } |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 430 | |
Anna Zaks | 1434518 | 2012-05-18 01:16:10 +0000 | [diff] [blame] | 431 | if (Filter.CMallocOptimistic && FD->hasAttrs()) |
| 432 | for (specific_attr_iterator<OwnershipAttr> |
| 433 | i = FD->specific_attr_begin<OwnershipAttr>(), |
| 434 | e = FD->specific_attr_end<OwnershipAttr>(); |
| 435 | i != e; ++i) |
| 436 | if ((*i)->getOwnKind() == OwnershipAttr::Takes || |
| 437 | (*i)->getOwnKind() == OwnershipAttr::Holds) |
| 438 | return true; |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 439 | return false; |
| 440 | } |
| 441 | |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 442 | void MallocChecker::checkPostStmt(const CallExpr *CE, CheckerContext &C) const { |
Jordan Rose | c20c727 | 2012-09-20 01:55:32 +0000 | [diff] [blame] | 443 | if (C.wasInlined) |
| 444 | return; |
| 445 | |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 446 | const FunctionDecl *FD = C.getCalleeDecl(CE); |
| 447 | if (!FD) |
| 448 | return; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 449 | |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 450 | ProgramStateRef State = C.getState(); |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 451 | bool ReleasedAllocatedMemory = false; |
Jordan Rose | 5ef6e94 | 2012-07-10 23:13:01 +0000 | [diff] [blame] | 452 | |
| 453 | if (FD->getKind() == Decl::Function) { |
| 454 | initIdentifierInfo(C.getASTContext()); |
| 455 | IdentifierInfo *FunI = FD->getIdentifier(); |
| 456 | |
| 457 | if (FunI == II_malloc || FunI == II_valloc) { |
| 458 | if (CE->getNumArgs() < 1) |
| 459 | return; |
| 460 | State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State); |
| 461 | } else if (FunI == II_realloc) { |
| 462 | State = ReallocMem(C, CE, false); |
| 463 | } else if (FunI == II_reallocf) { |
| 464 | State = ReallocMem(C, CE, true); |
| 465 | } else if (FunI == II_calloc) { |
| 466 | State = CallocMem(C, CE); |
| 467 | } else if (FunI == II_free) { |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 468 | State = FreeMemAux(C, CE, State, 0, false, ReleasedAllocatedMemory); |
Jordan Rose | 5ef6e94 | 2012-07-10 23:13:01 +0000 | [diff] [blame] | 469 | } else if (FunI == II_strdup) { |
| 470 | State = MallocUpdateRefState(C, CE, State); |
| 471 | } else if (FunI == II_strndup) { |
| 472 | State = MallocUpdateRefState(C, CE, State); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if (Filter.CMallocOptimistic) { |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 477 | // Check all the attributes, if there are any. |
| 478 | // There can be multiple of these attributes. |
| 479 | if (FD->hasAttrs()) |
| 480 | for (specific_attr_iterator<OwnershipAttr> |
| 481 | i = FD->specific_attr_begin<OwnershipAttr>(), |
| 482 | e = FD->specific_attr_end<OwnershipAttr>(); |
| 483 | i != e; ++i) { |
| 484 | switch ((*i)->getOwnKind()) { |
| 485 | case OwnershipAttr::Returns: |
| 486 | State = MallocMemReturnsAttr(C, CE, *i); |
| 487 | break; |
| 488 | case OwnershipAttr::Takes: |
| 489 | case OwnershipAttr::Holds: |
| 490 | State = FreeMemAttr(C, CE, *i); |
| 491 | break; |
| 492 | } |
| 493 | } |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 494 | } |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 495 | C.addTransition(State); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Jordan Rose | cde8cdb | 2012-07-02 19:27:56 +0000 | [diff] [blame] | 498 | static bool isFreeWhenDoneSetToZero(const ObjCMethodCall &Call) { |
| 499 | Selector S = Call.getSelector(); |
Anna Zaks | 3e4f65d | 2012-06-22 22:08:09 +0000 | [diff] [blame] | 500 | for (unsigned i = 1; i < S.getNumArgs(); ++i) |
Anna Zaks | 5b7aa34 | 2012-06-22 02:04:31 +0000 | [diff] [blame] | 501 | if (S.getNameForSlot(i).equals("freeWhenDone")) |
| 502 | if (Call.getArgSVal(i).isConstant(0)) |
| 503 | return true; |
| 504 | |
| 505 | return false; |
| 506 | } |
| 507 | |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 508 | void MallocChecker::checkPostObjCMessage(const ObjCMethodCall &Call, |
| 509 | CheckerContext &C) const { |
Anna Zaks | c2cca23 | 2012-12-11 00:17:53 +0000 | [diff] [blame] | 510 | if (C.wasInlined) |
| 511 | return; |
| 512 | |
Anna Zaks | 5b7aa34 | 2012-06-22 02:04:31 +0000 | [diff] [blame] | 513 | // If the first selector is dataWithBytesNoCopy, assume that the memory will |
| 514 | // be released with 'free' by the new object. |
| 515 | // Ex: [NSData dataWithBytesNoCopy:bytes length:10]; |
| 516 | // Unless 'freeWhenDone' param set to 0. |
| 517 | // TODO: Check that the memory was allocated with malloc. |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 518 | bool ReleasedAllocatedMemory = false; |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 519 | Selector S = Call.getSelector(); |
Anna Zaks | 7186dc6 | 2012-06-22 22:42:30 +0000 | [diff] [blame] | 520 | if ((S.getNameForSlot(0) == "dataWithBytesNoCopy" || |
| 521 | S.getNameForSlot(0) == "initWithBytesNoCopy" || |
| 522 | S.getNameForSlot(0) == "initWithCharactersNoCopy") && |
Jordan Rose | cde8cdb | 2012-07-02 19:27:56 +0000 | [diff] [blame] | 523 | !isFreeWhenDoneSetToZero(Call)){ |
Anna Zaks | 5b7aa34 | 2012-06-22 02:04:31 +0000 | [diff] [blame] | 524 | unsigned int argIdx = 0; |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 525 | ProgramStateRef State = FreeMemAux(C, Call.getArgExpr(argIdx), |
| 526 | Call.getOriginExpr(), C.getState(), true, |
| 527 | ReleasedAllocatedMemory, |
| 528 | /* RetNullOnFailure*/ true); |
| 529 | |
| 530 | C.addTransition(State); |
Anna Zaks | 5b7aa34 | 2012-06-22 02:04:31 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 534 | ProgramStateRef MallocChecker::MallocMemReturnsAttr(CheckerContext &C, |
| 535 | const CallExpr *CE, |
| 536 | const OwnershipAttr* Att) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 537 | if (Att->getModule() != "malloc") |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 538 | return 0; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 539 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 540 | OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 541 | if (I != E) { |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 542 | return MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), C.getState()); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 543 | } |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 544 | return MallocMemAux(C, CE, UnknownVal(), UndefinedVal(), C.getState()); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 547 | ProgramStateRef MallocChecker::MallocMemAux(CheckerContext &C, |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 548 | const CallExpr *CE, |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 549 | SVal Size, SVal Init, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 550 | ProgramStateRef state) { |
Anna Zaks | e17fdb2 | 2012-06-07 03:57:32 +0000 | [diff] [blame] | 551 | |
| 552 | // Bind the return value to the symbolic value from the heap region. |
| 553 | // TODO: We could rewrite post visit to eval call; 'malloc' does not have |
| 554 | // side effects other than what we model here. |
Ted Kremenek | 66c486f | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 555 | unsigned Count = C.blockCount(); |
Anna Zaks | e17fdb2 | 2012-06-07 03:57:32 +0000 | [diff] [blame] | 556 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 557 | const LocationContext *LCtx = C.getPredecessor()->getLocationContext(); |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 558 | DefinedSVal RetVal = svalBuilder.getConjuredHeapSymbolVal(CE, LCtx, Count) |
| 559 | .castAs<DefinedSVal>(); |
Anna Zaks | e17fdb2 | 2012-06-07 03:57:32 +0000 | [diff] [blame] | 560 | state = state->BindExpr(CE, C.getLocationContext(), RetVal); |
Zhongxing Xu | a49c6b7 | 2009-12-11 03:09:01 +0000 | [diff] [blame] | 561 | |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 562 | // We expect the malloc functions to return a pointer. |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 563 | if (!RetVal.getAs<Loc>()) |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 564 | return 0; |
| 565 | |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 566 | // Fill the region with the initialization value. |
Anna Zaks | e17fdb2 | 2012-06-07 03:57:32 +0000 | [diff] [blame] | 567 | state = state->bindDefault(RetVal, Init); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 568 | |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 569 | // Set the region's extent equal to the Size parameter. |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 570 | const SymbolicRegion *R = |
Anna Zaks | e17fdb2 | 2012-06-07 03:57:32 +0000 | [diff] [blame] | 571 | dyn_cast_or_null<SymbolicRegion>(RetVal.getAsRegion()); |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 572 | if (!R) |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 573 | return 0; |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame^] | 574 | if (Optional<DefinedOrUnknownSVal> DefinedSize = |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 575 | Size.getAs<DefinedOrUnknownSVal>()) { |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 576 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 577 | DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder); |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 578 | DefinedOrUnknownSVal extentMatchesSize = |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 579 | svalBuilder.evalEQ(state, Extent, *DefinedSize); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 580 | |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 581 | state = state->assume(extentMatchesSize, true); |
| 582 | assert(state); |
| 583 | } |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 584 | |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 585 | return MallocUpdateRefState(C, CE, state); |
| 586 | } |
| 587 | |
| 588 | ProgramStateRef MallocChecker::MallocUpdateRefState(CheckerContext &C, |
| 589 | const CallExpr *CE, |
| 590 | ProgramStateRef state) { |
| 591 | // Get the return value. |
| 592 | SVal retVal = state->getSVal(CE, C.getLocationContext()); |
| 593 | |
| 594 | // We expect the malloc functions to return a pointer. |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 595 | if (!retVal.getAs<Loc>()) |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 596 | return 0; |
| 597 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 598 | SymbolRef Sym = retVal.getAsLocSymbol(); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 599 | assert(Sym); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 600 | |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 601 | // Set the symbol's state to Allocated. |
Anna Zaks | 050cdd7 | 2012-06-20 20:57:46 +0000 | [diff] [blame] | 602 | return state->set<RegionState>(Sym, RefState::getAllocated(CE)); |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 603 | |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 606 | ProgramStateRef MallocChecker::FreeMemAttr(CheckerContext &C, |
| 607 | const CallExpr *CE, |
| 608 | const OwnershipAttr* Att) const { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 609 | if (Att->getModule() != "malloc") |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 610 | return 0; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 611 | |
Anna Zaks | b3d7275 | 2012-03-01 22:06:06 +0000 | [diff] [blame] | 612 | ProgramStateRef State = C.getState(); |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 613 | bool ReleasedAllocated = false; |
Anna Zaks | b3d7275 | 2012-03-01 22:06:06 +0000 | [diff] [blame] | 614 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 615 | for (OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end(); |
| 616 | I != E; ++I) { |
Anna Zaks | b3d7275 | 2012-03-01 22:06:06 +0000 | [diff] [blame] | 617 | ProgramStateRef StateI = FreeMemAux(C, CE, State, *I, |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 618 | Att->getOwnKind() == OwnershipAttr::Holds, |
| 619 | ReleasedAllocated); |
Anna Zaks | b3d7275 | 2012-03-01 22:06:06 +0000 | [diff] [blame] | 620 | if (StateI) |
| 621 | State = StateI; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 622 | } |
Anna Zaks | b3d7275 | 2012-03-01 22:06:06 +0000 | [diff] [blame] | 623 | return State; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 626 | ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C, |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 627 | const CallExpr *CE, |
| 628 | ProgramStateRef state, |
| 629 | unsigned Num, |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 630 | bool Hold, |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 631 | bool &ReleasedAllocated, |
| 632 | bool ReturnsNullOnFailure) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 633 | if (CE->getNumArgs() < (Num + 1)) |
| 634 | return 0; |
| 635 | |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 636 | return FreeMemAux(C, CE->getArg(Num), CE, state, Hold, |
| 637 | ReleasedAllocated, ReturnsNullOnFailure); |
| 638 | } |
| 639 | |
Anna Zaks | 2ccecfa | 2012-11-13 19:47:40 +0000 | [diff] [blame] | 640 | /// Checks if the previous call to free on the given symbol failed - if free |
| 641 | /// failed, returns true. Also, returns the corresponding return value symbol. |
Benjamin Kramer | 4d9f4e5 | 2012-11-22 15:02:44 +0000 | [diff] [blame] | 642 | static bool didPreviousFreeFail(ProgramStateRef State, |
| 643 | SymbolRef Sym, SymbolRef &RetStatusSymbol) { |
Anna Zaks | 2ccecfa | 2012-11-13 19:47:40 +0000 | [diff] [blame] | 644 | const SymbolRef *Ret = State->get<FreeReturnValue>(Sym); |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 645 | if (Ret) { |
| 646 | assert(*Ret && "We should not store the null return symbol"); |
| 647 | ConstraintManager &CMgr = State->getConstraintManager(); |
| 648 | ConditionTruthVal FreeFailed = CMgr.isNull(State, *Ret); |
Anna Zaks | 2ccecfa | 2012-11-13 19:47:40 +0000 | [diff] [blame] | 649 | RetStatusSymbol = *Ret; |
| 650 | return FreeFailed.isConstrainedTrue(); |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 651 | } |
Anna Zaks | 2ccecfa | 2012-11-13 19:47:40 +0000 | [diff] [blame] | 652 | return false; |
Anna Zaks | 5b7aa34 | 2012-06-22 02:04:31 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C, |
| 656 | const Expr *ArgExpr, |
| 657 | const Expr *ParentExpr, |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 658 | ProgramStateRef State, |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 659 | bool Hold, |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 660 | bool &ReleasedAllocated, |
| 661 | bool ReturnsNullOnFailure) const { |
Anna Zaks | 5b7aa34 | 2012-06-22 02:04:31 +0000 | [diff] [blame] | 662 | |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 663 | SVal ArgVal = State->getSVal(ArgExpr, C.getLocationContext()); |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 664 | if (!ArgVal.getAs<DefinedOrUnknownSVal>()) |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 665 | return 0; |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 666 | DefinedOrUnknownSVal location = ArgVal.castAs<DefinedOrUnknownSVal>(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 667 | |
| 668 | // Check for null dereferences. |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 669 | if (!location.getAs<Loc>()) |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 670 | return 0; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 671 | |
Anna Zaks | b276bd9 | 2012-02-14 00:26:13 +0000 | [diff] [blame] | 672 | // The explicit NULL case, no operation is performed. |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 673 | ProgramStateRef notNullState, nullState; |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 674 | llvm::tie(notNullState, nullState) = State->assume(location); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 675 | if (nullState && !notNullState) |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 676 | return 0; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 677 | |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 678 | // Unknown values could easily be okay |
| 679 | // Undefined values are handled elsewhere |
| 680 | if (ArgVal.isUnknownOrUndef()) |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 681 | return 0; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 682 | |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 683 | const MemRegion *R = ArgVal.getAsRegion(); |
| 684 | |
| 685 | // Nonlocs can't be freed, of course. |
| 686 | // Non-region locations (labels and fixed addresses) also shouldn't be freed. |
| 687 | if (!R) { |
| 688 | ReportBadFree(C, ArgVal, ArgExpr->getSourceRange()); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 689 | return 0; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | R = R->StripCasts(); |
| 693 | |
| 694 | // Blocks might show up as heap data, but should not be free()d |
| 695 | if (isa<BlockDataRegion>(R)) { |
| 696 | ReportBadFree(C, ArgVal, ArgExpr->getSourceRange()); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 697 | return 0; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | const MemSpaceRegion *MS = R->getMemorySpace(); |
| 701 | |
| 702 | // Parameters, locals, statics, and globals shouldn't be freed. |
| 703 | if (!(isa<UnknownSpaceRegion>(MS) || isa<HeapSpaceRegion>(MS))) { |
| 704 | // FIXME: at the time this code was written, malloc() regions were |
| 705 | // represented by conjured symbols, which are all in UnknownSpaceRegion. |
| 706 | // This means that there isn't actually anything from HeapSpaceRegion |
| 707 | // that should be freed, even though we allow it here. |
| 708 | // Of course, free() can work on memory allocated outside the current |
| 709 | // function, so UnknownSpaceRegion is always a possibility. |
| 710 | // False negatives are better than false positives. |
| 711 | |
| 712 | ReportBadFree(C, ArgVal, ArgExpr->getSourceRange()); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 713 | return 0; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 714 | } |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 715 | |
| 716 | const SymbolicRegion *SrBase = dyn_cast<SymbolicRegion>(R->getBaseRegion()); |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 717 | // Various cases could lead to non-symbol values here. |
| 718 | // For now, ignore them. |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 719 | if (!SrBase) |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 720 | return 0; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 721 | |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 722 | SymbolRef SymBase = SrBase->getSymbol(); |
| 723 | const RefState *RsBase = State->get<RegionState>(SymBase); |
Anna Zaks | 2ccecfa | 2012-11-13 19:47:40 +0000 | [diff] [blame] | 724 | SymbolRef PreviousRetStatusSymbol = 0; |
Zhongxing Xu | 7e3cda9 | 2010-01-18 03:27:34 +0000 | [diff] [blame] | 725 | |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 726 | // Check double free. |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 727 | if (RsBase && |
| 728 | (RsBase->isReleased() || RsBase->isRelinquished()) && |
| 729 | !didPreviousFreeFail(State, SymBase, PreviousRetStatusSymbol)) { |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 730 | |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 731 | if (ExplodedNode *N = C.generateSink()) { |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 732 | if (!BT_DoubleFree) |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 733 | BT_DoubleFree.reset( |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 734 | new BugType("Double free", "Memory Error")); |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 735 | BugReport *R = new BugReport(*BT_DoubleFree, |
| 736 | (RsBase->isReleased() ? "Attempt to free released memory" |
| 737 | : "Attempt to free non-owned memory"), |
| 738 | N); |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 739 | R->addRange(ArgExpr->getSourceRange()); |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 740 | R->markInteresting(SymBase); |
Anna Zaks | 2ccecfa | 2012-11-13 19:47:40 +0000 | [diff] [blame] | 741 | if (PreviousRetStatusSymbol) |
| 742 | R->markInteresting(PreviousRetStatusSymbol); |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 743 | R->addVisitor(new MallocBugVisitor(SymBase)); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 744 | C.emitReport(R); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 745 | } |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 746 | return 0; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 749 | // Check if the memory location being freed is the actual location |
| 750 | // allocated, or an offset. |
| 751 | RegionOffset Offset = R->getAsOffset(); |
| 752 | if (RsBase && RsBase->isAllocated() && |
| 753 | Offset.isValid() && |
| 754 | !Offset.hasSymbolicOffset() && |
| 755 | Offset.getOffset() != 0) { |
| 756 | ReportOffsetFree(C, ArgVal, ArgExpr->getSourceRange()); |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | ReleasedAllocated = (RsBase != 0); |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 761 | |
Anna Zaks | 2ccecfa | 2012-11-13 19:47:40 +0000 | [diff] [blame] | 762 | // Clean out the info on previous call to free return info. |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 763 | State = State->remove<FreeReturnValue>(SymBase); |
Anna Zaks | 2ccecfa | 2012-11-13 19:47:40 +0000 | [diff] [blame] | 764 | |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 765 | // Keep track of the return value. If it is NULL, we will know that free |
| 766 | // failed. |
| 767 | if (ReturnsNullOnFailure) { |
| 768 | SVal RetVal = C.getSVal(ParentExpr); |
| 769 | SymbolRef RetStatusSymbol = RetVal.getAsSymbol(); |
| 770 | if (RetStatusSymbol) { |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 771 | C.getSymbolManager().addSymbolDependency(SymBase, RetStatusSymbol); |
| 772 | State = State->set<FreeReturnValue>(SymBase, RetStatusSymbol); |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 776 | // Normal free. |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 777 | if (Hold) { |
| 778 | return State->set<RegionState>(SymBase, |
| 779 | RefState::getRelinquished(ParentExpr)); |
| 780 | } |
| 781 | return State->set<RegionState>(SymBase, RefState::getReleased(ParentExpr)); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 784 | bool MallocChecker::SummarizeValue(raw_ostream &os, SVal V) { |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame^] | 785 | if (Optional<nonloc::ConcreteInt> IntVal = V.getAs<nonloc::ConcreteInt>()) |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 786 | os << "an integer (" << IntVal->getValue() << ")"; |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame^] | 787 | else if (Optional<loc::ConcreteInt> ConstAddr = V.getAs<loc::ConcreteInt>()) |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 788 | os << "a constant address (" << ConstAddr->getValue() << ")"; |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame^] | 789 | else if (Optional<loc::GotoLabel> Label = V.getAs<loc::GotoLabel>()) |
Chris Lattner | 6810630 | 2011-02-17 05:38:27 +0000 | [diff] [blame] | 790 | os << "the address of the label '" << Label->getLabel()->getName() << "'"; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 791 | else |
| 792 | return false; |
| 793 | |
| 794 | return true; |
| 795 | } |
| 796 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 797 | bool MallocChecker::SummarizeRegion(raw_ostream &os, |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 798 | const MemRegion *MR) { |
| 799 | switch (MR->getKind()) { |
| 800 | case MemRegion::FunctionTextRegionKind: { |
Anna Zaks | 5fc1d0c | 2012-09-17 19:13:56 +0000 | [diff] [blame] | 801 | const NamedDecl *FD = cast<FunctionTextRegion>(MR)->getDecl(); |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 802 | if (FD) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 803 | os << "the address of the function '" << *FD << '\''; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 804 | else |
| 805 | os << "the address of a function"; |
| 806 | return true; |
| 807 | } |
| 808 | case MemRegion::BlockTextRegionKind: |
| 809 | os << "block text"; |
| 810 | return true; |
| 811 | case MemRegion::BlockDataRegionKind: |
| 812 | // FIXME: where the block came from? |
| 813 | os << "a block"; |
| 814 | return true; |
| 815 | default: { |
| 816 | const MemSpaceRegion *MS = MR->getMemorySpace(); |
| 817 | |
Anna Zaks | eb31a76 | 2012-01-04 23:54:01 +0000 | [diff] [blame] | 818 | if (isa<StackLocalsSpaceRegion>(MS)) { |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 819 | const VarRegion *VR = dyn_cast<VarRegion>(MR); |
| 820 | const VarDecl *VD; |
| 821 | if (VR) |
| 822 | VD = VR->getDecl(); |
| 823 | else |
| 824 | VD = NULL; |
| 825 | |
| 826 | if (VD) |
| 827 | os << "the address of the local variable '" << VD->getName() << "'"; |
| 828 | else |
| 829 | os << "the address of a local stack variable"; |
| 830 | return true; |
| 831 | } |
Anna Zaks | eb31a76 | 2012-01-04 23:54:01 +0000 | [diff] [blame] | 832 | |
| 833 | if (isa<StackArgumentsSpaceRegion>(MS)) { |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 834 | const VarRegion *VR = dyn_cast<VarRegion>(MR); |
| 835 | const VarDecl *VD; |
| 836 | if (VR) |
| 837 | VD = VR->getDecl(); |
| 838 | else |
| 839 | VD = NULL; |
| 840 | |
| 841 | if (VD) |
| 842 | os << "the address of the parameter '" << VD->getName() << "'"; |
| 843 | else |
| 844 | os << "the address of a parameter"; |
| 845 | return true; |
| 846 | } |
Anna Zaks | eb31a76 | 2012-01-04 23:54:01 +0000 | [diff] [blame] | 847 | |
| 848 | if (isa<GlobalsSpaceRegion>(MS)) { |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 849 | const VarRegion *VR = dyn_cast<VarRegion>(MR); |
| 850 | const VarDecl *VD; |
| 851 | if (VR) |
| 852 | VD = VR->getDecl(); |
| 853 | else |
| 854 | VD = NULL; |
| 855 | |
| 856 | if (VD) { |
| 857 | if (VD->isStaticLocal()) |
| 858 | os << "the address of the static variable '" << VD->getName() << "'"; |
| 859 | else |
| 860 | os << "the address of the global variable '" << VD->getName() << "'"; |
| 861 | } else |
| 862 | os << "the address of a global variable"; |
| 863 | return true; |
| 864 | } |
Anna Zaks | eb31a76 | 2012-01-04 23:54:01 +0000 | [diff] [blame] | 865 | |
| 866 | return false; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal, |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 872 | SourceRange range) const { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 873 | if (ExplodedNode *N = C.generateSink()) { |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 874 | if (!BT_BadFree) |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 875 | BT_BadFree.reset(new BugType("Bad free", "Memory Error")); |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 876 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 877 | SmallString<100> buf; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 878 | llvm::raw_svector_ostream os(buf); |
| 879 | |
| 880 | const MemRegion *MR = ArgVal.getAsRegion(); |
| 881 | if (MR) { |
| 882 | while (const ElementRegion *ER = dyn_cast<ElementRegion>(MR)) |
| 883 | MR = ER->getSuperRegion(); |
| 884 | |
| 885 | // Special case for alloca() |
| 886 | if (isa<AllocaRegion>(MR)) |
| 887 | os << "Argument to free() was allocated by alloca(), not malloc()"; |
| 888 | else { |
| 889 | os << "Argument to free() is "; |
| 890 | if (SummarizeRegion(os, MR)) |
| 891 | os << ", which is not memory allocated by malloc()"; |
| 892 | else |
| 893 | os << "not memory allocated by malloc()"; |
| 894 | } |
| 895 | } else { |
| 896 | os << "Argument to free() is "; |
| 897 | if (SummarizeValue(os, ArgVal)) |
| 898 | os << ", which is not memory allocated by malloc()"; |
| 899 | else |
| 900 | os << "not memory allocated by malloc()"; |
| 901 | } |
| 902 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 903 | BugReport *R = new BugReport(*BT_BadFree, os.str(), N); |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 904 | R->markInteresting(MR); |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 905 | R->addRange(range); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 906 | C.emitReport(R); |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 910 | void MallocChecker::ReportOffsetFree(CheckerContext &C, SVal ArgVal, |
| 911 | SourceRange Range) const { |
| 912 | ExplodedNode *N = C.generateSink(); |
| 913 | if (N == NULL) |
| 914 | return; |
| 915 | |
| 916 | if (!BT_OffsetFree) |
| 917 | BT_OffsetFree.reset(new BugType("Offset free", "Memory Error")); |
| 918 | |
| 919 | SmallString<100> buf; |
| 920 | llvm::raw_svector_ostream os(buf); |
| 921 | |
| 922 | const MemRegion *MR = ArgVal.getAsRegion(); |
| 923 | assert(MR && "Only MemRegion based symbols can have offset free errors"); |
| 924 | |
| 925 | RegionOffset Offset = MR->getAsOffset(); |
| 926 | assert((Offset.isValid() && |
| 927 | !Offset.hasSymbolicOffset() && |
| 928 | Offset.getOffset() != 0) && |
| 929 | "Only symbols with a valid offset can have offset free errors"); |
| 930 | |
| 931 | int offsetBytes = Offset.getOffset() / C.getASTContext().getCharWidth(); |
| 932 | |
| 933 | os << "Argument to free() is offset by " |
| 934 | << offsetBytes |
| 935 | << " " |
| 936 | << ((abs(offsetBytes) > 1) ? "bytes" : "byte") |
| 937 | << " from the start of memory allocated by malloc()"; |
| 938 | |
| 939 | BugReport *R = new BugReport(*BT_OffsetFree, os.str(), N); |
| 940 | R->markInteresting(MR->getBaseRegion()); |
| 941 | R->addRange(Range); |
| 942 | C.emitReport(R); |
| 943 | } |
| 944 | |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 945 | ProgramStateRef MallocChecker::ReallocMem(CheckerContext &C, |
| 946 | const CallExpr *CE, |
| 947 | bool FreesOnFail) const { |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 948 | if (CE->getNumArgs() < 2) |
| 949 | return 0; |
| 950 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 951 | ProgramStateRef state = C.getState(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 952 | const Expr *arg0Expr = CE->getArg(0); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 953 | const LocationContext *LCtx = C.getLocationContext(); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 954 | SVal Arg0Val = state->getSVal(arg0Expr, LCtx); |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 955 | if (!Arg0Val.getAs<DefinedOrUnknownSVal>()) |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 956 | return 0; |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 957 | DefinedOrUnknownSVal arg0Val = Arg0Val.castAs<DefinedOrUnknownSVal>(); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 958 | |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 959 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 960 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 961 | DefinedOrUnknownSVal PtrEQ = |
| 962 | svalBuilder.evalEQ(state, arg0Val, svalBuilder.makeNull()); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 963 | |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 964 | // Get the size argument. If there is no size arg then give up. |
| 965 | const Expr *Arg1 = CE->getArg(1); |
| 966 | if (!Arg1) |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 967 | return 0; |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 968 | |
| 969 | // Get the value of the size argument. |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 970 | SVal Arg1ValG = state->getSVal(Arg1, LCtx); |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 971 | if (!Arg1ValG.getAs<DefinedOrUnknownSVal>()) |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 972 | return 0; |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 973 | DefinedOrUnknownSVal Arg1Val = Arg1ValG.castAs<DefinedOrUnknownSVal>(); |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 974 | |
| 975 | // Compare the size argument to 0. |
| 976 | DefinedOrUnknownSVal SizeZero = |
| 977 | svalBuilder.evalEQ(state, Arg1Val, |
| 978 | svalBuilder.makeIntValWithPtrWidth(0, false)); |
| 979 | |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 980 | ProgramStateRef StatePtrIsNull, StatePtrNotNull; |
| 981 | llvm::tie(StatePtrIsNull, StatePtrNotNull) = state->assume(PtrEQ); |
| 982 | ProgramStateRef StateSizeIsZero, StateSizeNotZero; |
| 983 | llvm::tie(StateSizeIsZero, StateSizeNotZero) = state->assume(SizeZero); |
| 984 | // We only assume exceptional states if they are definitely true; if the |
| 985 | // state is under-constrained, assume regular realloc behavior. |
| 986 | bool PrtIsNull = StatePtrIsNull && !StatePtrNotNull; |
| 987 | bool SizeIsZero = StateSizeIsZero && !StateSizeNotZero; |
| 988 | |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 989 | // If the ptr is NULL and the size is not 0, the call is equivalent to |
| 990 | // malloc(size). |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 991 | if ( PrtIsNull && !SizeIsZero) { |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 992 | ProgramStateRef stateMalloc = MallocMemAux(C, CE, CE->getArg(1), |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 993 | UndefinedVal(), StatePtrIsNull); |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 994 | return stateMalloc; |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 997 | if (PrtIsNull && SizeIsZero) |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 998 | return 0; |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 999 | |
Anna Zaks | 30838b9 | 2012-02-13 20:57:07 +0000 | [diff] [blame] | 1000 | // 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] | 1001 | assert(!PrtIsNull); |
Anna Zaks | 30838b9 | 2012-02-13 20:57:07 +0000 | [diff] [blame] | 1002 | SymbolRef FromPtr = arg0Val.getAsSymbol(); |
| 1003 | SVal RetVal = state->getSVal(CE, LCtx); |
| 1004 | SymbolRef ToPtr = RetVal.getAsSymbol(); |
| 1005 | if (!FromPtr || !ToPtr) |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 1006 | return 0; |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 1007 | |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 1008 | bool ReleasedAllocated = false; |
| 1009 | |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 1010 | // If the size is 0, free the memory. |
| 1011 | if (SizeIsZero) |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 1012 | if (ProgramStateRef stateFree = FreeMemAux(C, CE, StateSizeIsZero, 0, |
| 1013 | false, ReleasedAllocated)){ |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 1014 | // The semantics of the return value are: |
| 1015 | // 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] | 1016 | // to free() is returned. We just free the input pointer and do not add |
| 1017 | // any constrains on the output pointer. |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 1018 | return stateFree; |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | // Default behavior. |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 1022 | if (ProgramStateRef stateFree = |
| 1023 | FreeMemAux(C, CE, state, 0, false, ReleasedAllocated)) { |
| 1024 | |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 1025 | ProgramStateRef stateRealloc = MallocMemAux(C, CE, CE->getArg(1), |
| 1026 | UnknownVal(), stateFree); |
Anna Zaks | 30838b9 | 2012-02-13 20:57:07 +0000 | [diff] [blame] | 1027 | if (!stateRealloc) |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 1028 | return 0; |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 1029 | |
Anna Zaks | 9dc298b | 2012-09-12 22:57:34 +0000 | [diff] [blame] | 1030 | ReallocPairKind Kind = RPToBeFreedAfterFailure; |
| 1031 | if (FreesOnFail) |
| 1032 | Kind = RPIsFreeOnFailure; |
| 1033 | else if (!ReleasedAllocated) |
| 1034 | Kind = RPDoNotTrackAfterFailure; |
| 1035 | |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 1036 | // Record the info about the reallocated symbol so that we could properly |
| 1037 | // process failed reallocation. |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 1038 | stateRealloc = stateRealloc->set<ReallocPairs>(ToPtr, |
Anna Zaks | 9dc298b | 2012-09-12 22:57:34 +0000 | [diff] [blame] | 1039 | ReallocPair(FromPtr, Kind)); |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 1040 | // 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] | 1041 | C.getSymbolManager().addSymbolDependency(ToPtr, FromPtr); |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 1042 | return stateRealloc; |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 1043 | } |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 1044 | return 0; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 1045 | } |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 1046 | |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 1047 | ProgramStateRef MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE){ |
Anna Zaks | 259052d | 2012-04-10 23:41:11 +0000 | [diff] [blame] | 1048 | if (CE->getNumArgs() < 2) |
| 1049 | return 0; |
| 1050 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1051 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 1052 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1053 | const LocationContext *LCtx = C.getLocationContext(); |
| 1054 | SVal count = state->getSVal(CE->getArg(0), LCtx); |
| 1055 | SVal elementSize = state->getSVal(CE->getArg(1), LCtx); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1056 | SVal TotalSize = svalBuilder.evalBinOp(state, BO_Mul, count, elementSize, |
| 1057 | svalBuilder.getContext().getSizeType()); |
| 1058 | SVal zeroVal = svalBuilder.makeZeroVal(svalBuilder.getContext().CharTy); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 1059 | |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 1060 | return MallocMemAux(C, CE, TotalSize, zeroVal, state); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Anna Zaks | 3d7c44e | 2012-03-21 19:45:08 +0000 | [diff] [blame] | 1063 | LeakInfo |
Anna Zaks | ca8e36e | 2012-02-23 21:38:21 +0000 | [diff] [blame] | 1064 | MallocChecker::getAllocationSite(const ExplodedNode *N, SymbolRef Sym, |
| 1065 | CheckerContext &C) const { |
Anna Zaks | 7752d29 | 2012-02-27 23:40:55 +0000 | [diff] [blame] | 1066 | const LocationContext *LeakContext = N->getLocationContext(); |
Anna Zaks | ca8e36e | 2012-02-23 21:38:21 +0000 | [diff] [blame] | 1067 | // Walk the ExplodedGraph backwards and find the first node that referred to |
| 1068 | // the tracked symbol. |
| 1069 | const ExplodedNode *AllocNode = N; |
Anna Zaks | 3d7c44e | 2012-03-21 19:45:08 +0000 | [diff] [blame] | 1070 | const MemRegion *ReferenceRegion = 0; |
Anna Zaks | ca8e36e | 2012-02-23 21:38:21 +0000 | [diff] [blame] | 1071 | |
| 1072 | while (N) { |
Anna Zaks | 3d7c44e | 2012-03-21 19:45:08 +0000 | [diff] [blame] | 1073 | ProgramStateRef State = N->getState(); |
| 1074 | if (!State->get<RegionState>(Sym)) |
Anna Zaks | ca8e36e | 2012-02-23 21:38:21 +0000 | [diff] [blame] | 1075 | break; |
Anna Zaks | 3d7c44e | 2012-03-21 19:45:08 +0000 | [diff] [blame] | 1076 | |
| 1077 | // Find the most recent expression bound to the symbol in the current |
| 1078 | // context. |
Anna Zaks | 3d7c44e | 2012-03-21 19:45:08 +0000 | [diff] [blame] | 1079 | if (!ReferenceRegion) { |
Benjamin Kramer | 850f1b1 | 2012-03-21 21:03:48 +0000 | [diff] [blame] | 1080 | if (const MemRegion *MR = C.getLocationRegionIfPostStore(N)) { |
| 1081 | SVal Val = State->getSVal(MR); |
| 1082 | if (Val.getAsLocSymbol() == Sym) |
| 1083 | ReferenceRegion = MR; |
| 1084 | } |
Anna Zaks | 3d7c44e | 2012-03-21 19:45:08 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Anna Zaks | 7752d29 | 2012-02-27 23:40:55 +0000 | [diff] [blame] | 1087 | // Allocation node, is the last node in the current context in which the |
| 1088 | // symbol was tracked. |
| 1089 | if (N->getLocationContext() == LeakContext) |
| 1090 | AllocNode = N; |
Anna Zaks | ca8e36e | 2012-02-23 21:38:21 +0000 | [diff] [blame] | 1091 | N = N->pred_empty() ? NULL : *(N->pred_begin()); |
| 1092 | } |
| 1093 | |
Anna Zaks | 97bfb55 | 2013-01-08 00:25:29 +0000 | [diff] [blame] | 1094 | return LeakInfo(AllocNode, ReferenceRegion); |
Anna Zaks | ca8e36e | 2012-02-23 21:38:21 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 1097 | void MallocChecker::reportLeak(SymbolRef Sym, ExplodedNode *N, |
| 1098 | CheckerContext &C) const { |
| 1099 | assert(N); |
| 1100 | if (!BT_Leak) { |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 1101 | BT_Leak.reset(new BugType("Memory leak", "Memory Error")); |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 1102 | // Leaks should not be reported if they are post-dominated by a sink: |
| 1103 | // (1) Sinks are higher importance bugs. |
| 1104 | // (2) NoReturnFunctionChecker uses sink nodes to represent paths ending |
| 1105 | // with __noreturn functions such as assert() or exit(). We choose not |
| 1106 | // to report leaks on such paths. |
| 1107 | BT_Leak->setSuppressOnSink(true); |
| 1108 | } |
| 1109 | |
Anna Zaks | ca8e36e | 2012-02-23 21:38:21 +0000 | [diff] [blame] | 1110 | // Most bug reports are cached at the location where they occurred. |
| 1111 | // With leaks, we want to unique them by the location where they were |
| 1112 | // allocated, and only report a single path. |
Anna Zaks | 7752d29 | 2012-02-27 23:40:55 +0000 | [diff] [blame] | 1113 | PathDiagnosticLocation LocUsedForUniqueing; |
Anna Zaks | 97bfb55 | 2013-01-08 00:25:29 +0000 | [diff] [blame] | 1114 | const ExplodedNode *AllocNode = 0; |
Anna Zaks | 3d7c44e | 2012-03-21 19:45:08 +0000 | [diff] [blame] | 1115 | const MemRegion *Region = 0; |
Anna Zaks | 97bfb55 | 2013-01-08 00:25:29 +0000 | [diff] [blame] | 1116 | llvm::tie(AllocNode, Region) = getAllocationSite(N, Sym, C); |
| 1117 | |
| 1118 | ProgramPoint P = AllocNode->getLocation(); |
| 1119 | const Stmt *AllocationStmt = 0; |
| 1120 | if (CallExitEnd *Exit = dyn_cast<CallExitEnd>(&P)) |
| 1121 | AllocationStmt = Exit->getCalleeContext()->getCallSite(); |
| 1122 | else if (StmtPoint *SP = dyn_cast<StmtPoint>(&P)) |
| 1123 | AllocationStmt = SP->getStmt(); |
| 1124 | if (AllocationStmt) |
| 1125 | LocUsedForUniqueing = PathDiagnosticLocation::createBegin(AllocationStmt, |
| 1126 | C.getSourceManager(), |
| 1127 | AllocNode->getLocationContext()); |
Anna Zaks | ca8e36e | 2012-02-23 21:38:21 +0000 | [diff] [blame] | 1128 | |
Anna Zaks | 3d7c44e | 2012-03-21 19:45:08 +0000 | [diff] [blame] | 1129 | SmallString<200> buf; |
| 1130 | llvm::raw_svector_ostream os(buf); |
| 1131 | os << "Memory is never released; potential leak"; |
Jordan Rose | 919e8a1 | 2012-08-08 18:23:36 +0000 | [diff] [blame] | 1132 | if (Region && Region->canPrintPretty()) { |
Anna Zaks | 3d7c44e | 2012-03-21 19:45:08 +0000 | [diff] [blame] | 1133 | os << " of memory pointed to by '"; |
Jordan Rose | 919e8a1 | 2012-08-08 18:23:36 +0000 | [diff] [blame] | 1134 | Region->printPretty(os); |
Jordan Rose | 0d53ab4 | 2012-08-08 18:23:31 +0000 | [diff] [blame] | 1135 | os << '\''; |
Anna Zaks | 3d7c44e | 2012-03-21 19:45:08 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Anna Zaks | 97bfb55 | 2013-01-08 00:25:29 +0000 | [diff] [blame] | 1138 | BugReport *R = new BugReport(*BT_Leak, os.str(), N, |
| 1139 | LocUsedForUniqueing, |
| 1140 | AllocNode->getLocationContext()->getDecl()); |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 1141 | R->markInteresting(Sym); |
Anna Zaks | 88feba0 | 2012-05-10 01:37:40 +0000 | [diff] [blame] | 1142 | R->addVisitor(new MallocBugVisitor(Sym, true)); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 1143 | C.emitReport(R); |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 1146 | void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper, |
| 1147 | CheckerContext &C) const |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 1148 | { |
Zhongxing Xu | 173ff56 | 2010-08-15 08:19:57 +0000 | [diff] [blame] | 1149 | if (!SymReaper.hasDeadSymbols()) |
| 1150 | return; |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 1151 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1152 | ProgramStateRef state = C.getState(); |
Zhongxing Xu | 173ff56 | 2010-08-15 08:19:57 +0000 | [diff] [blame] | 1153 | RegionStateTy RS = state->get<RegionState>(); |
Jordy Rose | 9076014 | 2010-08-18 04:33:47 +0000 | [diff] [blame] | 1154 | RegionStateTy::Factory &F = state->get_context<RegionState>(); |
Zhongxing Xu | 173ff56 | 2010-08-15 08:19:57 +0000 | [diff] [blame] | 1155 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1156 | SmallVector<SymbolRef, 2> Errors; |
Zhongxing Xu | 173ff56 | 2010-08-15 08:19:57 +0000 | [diff] [blame] | 1157 | for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) { |
| 1158 | if (SymReaper.isDead(I->first)) { |
Anna Zaks | 5445870 | 2012-10-29 22:51:54 +0000 | [diff] [blame] | 1159 | if (I->second.isAllocated()) |
Anna Zaks | f8c17b7 | 2012-02-09 06:48:19 +0000 | [diff] [blame] | 1160 | Errors.push_back(I->first); |
Jordy Rose | 9076014 | 2010-08-18 04:33:47 +0000 | [diff] [blame] | 1161 | // Remove the dead symbol from the map. |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 1162 | RS = F.remove(RS, I->first); |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 1163 | |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 1164 | } |
| 1165 | } |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 1166 | |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 1167 | // Cleanup the Realloc Pairs Map. |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 1168 | ReallocPairsTy RP = state->get<ReallocPairs>(); |
| 1169 | for (ReallocPairsTy::iterator I = RP.begin(), E = RP.end(); I != E; ++I) { |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 1170 | if (SymReaper.isDead(I->first) || |
| 1171 | SymReaper.isDead(I->second.ReallocatedSym)) { |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 1172 | state = state->remove<ReallocPairs>(I->first); |
| 1173 | } |
| 1174 | } |
| 1175 | |
Anna Zaks | 4141e4d | 2012-11-13 03:18:01 +0000 | [diff] [blame] | 1176 | // Cleanup the FreeReturnValue Map. |
| 1177 | FreeReturnValueTy FR = state->get<FreeReturnValue>(); |
| 1178 | for (FreeReturnValueTy::iterator I = FR.begin(), E = FR.end(); I != E; ++I) { |
| 1179 | if (SymReaper.isDead(I->first) || |
| 1180 | SymReaper.isDead(I->second)) { |
| 1181 | state = state->remove<FreeReturnValue>(I->first); |
| 1182 | } |
| 1183 | } |
| 1184 | |
Anna Zaks | ca8e36e | 2012-02-23 21:38:21 +0000 | [diff] [blame] | 1185 | // Generate leak node. |
Anna Zaks | 5445870 | 2012-10-29 22:51:54 +0000 | [diff] [blame] | 1186 | ExplodedNode *N = C.getPredecessor(); |
| 1187 | if (!Errors.empty()) { |
| 1188 | static SimpleProgramPointTag Tag("MallocChecker : DeadSymbolsLeak"); |
| 1189 | N = C.addTransition(C.getState(), C.getPredecessor(), &Tag); |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1190 | for (SmallVector<SymbolRef, 2>::iterator |
Anna Zaks | 5445870 | 2012-10-29 22:51:54 +0000 | [diff] [blame] | 1191 | I = Errors.begin(), E = Errors.end(); I != E; ++I) { |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 1192 | reportLeak(*I, N, C); |
Anna Zaks | f8c17b7 | 2012-02-09 06:48:19 +0000 | [diff] [blame] | 1193 | } |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 1194 | } |
Anna Zaks | 5445870 | 2012-10-29 22:51:54 +0000 | [diff] [blame] | 1195 | |
Anna Zaks | ca8e36e | 2012-02-23 21:38:21 +0000 | [diff] [blame] | 1196 | C.addTransition(state->set<RegionState>(RS), N); |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 1197 | } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 1198 | |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1199 | void MallocChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const { |
Anna Zaks | 1434518 | 2012-05-18 01:16:10 +0000 | [diff] [blame] | 1200 | // We will check for double free in the post visit. |
| 1201 | if (isFreeFunction(C.getCalleeDecl(CE), C.getASTContext())) |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1202 | return; |
| 1203 | |
| 1204 | // Check use after free, when a freed pointer is passed to a call. |
| 1205 | ProgramStateRef State = C.getState(); |
| 1206 | for (CallExpr::const_arg_iterator I = CE->arg_begin(), |
| 1207 | E = CE->arg_end(); I != E; ++I) { |
| 1208 | const Expr *A = *I; |
| 1209 | if (A->getType().getTypePtr()->isAnyPointerType()) { |
| 1210 | SymbolRef Sym = State->getSVal(A, C.getLocationContext()).getAsSymbol(); |
| 1211 | if (!Sym) |
| 1212 | continue; |
| 1213 | if (checkUseAfterFree(Sym, C, A)) |
| 1214 | return; |
| 1215 | } |
| 1216 | } |
| 1217 | } |
| 1218 | |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 1219 | void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const { |
| 1220 | const Expr *E = S->getRetValue(); |
| 1221 | if (!E) |
| 1222 | return; |
Anna Zaks | 0860cd0 | 2012-02-11 21:44:39 +0000 | [diff] [blame] | 1223 | |
| 1224 | // Check if we are returning a symbol. |
Jordan Rose | 0d53ab4 | 2012-08-08 18:23:31 +0000 | [diff] [blame] | 1225 | ProgramStateRef State = C.getState(); |
| 1226 | SVal RetVal = State->getSVal(E, C.getLocationContext()); |
Anna Zaks | d9ab7bb | 2012-02-22 02:36:01 +0000 | [diff] [blame] | 1227 | SymbolRef Sym = RetVal.getAsSymbol(); |
| 1228 | if (!Sym) |
| 1229 | // If we are returning a field of the allocated struct or an array element, |
| 1230 | // the callee could still free the memory. |
| 1231 | // TODO: This logic should be a part of generic symbol escape callback. |
| 1232 | if (const MemRegion *MR = RetVal.getAsRegion()) |
| 1233 | if (isa<FieldRegion>(MR) || isa<ElementRegion>(MR)) |
| 1234 | if (const SymbolicRegion *BMR = |
| 1235 | dyn_cast<SymbolicRegion>(MR->getBaseRegion())) |
| 1236 | Sym = BMR->getSymbol(); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 1237 | |
Anna Zaks | 0860cd0 | 2012-02-11 21:44:39 +0000 | [diff] [blame] | 1238 | // Check if we are returning freed memory. |
Jordan Rose | 0d53ab4 | 2012-08-08 18:23:31 +0000 | [diff] [blame] | 1239 | if (Sym) |
Jordan Rose | 65d4bd6 | 2012-11-15 19:11:33 +0000 | [diff] [blame] | 1240 | checkUseAfterFree(Sym, C, E); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 1241 | } |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 1242 | |
Anna Zaks | f5aa3f5 | 2012-03-22 00:57:20 +0000 | [diff] [blame] | 1243 | // TODO: Blocks should be either inlined or should call invalidate regions |
| 1244 | // upon invocation. After that's in place, special casing here will not be |
| 1245 | // needed. |
| 1246 | void MallocChecker::checkPostStmt(const BlockExpr *BE, |
| 1247 | CheckerContext &C) const { |
| 1248 | |
| 1249 | // Scan the BlockDecRefExprs for any object the retain count checker |
| 1250 | // may be tracking. |
| 1251 | if (!BE->getBlockDecl()->hasCaptures()) |
| 1252 | return; |
| 1253 | |
| 1254 | ProgramStateRef state = C.getState(); |
| 1255 | const BlockDataRegion *R = |
| 1256 | cast<BlockDataRegion>(state->getSVal(BE, |
| 1257 | C.getLocationContext()).getAsRegion()); |
| 1258 | |
| 1259 | BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(), |
| 1260 | E = R->referenced_vars_end(); |
| 1261 | |
| 1262 | if (I == E) |
| 1263 | return; |
| 1264 | |
| 1265 | SmallVector<const MemRegion*, 10> Regions; |
| 1266 | const LocationContext *LC = C.getLocationContext(); |
| 1267 | MemRegionManager &MemMgr = C.getSValBuilder().getRegionManager(); |
| 1268 | |
| 1269 | for ( ; I != E; ++I) { |
Ted Kremenek | e3ce2c1 | 2012-12-06 07:17:20 +0000 | [diff] [blame] | 1270 | const VarRegion *VR = I.getCapturedRegion(); |
Anna Zaks | f5aa3f5 | 2012-03-22 00:57:20 +0000 | [diff] [blame] | 1271 | if (VR->getSuperRegion() == R) { |
| 1272 | VR = MemMgr.getVarRegion(VR->getDecl(), LC); |
| 1273 | } |
| 1274 | Regions.push_back(VR); |
| 1275 | } |
| 1276 | |
| 1277 | state = |
| 1278 | state->scanReachableSymbols<StopTrackingCallback>(Regions.data(), |
| 1279 | Regions.data() + Regions.size()).getState(); |
| 1280 | C.addTransition(state); |
| 1281 | } |
| 1282 | |
Anna Zaks | 1434518 | 2012-05-18 01:16:10 +0000 | [diff] [blame] | 1283 | bool MallocChecker::isReleased(SymbolRef Sym, CheckerContext &C) const { |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 1284 | assert(Sym); |
| 1285 | const RefState *RS = C.getState()->get<RegionState>(Sym); |
Anna Zaks | 1434518 | 2012-05-18 01:16:10 +0000 | [diff] [blame] | 1286 | return (RS && RS->isReleased()); |
| 1287 | } |
| 1288 | |
| 1289 | bool MallocChecker::checkUseAfterFree(SymbolRef Sym, CheckerContext &C, |
| 1290 | const Stmt *S) const { |
| 1291 | if (isReleased(Sym, C)) { |
Anna Zaks | 15d0ae1 | 2012-02-11 23:46:36 +0000 | [diff] [blame] | 1292 | if (ExplodedNode *N = C.generateSink()) { |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 1293 | if (!BT_UseFree) |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 1294 | BT_UseFree.reset(new BugType("Use-after-free", "Memory Error")); |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 1295 | |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 1296 | BugReport *R = new BugReport(*BT_UseFree, |
| 1297 | "Use of memory after it is freed",N); |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 1298 | if (S) |
| 1299 | R->addRange(S->getSourceRange()); |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 1300 | R->markInteresting(Sym); |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 1301 | R->addVisitor(new MallocBugVisitor(Sym)); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 1302 | C.emitReport(R); |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 1303 | return true; |
| 1304 | } |
| 1305 | } |
| 1306 | return false; |
| 1307 | } |
| 1308 | |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 1309 | // Check if the location is a freed symbolic region. |
Anna Zaks | 390909c | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 1310 | void MallocChecker::checkLocation(SVal l, bool isLoad, const Stmt *S, |
| 1311 | CheckerContext &C) const { |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 1312 | SymbolRef Sym = l.getLocSymbolInBase(); |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 1313 | if (Sym) |
Anna Zaks | 1434518 | 2012-05-18 01:16:10 +0000 | [diff] [blame] | 1314 | checkUseAfterFree(Sym, C, S); |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 1315 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1316 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 1317 | // If a symbolic region is assumed to NULL (or another constant), stop tracking |
| 1318 | // it - assuming that allocation failed on this path. |
| 1319 | ProgramStateRef MallocChecker::evalAssume(ProgramStateRef state, |
| 1320 | SVal Cond, |
| 1321 | bool Assumption) const { |
| 1322 | RegionStateTy RS = state->get<RegionState>(); |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 1323 | for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) { |
Ted Kremenek | 47cbd0f | 2012-09-07 22:31:01 +0000 | [diff] [blame] | 1324 | // If the symbol is assumed to be NULL, remove it from consideration. |
Jordan Rose | ec8d420 | 2012-11-01 00:18:27 +0000 | [diff] [blame] | 1325 | ConstraintManager &CMgr = state->getConstraintManager(); |
| 1326 | ConditionTruthVal AllocFailed = CMgr.isNull(state, I.getKey()); |
| 1327 | if (AllocFailed.isConstrainedTrue()) |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 1328 | state = state->remove<RegionState>(I.getKey()); |
| 1329 | } |
| 1330 | |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 1331 | // Realloc returns 0 when reallocation fails, which means that we should |
| 1332 | // restore the state of the pointer being reallocated. |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 1333 | ReallocPairsTy RP = state->get<ReallocPairs>(); |
| 1334 | for (ReallocPairsTy::iterator I = RP.begin(), E = RP.end(); I != E; ++I) { |
Ted Kremenek | 47cbd0f | 2012-09-07 22:31:01 +0000 | [diff] [blame] | 1335 | // If the symbol is assumed to be NULL, remove it from consideration. |
Jordan Rose | ec8d420 | 2012-11-01 00:18:27 +0000 | [diff] [blame] | 1336 | ConstraintManager &CMgr = state->getConstraintManager(); |
| 1337 | ConditionTruthVal AllocFailed = CMgr.isNull(state, I.getKey()); |
Jordan Rose | 79a29eb | 2012-11-01 00:25:15 +0000 | [diff] [blame] | 1338 | if (!AllocFailed.isConstrainedTrue()) |
Anna Zaks | 9dc298b | 2012-09-12 22:57:34 +0000 | [diff] [blame] | 1339 | continue; |
Jordan Rose | ec8d420 | 2012-11-01 00:18:27 +0000 | [diff] [blame] | 1340 | |
Anna Zaks | 9dc298b | 2012-09-12 22:57:34 +0000 | [diff] [blame] | 1341 | SymbolRef ReallocSym = I.getData().ReallocatedSym; |
| 1342 | if (const RefState *RS = state->get<RegionState>(ReallocSym)) { |
| 1343 | if (RS->isReleased()) { |
| 1344 | if (I.getData().Kind == RPToBeFreedAfterFailure) |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 1345 | state = state->set<RegionState>(ReallocSym, |
Anna Zaks | 9dc298b | 2012-09-12 22:57:34 +0000 | [diff] [blame] | 1346 | RefState::getAllocated(RS->getStmt())); |
| 1347 | else if (I.getData().Kind == RPDoNotTrackAfterFailure) |
| 1348 | state = state->remove<RegionState>(ReallocSym); |
| 1349 | else |
| 1350 | assert(I.getData().Kind == RPIsFreeOnFailure); |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 1351 | } |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 1352 | } |
Anna Zaks | 9dc298b | 2012-09-12 22:57:34 +0000 | [diff] [blame] | 1353 | state = state->remove<ReallocPairs>(I.getKey()); |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 1356 | return state; |
| 1357 | } |
| 1358 | |
Anna Zaks | 3cd89ad | 2012-02-24 23:56:53 +0000 | [diff] [blame] | 1359 | // Check if the function is known to us. So, for example, we could |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1360 | // conservatively assume it can free/reallocate its pointer arguments. |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1361 | // (We assume that the pointers cannot escape through calls to system |
| 1362 | // functions not handled by this checker.) |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1363 | bool MallocChecker::doesNotFreeMemory(const CallEvent *Call, |
Anna Zaks | 3cd89ad | 2012-02-24 23:56:53 +0000 | [diff] [blame] | 1364 | ProgramStateRef State) const { |
Jordan Rose | 85d7e01 | 2012-07-02 19:27:51 +0000 | [diff] [blame] | 1365 | assert(Call); |
Anna Zaks | 3cd89ad | 2012-02-24 23:56:53 +0000 | [diff] [blame] | 1366 | |
| 1367 | // For now, assume that any C++ call can free memory. |
| 1368 | // TODO: If we want to be more optimistic here, we'll need to make sure that |
| 1369 | // regions escape to C++ containers. They seem to do that even now, but for |
| 1370 | // mysterious reasons. |
Jordan Rose | cde8cdb | 2012-07-02 19:27:56 +0000 | [diff] [blame] | 1371 | if (!(isa<FunctionCall>(Call) || isa<ObjCMethodCall>(Call))) |
Anna Zaks | 3cd89ad | 2012-02-24 23:56:53 +0000 | [diff] [blame] | 1372 | return false; |
| 1373 | |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1374 | // Check Objective-C messages by selector name. |
Jordan Rose | cde8cdb | 2012-07-02 19:27:56 +0000 | [diff] [blame] | 1375 | if (const ObjCMethodCall *Msg = dyn_cast<ObjCMethodCall>(Call)) { |
Jordan Rose | 85d7e01 | 2012-07-02 19:27:51 +0000 | [diff] [blame] | 1376 | // If it's not a framework call, or if it takes a callback, assume it |
| 1377 | // can free memory. |
| 1378 | if (!Call->isInSystemHeader() || Call->hasNonZeroCallbackArg()) |
Anna Zaks | 07d39a4 | 2012-02-28 01:54:22 +0000 | [diff] [blame] | 1379 | return false; |
| 1380 | |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1381 | Selector S = Msg->getSelector(); |
Anna Zaks | 52a0481 | 2012-06-20 23:35:57 +0000 | [diff] [blame] | 1382 | |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1383 | // Whitelist the ObjC methods which do free memory. |
Anna Zaks | 3cd89ad | 2012-02-24 23:56:53 +0000 | [diff] [blame] | 1384 | // - Anything containing 'freeWhenDone' param set to 1. |
| 1385 | // Ex: dataWithBytesNoCopy:length:freeWhenDone. |
Anna Zaks | 3e4f65d | 2012-06-22 22:08:09 +0000 | [diff] [blame] | 1386 | for (unsigned i = 1; i < S.getNumArgs(); ++i) { |
Anna Zaks | 3cd89ad | 2012-02-24 23:56:53 +0000 | [diff] [blame] | 1387 | if (S.getNameForSlot(i).equals("freeWhenDone")) { |
| 1388 | if (Call->getArgSVal(i).isConstant(1)) |
| 1389 | return false; |
Anna Zaks | fb7f76f | 2012-03-05 17:42:10 +0000 | [diff] [blame] | 1390 | else |
| 1391 | return true; |
Anna Zaks | 3cd89ad | 2012-02-24 23:56:53 +0000 | [diff] [blame] | 1392 | } |
| 1393 | } |
| 1394 | |
Anna Zaks | fb7f76f | 2012-03-05 17:42:10 +0000 | [diff] [blame] | 1395 | // If the first selector ends with NoCopy, assume that the ownership is |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 1396 | // transferred as well. |
Anna Zaks | fb7f76f | 2012-03-05 17:42:10 +0000 | [diff] [blame] | 1397 | // Ex: [NSData dataWithBytesNoCopy:bytes length:10]; |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1398 | StringRef FirstSlot = S.getNameForSlot(0); |
| 1399 | if (FirstSlot.endswith("NoCopy")) |
Anna Zaks | fb7f76f | 2012-03-05 17:42:10 +0000 | [diff] [blame] | 1400 | return false; |
Anna Zaks | fb7f76f | 2012-03-05 17:42:10 +0000 | [diff] [blame] | 1401 | |
Anna Zaks | 5f75768 | 2012-06-19 05:10:32 +0000 | [diff] [blame] | 1402 | // If the first selector starts with addPointer, insertPointer, |
| 1403 | // or replacePointer, assume we are dealing with NSPointerArray or similar. |
| 1404 | // 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] | 1405 | // that the pointers get freed by following the container itself. |
| 1406 | if (FirstSlot.startswith("addPointer") || |
| 1407 | FirstSlot.startswith("insertPointer") || |
| 1408 | FirstSlot.startswith("replacePointer")) { |
Anna Zaks | 5f75768 | 2012-06-19 05:10:32 +0000 | [diff] [blame] | 1409 | return false; |
| 1410 | } |
| 1411 | |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1412 | // Otherwise, assume that the method does not free memory. |
| 1413 | // Most framework methods do not free memory. |
Anna Zaks | 3cd89ad | 2012-02-24 23:56:53 +0000 | [diff] [blame] | 1414 | return true; |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1417 | // At this point the only thing left to handle is straight function calls. |
| 1418 | const FunctionDecl *FD = cast<FunctionCall>(Call)->getDecl(); |
| 1419 | if (!FD) |
| 1420 | return false; |
Anna Zaks | 3cd89ad | 2012-02-24 23:56:53 +0000 | [diff] [blame] | 1421 | |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1422 | ASTContext &ASTC = State->getStateManager().getContext(); |
| 1423 | |
| 1424 | // If it's one of the allocation functions we can reason about, we model |
| 1425 | // its behavior explicitly. |
| 1426 | if (isMemFunction(FD, ASTC)) |
| 1427 | return true; |
| 1428 | |
| 1429 | // If it's not a system call, assume it frees memory. |
| 1430 | if (!Call->isInSystemHeader()) |
| 1431 | return false; |
| 1432 | |
| 1433 | // White list the system functions whose arguments escape. |
| 1434 | const IdentifierInfo *II = FD->getIdentifier(); |
| 1435 | if (!II) |
| 1436 | return false; |
| 1437 | StringRef FName = II->getName(); |
| 1438 | |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1439 | // White list the 'XXXNoCopy' CoreFoundation functions. |
Jordan Rose | 85d7e01 | 2012-07-02 19:27:51 +0000 | [diff] [blame] | 1440 | // We specifically check these before |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1441 | if (FName.endswith("NoCopy")) { |
| 1442 | // Look for the deallocator argument. We know that the memory ownership |
| 1443 | // is not transferred only if the deallocator argument is |
| 1444 | // 'kCFAllocatorNull'. |
| 1445 | for (unsigned i = 1; i < Call->getNumArgs(); ++i) { |
| 1446 | const Expr *ArgE = Call->getArgExpr(i)->IgnoreParenCasts(); |
| 1447 | if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(ArgE)) { |
| 1448 | StringRef DeallocatorName = DE->getFoundDecl()->getName(); |
| 1449 | if (DeallocatorName == "kCFAllocatorNull") |
| 1450 | return true; |
| 1451 | } |
| 1452 | } |
| 1453 | return false; |
| 1454 | } |
| 1455 | |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1456 | // Associating streams with malloced buffers. The pointer can escape if |
Jordan Rose | 85d7e01 | 2012-07-02 19:27:51 +0000 | [diff] [blame] | 1457 | // 'closefn' is specified (and if that function does free memory), |
| 1458 | // but it will not if closefn is not specified. |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1459 | // Currently, we do not inspect the 'closefn' function (PR12101). |
| 1460 | if (FName == "funopen") |
Jordan Rose | 85d7e01 | 2012-07-02 19:27:51 +0000 | [diff] [blame] | 1461 | if (Call->getNumArgs() >= 4 && Call->getArgSVal(4).isConstant(0)) |
| 1462 | return true; |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1463 | |
| 1464 | // Do not warn on pointers passed to 'setbuf' when used with std streams, |
| 1465 | // these leaks might be intentional when setting the buffer for stdio. |
| 1466 | // http://stackoverflow.com/questions/2671151/who-frees-setvbuf-buffer |
| 1467 | if (FName == "setbuf" || FName =="setbuffer" || |
| 1468 | FName == "setlinebuf" || FName == "setvbuf") { |
| 1469 | if (Call->getNumArgs() >= 1) { |
| 1470 | const Expr *ArgE = Call->getArgExpr(0)->IgnoreParenCasts(); |
| 1471 | if (const DeclRefExpr *ArgDRE = dyn_cast<DeclRefExpr>(ArgE)) |
| 1472 | if (const VarDecl *D = dyn_cast<VarDecl>(ArgDRE->getDecl())) |
| 1473 | if (D->getCanonicalDecl()->getName().find("std") != StringRef::npos) |
| 1474 | return false; |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | // A bunch of other functions which either take ownership of a pointer or |
| 1479 | // wrap the result up in a struct or object, meaning it can be freed later. |
| 1480 | // (See RetainCountChecker.) Not all the parameters here are invalidated, |
| 1481 | // but the Malloc checker cannot differentiate between them. The right way |
| 1482 | // of doing this would be to implement a pointer escapes callback. |
| 1483 | if (FName == "CGBitmapContextCreate" || |
| 1484 | FName == "CGBitmapContextCreateWithData" || |
| 1485 | FName == "CVPixelBufferCreateWithBytes" || |
| 1486 | FName == "CVPixelBufferCreateWithPlanarBytes" || |
| 1487 | FName == "OSAtomicEnqueue") { |
| 1488 | return false; |
| 1489 | } |
| 1490 | |
Jordan Rose | 85d7e01 | 2012-07-02 19:27:51 +0000 | [diff] [blame] | 1491 | // Handle cases where we know a buffer's /address/ can escape. |
| 1492 | // Note that the above checks handle some special cases where we know that |
| 1493 | // even though the address escapes, it's still our responsibility to free the |
| 1494 | // buffer. |
| 1495 | if (Call->argumentsMayEscape()) |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 1496 | return false; |
| 1497 | |
| 1498 | // Otherwise, assume that the function does not free memory. |
| 1499 | // Most system calls do not free the memory. |
| 1500 | return true; |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Anna Zaks | bf53dfa | 2012-12-20 00:38:25 +0000 | [diff] [blame] | 1503 | ProgramStateRef MallocChecker::checkPointerEscape(ProgramStateRef State, |
| 1504 | const InvalidatedSymbols &Escaped, |
Anna Zaks | 233e26a | 2013-02-07 23:05:43 +0000 | [diff] [blame] | 1505 | const CallEvent *Call, |
| 1506 | PointerEscapeKind Kind) const { |
Anna Zaks | bf53dfa | 2012-12-20 00:38:25 +0000 | [diff] [blame] | 1507 | // If we know that the call does not free memory, keep tracking the top |
| 1508 | // level arguments. |
Anna Zaks | 233e26a | 2013-02-07 23:05:43 +0000 | [diff] [blame] | 1509 | if ((Kind == PSK_DirectEscapeOnCall || |
| 1510 | Kind == PSK_IndirectEscapeOnCall) && |
| 1511 | doesNotFreeMemory(Call, State)) { |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1512 | return State; |
Anna Zaks | 233e26a | 2013-02-07 23:05:43 +0000 | [diff] [blame] | 1513 | } |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1514 | |
Anna Zaks | bf53dfa | 2012-12-20 00:38:25 +0000 | [diff] [blame] | 1515 | for (InvalidatedSymbols::const_iterator I = Escaped.begin(), |
| 1516 | E = Escaped.end(); |
| 1517 | I != E; ++I) { |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 1518 | SymbolRef sym = *I; |
Anna Zaks | bf53dfa | 2012-12-20 00:38:25 +0000 | [diff] [blame] | 1519 | |
Anna Zaks | 5b7aa34 | 2012-06-22 02:04:31 +0000 | [diff] [blame] | 1520 | if (const RefState *RS = State->get<RegionState>(sym)) { |
| 1521 | if (RS->isAllocated()) |
Anna Zaks | 431e35c | 2012-08-09 00:42:24 +0000 | [diff] [blame] | 1522 | State = State->remove<RegionState>(sym); |
Anna Zaks | 5b7aa34 | 2012-06-22 02:04:31 +0000 | [diff] [blame] | 1523 | } |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 1524 | } |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1525 | return State; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1526 | } |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 1527 | |
Jordy Rose | 393f98b | 2012-03-18 07:43:35 +0000 | [diff] [blame] | 1528 | static SymbolRef findFailedReallocSymbol(ProgramStateRef currState, |
| 1529 | ProgramStateRef prevState) { |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 1530 | ReallocPairsTy currMap = currState->get<ReallocPairs>(); |
| 1531 | ReallocPairsTy prevMap = prevState->get<ReallocPairs>(); |
Jordy Rose | 393f98b | 2012-03-18 07:43:35 +0000 | [diff] [blame] | 1532 | |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 1533 | for (ReallocPairsTy::iterator I = prevMap.begin(), E = prevMap.end(); |
Jordy Rose | 393f98b | 2012-03-18 07:43:35 +0000 | [diff] [blame] | 1534 | I != E; ++I) { |
| 1535 | SymbolRef sym = I.getKey(); |
| 1536 | if (!currMap.lookup(sym)) |
| 1537 | return sym; |
| 1538 | } |
| 1539 | |
| 1540 | return NULL; |
| 1541 | } |
| 1542 | |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 1543 | PathDiagnosticPiece * |
| 1544 | MallocChecker::MallocBugVisitor::VisitNode(const ExplodedNode *N, |
| 1545 | const ExplodedNode *PrevN, |
| 1546 | BugReporterContext &BRC, |
| 1547 | BugReport &BR) { |
Jordy Rose | 393f98b | 2012-03-18 07:43:35 +0000 | [diff] [blame] | 1548 | ProgramStateRef state = N->getState(); |
| 1549 | ProgramStateRef statePrev = PrevN->getState(); |
| 1550 | |
| 1551 | const RefState *RS = state->get<RegionState>(Sym); |
| 1552 | const RefState *RSPrev = statePrev->get<RegionState>(Sym); |
Anna Zaks | ede875b | 2012-08-03 18:30:18 +0000 | [diff] [blame] | 1553 | if (!RS) |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 1554 | return 0; |
| 1555 | |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 1556 | const Stmt *S = 0; |
| 1557 | const char *Msg = 0; |
Anna Zaks | 56a938f | 2012-03-16 23:24:20 +0000 | [diff] [blame] | 1558 | StackHintGeneratorForSymbol *StackHint = 0; |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 1559 | |
| 1560 | // Retrieve the associated statement. |
| 1561 | ProgramPoint ProgLoc = N->getLocation(); |
Ted Kremenek | a4a1759 | 2013-01-04 19:04:36 +0000 | [diff] [blame] | 1562 | if (StmtPoint *SP = dyn_cast<StmtPoint>(&ProgLoc)) { |
Jordan Rose | 852aa0d | 2012-07-10 22:07:52 +0000 | [diff] [blame] | 1563 | S = SP->getStmt(); |
Ted Kremenek | a4a1759 | 2013-01-04 19:04:36 +0000 | [diff] [blame] | 1564 | } else if (CallExitEnd *Exit = dyn_cast<CallExitEnd>(&ProgLoc)) { |
Jordan Rose | 852aa0d | 2012-07-10 22:07:52 +0000 | [diff] [blame] | 1565 | S = Exit->getCalleeContext()->getCallSite(); |
Ted Kremenek | a4a1759 | 2013-01-04 19:04:36 +0000 | [diff] [blame] | 1566 | } else if (BlockEdge *Edge = dyn_cast<BlockEdge>(&ProgLoc)) { |
| 1567 | // If an assumption was made on a branch, it should be caught |
| 1568 | // here by looking at the state transition. |
| 1569 | S = Edge->getSrc()->getTerminator(); |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 1570 | } |
Ted Kremenek | a4a1759 | 2013-01-04 19:04:36 +0000 | [diff] [blame] | 1571 | |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 1572 | if (!S) |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 1573 | return 0; |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 1574 | |
Jordan Rose | 28038f3 | 2012-07-10 22:07:42 +0000 | [diff] [blame] | 1575 | // FIXME: We will eventually need to handle non-statement-based events |
| 1576 | // (__attribute__((cleanup))). |
| 1577 | |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 1578 | // 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] | 1579 | if (Mode == Normal) { |
Anna Zaks | 368a0d5 | 2012-03-15 21:13:02 +0000 | [diff] [blame] | 1580 | if (isAllocated(RS, RSPrev, S)) { |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 1581 | Msg = "Memory is allocated"; |
Anna Zaks | fbd5874 | 2012-03-16 23:44:28 +0000 | [diff] [blame] | 1582 | StackHint = new StackHintGeneratorForSymbol(Sym, |
| 1583 | "Returned allocated memory"); |
Anna Zaks | 368a0d5 | 2012-03-15 21:13:02 +0000 | [diff] [blame] | 1584 | } else if (isReleased(RS, RSPrev, S)) { |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 1585 | Msg = "Memory is released"; |
Anna Zaks | fbd5874 | 2012-03-16 23:44:28 +0000 | [diff] [blame] | 1586 | StackHint = new StackHintGeneratorForSymbol(Sym, |
| 1587 | "Returned released memory"); |
Anna Zaks | 5b7aa34 | 2012-06-22 02:04:31 +0000 | [diff] [blame] | 1588 | } else if (isRelinquished(RS, RSPrev, S)) { |
| 1589 | Msg = "Memory ownership is transfered"; |
| 1590 | StackHint = new StackHintGeneratorForSymbol(Sym, ""); |
Anna Zaks | 368a0d5 | 2012-03-15 21:13:02 +0000 | [diff] [blame] | 1591 | } else if (isReallocFailedCheck(RS, RSPrev, S)) { |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 1592 | Mode = ReallocationFailed; |
| 1593 | Msg = "Reallocation failed"; |
Anna Zaks | 56a938f | 2012-03-16 23:24:20 +0000 | [diff] [blame] | 1594 | StackHint = new StackHintGeneratorForReallocationFailed(Sym, |
Anna Zaks | fbd5874 | 2012-03-16 23:44:28 +0000 | [diff] [blame] | 1595 | "Reallocation failed"); |
Jordy Rose | 393f98b | 2012-03-18 07:43:35 +0000 | [diff] [blame] | 1596 | |
Jordy Rose | b000fb5 | 2012-03-24 03:15:09 +0000 | [diff] [blame] | 1597 | if (SymbolRef sym = findFailedReallocSymbol(state, statePrev)) { |
| 1598 | // Is it possible to fail two reallocs WITHOUT testing in between? |
| 1599 | assert((!FailedReallocSymbol || FailedReallocSymbol == sym) && |
| 1600 | "We only support one failed realloc at a time."); |
Jordy Rose | 393f98b | 2012-03-18 07:43:35 +0000 | [diff] [blame] | 1601 | BR.markInteresting(sym); |
Jordy Rose | b000fb5 | 2012-03-24 03:15:09 +0000 | [diff] [blame] | 1602 | FailedReallocSymbol = sym; |
| 1603 | } |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | // We are in a special mode if a reallocation failed later in the path. |
| 1607 | } else if (Mode == ReallocationFailed) { |
Jordy Rose | b000fb5 | 2012-03-24 03:15:09 +0000 | [diff] [blame] | 1608 | assert(FailedReallocSymbol && "No symbol to look for."); |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 1609 | |
Jordy Rose | b000fb5 | 2012-03-24 03:15:09 +0000 | [diff] [blame] | 1610 | // Is this is the first appearance of the reallocated symbol? |
| 1611 | if (!statePrev->get<RegionState>(FailedReallocSymbol)) { |
Jordy Rose | b000fb5 | 2012-03-24 03:15:09 +0000 | [diff] [blame] | 1612 | // We're at the reallocation point. |
| 1613 | Msg = "Attempt to reallocate memory"; |
| 1614 | StackHint = new StackHintGeneratorForSymbol(Sym, |
| 1615 | "Returned reallocated memory"); |
| 1616 | FailedReallocSymbol = NULL; |
| 1617 | Mode = Normal; |
| 1618 | } |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 1621 | if (!Msg) |
| 1622 | return 0; |
Anna Zaks | 56a938f | 2012-03-16 23:24:20 +0000 | [diff] [blame] | 1623 | assert(StackHint); |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 1624 | |
| 1625 | // Generate the extra diagnostic. |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 1626 | PathDiagnosticLocation Pos(S, BRC.getSourceManager(), |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 1627 | N->getLocationContext()); |
Anna Zaks | 56a938f | 2012-03-16 23:24:20 +0000 | [diff] [blame] | 1628 | return new PathDiagnosticEventPiece(Pos, Msg, true, StackHint); |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Anna Zaks | 93c5a24 | 2012-05-02 00:05:20 +0000 | [diff] [blame] | 1631 | void MallocChecker::printState(raw_ostream &Out, ProgramStateRef State, |
| 1632 | const char *NL, const char *Sep) const { |
| 1633 | |
| 1634 | RegionStateTy RS = State->get<RegionState>(); |
| 1635 | |
Ted Kremenek | c37fad6 | 2013-01-03 01:30:12 +0000 | [diff] [blame] | 1636 | if (!RS.isEmpty()) { |
| 1637 | Out << Sep << "MallocChecker:" << NL; |
| 1638 | for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) { |
| 1639 | I.getKey()->dumpToStream(Out); |
| 1640 | Out << " : "; |
| 1641 | I.getData().dump(Out); |
| 1642 | Out << NL; |
| 1643 | } |
| 1644 | } |
Anna Zaks | 93c5a24 | 2012-05-02 00:05:20 +0000 | [diff] [blame] | 1645 | } |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 1646 | |
Anna Zaks | 231361a | 2012-02-08 23:16:52 +0000 | [diff] [blame] | 1647 | #define REGISTER_CHECKER(name) \ |
| 1648 | void ento::register##name(CheckerManager &mgr) {\ |
Anna Zaks | f0dfc9c | 2012-02-17 22:35:31 +0000 | [diff] [blame] | 1649 | registerCStringCheckerBasic(mgr); \ |
Anna Zaks | 231361a | 2012-02-08 23:16:52 +0000 | [diff] [blame] | 1650 | mgr.registerChecker<MallocChecker>()->Filter.C##name = true;\ |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 1651 | } |
Anna Zaks | 231361a | 2012-02-08 23:16:52 +0000 | [diff] [blame] | 1652 | |
| 1653 | REGISTER_CHECKER(MallocPessimistic) |
| 1654 | REGISTER_CHECKER(MallocOptimistic) |