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