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