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