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