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