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" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 18 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" |
| 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h" |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/ImmutableMap.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 00bd44d | 2012-02-04 12:31:12 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/STLExtras.h" |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 26 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 27 | using namespace ento; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | |
Zhongxing Xu | 7fb1464 | 2009-12-11 00:55:44 +0000 | [diff] [blame] | 31 | class RefState { |
Ted Kremenek | dde201b | 2010-08-06 21:12:55 +0000 | [diff] [blame] | 32 | enum Kind { AllocateUnchecked, AllocateFailed, Released, Escaped, |
| 33 | Relinquished } K; |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 34 | const Stmt *S; |
| 35 | |
Zhongxing Xu | 7fb1464 | 2009-12-11 00:55:44 +0000 | [diff] [blame] | 36 | public: |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 37 | RefState(Kind k, const Stmt *s) : K(k), S(s) {} |
| 38 | |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 39 | bool isAllocated() const { return K == AllocateUnchecked; } |
Chris Lattner | fae9622 | 2010-09-03 04:34:38 +0000 | [diff] [blame] | 40 | //bool isFailed() const { return K == AllocateFailed; } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 41 | bool isReleased() const { return K == Released; } |
Chris Lattner | fae9622 | 2010-09-03 04:34:38 +0000 | [diff] [blame] | 42 | //bool isEscaped() const { return K == Escaped; } |
| 43 | //bool isRelinquished() const { return K == Relinquished; } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 44 | |
| 45 | bool operator==(const RefState &X) const { |
| 46 | return K == X.K && S == X.S; |
| 47 | } |
| 48 | |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 49 | static RefState getAllocateUnchecked(const Stmt *s) { |
| 50 | return RefState(AllocateUnchecked, s); |
| 51 | } |
| 52 | static RefState getAllocateFailed() { |
| 53 | return RefState(AllocateFailed, 0); |
| 54 | } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 55 | static RefState getReleased(const Stmt *s) { return RefState(Released, s); } |
| 56 | static RefState getEscaped(const Stmt *s) { return RefState(Escaped, s); } |
Ted Kremenek | dde201b | 2010-08-06 21:12:55 +0000 | [diff] [blame] | 57 | static RefState getRelinquished(const Stmt *s) { |
| 58 | return RefState(Relinquished, s); |
| 59 | } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 60 | |
| 61 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 62 | ID.AddInteger(K); |
| 63 | ID.AddPointer(S); |
| 64 | } |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 67 | class RegionState {}; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 68 | |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 69 | class MallocChecker : public Checker<check::DeadSymbols, |
Ted Kremenek | e3659a7 | 2012-01-04 23:48:37 +0000 | [diff] [blame] | 70 | check::EndPath, |
| 71 | check::PreStmt<ReturnStmt>, |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 72 | check::PostStmt<CallExpr>, |
Ted Kremenek | e3659a7 | 2012-01-04 23:48:37 +0000 | [diff] [blame] | 73 | check::Location, |
| 74 | check::Bind, |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 75 | eval::Assume, |
| 76 | check::RegionChanges> |
Ted Kremenek | e3659a7 | 2012-01-04 23:48:37 +0000 | [diff] [blame] | 77 | { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 78 | mutable OwningPtr<BuiltinBug> BT_DoubleFree; |
| 79 | mutable OwningPtr<BuiltinBug> BT_Leak; |
| 80 | mutable OwningPtr<BuiltinBug> BT_UseFree; |
| 81 | mutable OwningPtr<BuiltinBug> BT_UseRelinquished; |
| 82 | mutable OwningPtr<BuiltinBug> BT_BadFree; |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 83 | mutable IdentifierInfo *II_malloc, *II_free, *II_realloc, *II_calloc; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 84 | |
| 85 | public: |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 86 | MallocChecker() : II_malloc(0), II_free(0), II_realloc(0), II_calloc(0) {} |
Anna Zaks | 231361a | 2012-02-08 23:16:52 +0000 | [diff] [blame] | 87 | |
| 88 | /// In pessimistic mode, the checker assumes that it does not know which |
| 89 | /// functions might free the memory. |
| 90 | struct ChecksFilter { |
| 91 | DefaultBool CMallocPessimistic; |
| 92 | DefaultBool CMallocOptimistic; |
| 93 | }; |
| 94 | |
| 95 | ChecksFilter Filter; |
| 96 | |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 97 | void initIdentifierInfo(CheckerContext &C) const; |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 98 | |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 99 | void checkPostStmt(const CallExpr *CE, CheckerContext &C) const; |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 100 | void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const; |
Anna Zaks | af498a2 | 2011-10-25 19:56:48 +0000 | [diff] [blame] | 101 | void checkEndPath(CheckerContext &C) const; |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 102 | void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 103 | ProgramStateRef evalAssume(ProgramStateRef state, SVal Cond, |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 104 | bool Assumption) const; |
Anna Zaks | 390909c | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 105 | void checkLocation(SVal l, bool isLoad, const Stmt *S, |
| 106 | CheckerContext &C) const; |
| 107 | void checkBind(SVal location, SVal val, const Stmt*S, |
| 108 | CheckerContext &C) const; |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 109 | ProgramStateRef |
| 110 | checkRegionChanges(ProgramStateRef state, |
| 111 | const StoreManager::InvalidatedSymbols *invalidated, |
| 112 | ArrayRef<const MemRegion *> ExplicitRegions, |
| 113 | ArrayRef<const MemRegion *> Regions) const; |
| 114 | bool wantsRegionChangeUpdate(ProgramStateRef state) const { |
| 115 | return true; |
| 116 | } |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 117 | |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 118 | private: |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 119 | static void MallocMem(CheckerContext &C, const CallExpr *CE); |
| 120 | static void MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE, |
| 121 | const OwnershipAttr* Att); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 122 | static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE, |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 123 | const Expr *SizeEx, SVal Init, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 124 | ProgramStateRef state) { |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 125 | return MallocMemAux(C, CE, |
| 126 | state->getSVal(SizeEx, C.getLocationContext()), |
| 127 | Init, state); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 128 | } |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 129 | static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE, |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 130 | SVal SizeEx, SVal Init, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 131 | ProgramStateRef state); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 132 | |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 133 | void FreeMem(CheckerContext &C, const CallExpr *CE) const; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 134 | void FreeMemAttr(CheckerContext &C, const CallExpr *CE, |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 135 | const OwnershipAttr* Att) const; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 136 | ProgramStateRef FreeMemAux(CheckerContext &C, const CallExpr *CE, |
| 137 | ProgramStateRef state, unsigned Num, |
Ted Kremenek | e3659a7 | 2012-01-04 23:48:37 +0000 | [diff] [blame] | 138 | bool Hold) const; |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 139 | |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 140 | void ReallocMem(CheckerContext &C, const CallExpr *CE) const; |
| 141 | static void CallocMem(CheckerContext &C, const CallExpr *CE); |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 142 | |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 143 | bool checkEscape(SymbolRef Sym, const Stmt *S, CheckerContext &C) const; |
| 144 | bool checkUseAfterFree(SymbolRef Sym, CheckerContext &C, |
| 145 | const Stmt *S = 0) const; |
| 146 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 147 | static bool SummarizeValue(raw_ostream &os, SVal V); |
| 148 | static bool SummarizeRegion(raw_ostream &os, const MemRegion *MR); |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 149 | void ReportBadFree(CheckerContext &C, SVal ArgVal, SourceRange range) const; |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 150 | |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 151 | void reportLeak(SymbolRef Sym, ExplodedNode *N, CheckerContext &C) const; |
| 152 | |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 153 | /// The bug visitor which allows us to print extra diagnostics along the |
| 154 | /// BugReport path. For example, showing the allocation site of the leaked |
| 155 | /// region. |
| 156 | class MallocBugVisitor : public BugReporterVisitor { |
| 157 | protected: |
| 158 | // The allocated region symbol tracked by the main analysis. |
| 159 | SymbolRef Sym; |
| 160 | |
| 161 | public: |
| 162 | MallocBugVisitor(SymbolRef S) : Sym(S) {} |
| 163 | virtual ~MallocBugVisitor() {} |
| 164 | |
| 165 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 166 | static int X = 0; |
| 167 | ID.AddPointer(&X); |
| 168 | ID.AddPointer(Sym); |
| 169 | } |
| 170 | |
| 171 | inline bool isAllocated(const RefState *S, const RefState *SPrev) { |
| 172 | // Did not track -> allocated. Other state (released) -> allocated. |
| 173 | return ((S && S->isAllocated()) && (!SPrev || !SPrev->isAllocated())); |
| 174 | } |
| 175 | |
| 176 | inline bool isReleased(const RefState *S, const RefState *SPrev) { |
| 177 | // Did not track -> released. Other state (allocated) -> released. |
| 178 | return ((S && S->isReleased()) && (!SPrev || !SPrev->isReleased())); |
| 179 | } |
| 180 | |
| 181 | PathDiagnosticPiece *VisitNode(const ExplodedNode *N, |
| 182 | const ExplodedNode *PrevN, |
| 183 | BugReporterContext &BRC, |
| 184 | BugReport &BR); |
| 185 | }; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 186 | }; |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 187 | } // end anonymous namespace |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 188 | |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 189 | typedef llvm::ImmutableMap<SymbolRef, RefState> RegionStateTy; |
| 190 | |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 191 | namespace clang { |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 192 | namespace ento { |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 193 | template <> |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 194 | struct ProgramStateTrait<RegionState> |
| 195 | : public ProgramStatePartialTrait<RegionStateTy> { |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 196 | static void *GDMIndex() { static int x; return &x; } |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 197 | }; |
| 198 | } |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 199 | } |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 200 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 201 | namespace { |
| 202 | class StopTrackingCallback : public SymbolVisitor { |
| 203 | ProgramStateRef state; |
| 204 | public: |
| 205 | StopTrackingCallback(ProgramStateRef st) : state(st) {} |
| 206 | ProgramStateRef getState() const { return state; } |
| 207 | |
| 208 | bool VisitSymbol(SymbolRef sym) { |
| 209 | state = state->remove<RegionState>(sym); |
| 210 | return true; |
| 211 | } |
| 212 | }; |
| 213 | } // end anonymous namespace |
| 214 | |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 215 | void MallocChecker::initIdentifierInfo(CheckerContext &C) const { |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 216 | ASTContext &Ctx = C.getASTContext(); |
| 217 | if (!II_malloc) |
| 218 | II_malloc = &Ctx.Idents.get("malloc"); |
| 219 | if (!II_free) |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 220 | II_free = &Ctx.Idents.get("free"); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 221 | if (!II_realloc) |
| 222 | II_realloc = &Ctx.Idents.get("realloc"); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 223 | if (!II_calloc) |
| 224 | II_calloc = &Ctx.Idents.get("calloc"); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void MallocChecker::checkPostStmt(const CallExpr *CE, CheckerContext &C) const { |
| 228 | const FunctionDecl *FD = C.getCalleeDecl(CE); |
| 229 | if (!FD) |
| 230 | return; |
| 231 | initIdentifierInfo(C); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 232 | |
| 233 | if (FD->getIdentifier() == II_malloc) { |
| 234 | MallocMem(C, CE); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 235 | return; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 236 | } |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 237 | if (FD->getIdentifier() == II_realloc) { |
| 238 | ReallocMem(C, CE); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 239 | return; |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 242 | if (FD->getIdentifier() == II_calloc) { |
| 243 | CallocMem(C, CE); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 244 | return; |
| 245 | } |
| 246 | |
| 247 | if (FD->getIdentifier() == II_free) { |
| 248 | FreeMem(C, CE); |
| 249 | return; |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 252 | if (Filter.CMallocOptimistic) |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 253 | // Check all the attributes, if there are any. |
| 254 | // There can be multiple of these attributes. |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 255 | if (FD->hasAttrs()) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 256 | for (specific_attr_iterator<OwnershipAttr> |
| 257 | i = FD->specific_attr_begin<OwnershipAttr>(), |
| 258 | e = FD->specific_attr_end<OwnershipAttr>(); |
| 259 | i != e; ++i) { |
| 260 | switch ((*i)->getOwnKind()) { |
| 261 | case OwnershipAttr::Returns: { |
| 262 | MallocMemReturnsAttr(C, CE, *i); |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 263 | break; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 264 | } |
| 265 | case OwnershipAttr::Takes: |
| 266 | case OwnershipAttr::Holds: { |
| 267 | FreeMemAttr(C, CE, *i); |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 268 | break; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 269 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 273 | |
| 274 | if (Filter.CMallocPessimistic) { |
| 275 | ProgramStateRef State = C.getState(); |
| 276 | // The pointer might escape through a function call. |
| 277 | for (CallExpr::const_arg_iterator I = CE->arg_begin(), |
| 278 | E = CE->arg_end(); I != E; ++I) { |
| 279 | const Expr *A = *I; |
| 280 | if (A->getType().getTypePtr()->isAnyPointerType()) { |
| 281 | SymbolRef Sym = State->getSVal(A, C.getLocationContext()).getAsSymbol(); |
| 282 | if (!Sym) |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 283 | continue; |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 284 | checkEscape(Sym, A, C); |
| 285 | checkUseAfterFree(Sym, C, A); |
| 286 | } |
| 287 | } |
| 288 | } |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | void MallocChecker::MallocMem(CheckerContext &C, const CallExpr *CE) { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 292 | ProgramStateRef state = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 293 | C.getState()); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 294 | C.addTransition(state); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 297 | void MallocChecker::MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE, |
| 298 | const OwnershipAttr* Att) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 299 | if (Att->getModule() != "malloc") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 300 | return; |
| 301 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 302 | OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 303 | if (I != E) { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 304 | ProgramStateRef state = |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 305 | MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), C.getState()); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 306 | C.addTransition(state); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 307 | return; |
| 308 | } |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 309 | ProgramStateRef state = MallocMemAux(C, CE, UnknownVal(), UndefinedVal(), |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 310 | C.getState()); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 311 | C.addTransition(state); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 314 | ProgramStateRef MallocChecker::MallocMemAux(CheckerContext &C, |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 315 | const CallExpr *CE, |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 316 | SVal Size, SVal Init, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 317 | ProgramStateRef state) { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 318 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Zhongxing Xu | a49c6b7 | 2009-12-11 03:09:01 +0000 | [diff] [blame] | 319 | |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 320 | // Get the return value. |
| 321 | SVal retVal = state->getSVal(CE, C.getLocationContext()); |
Zhongxing Xu | a49c6b7 | 2009-12-11 03:09:01 +0000 | [diff] [blame] | 322 | |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 323 | // Fill the region with the initialization value. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 324 | state = state->bindDefault(retVal, Init); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 325 | |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 326 | // Set the region's extent equal to the Size parameter. |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 327 | const SymbolicRegion *R = |
| 328 | dyn_cast_or_null<SymbolicRegion>(retVal.getAsRegion()); |
| 329 | if (!R || !isa<DefinedOrUnknownSVal>(Size)) |
| 330 | return 0; |
| 331 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 332 | DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder); |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 333 | DefinedOrUnknownSVal DefinedSize = cast<DefinedOrUnknownSVal>(Size); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 334 | DefinedOrUnknownSVal extentMatchesSize = |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 335 | svalBuilder.evalEQ(state, Extent, DefinedSize); |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 336 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 337 | state = state->assume(extentMatchesSize, true); |
| 338 | assert(state); |
| 339 | |
| 340 | SymbolRef Sym = retVal.getAsLocSymbol(); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 341 | assert(Sym); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 342 | |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 343 | // Set the symbol's state to Allocated. |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 344 | return state->set<RegionState>(Sym, RefState::getAllocateUnchecked(CE)); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 347 | void MallocChecker::FreeMem(CheckerContext &C, const CallExpr *CE) const { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 348 | ProgramStateRef state = FreeMemAux(C, CE, C.getState(), 0, false); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 349 | |
| 350 | if (state) |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 351 | C.addTransition(state); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 354 | void MallocChecker::FreeMemAttr(CheckerContext &C, const CallExpr *CE, |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 355 | const OwnershipAttr* Att) const { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 356 | if (Att->getModule() != "malloc") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 357 | return; |
| 358 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 359 | for (OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end(); |
| 360 | I != E; ++I) { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 361 | ProgramStateRef state = |
Ted Kremenek | e3659a7 | 2012-01-04 23:48:37 +0000 | [diff] [blame] | 362 | FreeMemAux(C, CE, C.getState(), *I, |
| 363 | Att->getOwnKind() == OwnershipAttr::Holds); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 364 | if (state) |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 365 | C.addTransition(state); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 369 | ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C, |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 370 | const CallExpr *CE, |
| 371 | ProgramStateRef state, |
| 372 | unsigned Num, |
| 373 | bool Hold) const { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 374 | const Expr *ArgExpr = CE->getArg(Num); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 375 | SVal ArgVal = state->getSVal(ArgExpr, C.getLocationContext()); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 376 | if (!isa<DefinedOrUnknownSVal>(ArgVal)) |
| 377 | return 0; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 378 | DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(ArgVal); |
| 379 | |
| 380 | // Check for null dereferences. |
| 381 | if (!isa<Loc>(location)) |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 382 | return 0; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 383 | |
| 384 | // FIXME: Technically using 'Assume' here can result in a path |
| 385 | // bifurcation. In such cases we need to return two states, not just one. |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 386 | ProgramStateRef notNullState, nullState; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 387 | llvm::tie(notNullState, nullState) = state->assume(location); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 388 | |
| 389 | // The explicit NULL case, no operation is performed. |
| 390 | if (nullState && !notNullState) |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 391 | return 0; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 392 | |
| 393 | assert(notNullState); |
| 394 | |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 395 | // Unknown values could easily be okay |
| 396 | // Undefined values are handled elsewhere |
| 397 | if (ArgVal.isUnknownOrUndef()) |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 398 | return 0; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 399 | |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 400 | const MemRegion *R = ArgVal.getAsRegion(); |
| 401 | |
| 402 | // Nonlocs can't be freed, of course. |
| 403 | // Non-region locations (labels and fixed addresses) also shouldn't be freed. |
| 404 | if (!R) { |
| 405 | ReportBadFree(C, ArgVal, ArgExpr->getSourceRange()); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 406 | return 0; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | R = R->StripCasts(); |
| 410 | |
| 411 | // Blocks might show up as heap data, but should not be free()d |
| 412 | if (isa<BlockDataRegion>(R)) { |
| 413 | ReportBadFree(C, ArgVal, ArgExpr->getSourceRange()); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 414 | return 0; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | const MemSpaceRegion *MS = R->getMemorySpace(); |
| 418 | |
| 419 | // Parameters, locals, statics, and globals shouldn't be freed. |
| 420 | if (!(isa<UnknownSpaceRegion>(MS) || isa<HeapSpaceRegion>(MS))) { |
| 421 | // FIXME: at the time this code was written, malloc() regions were |
| 422 | // represented by conjured symbols, which are all in UnknownSpaceRegion. |
| 423 | // This means that there isn't actually anything from HeapSpaceRegion |
| 424 | // that should be freed, even though we allow it here. |
| 425 | // Of course, free() can work on memory allocated outside the current |
| 426 | // function, so UnknownSpaceRegion is always a possibility. |
| 427 | // False negatives are better than false positives. |
| 428 | |
| 429 | ReportBadFree(C, ArgVal, ArgExpr->getSourceRange()); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 430 | return 0; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R); |
| 434 | // Various cases could lead to non-symbol values here. |
| 435 | // For now, ignore them. |
| 436 | if (!SR) |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 437 | return 0; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 438 | |
| 439 | SymbolRef Sym = SR->getSymbol(); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 440 | const RefState *RS = state->get<RegionState>(Sym); |
Zhongxing Xu | 7e3cda9 | 2010-01-18 03:27:34 +0000 | [diff] [blame] | 441 | |
| 442 | // If the symbol has not been tracked, return. This is possible when free() is |
| 443 | // called on a pointer that does not get its pointee directly from malloc(). |
| 444 | // Full support of this requires inter-procedural analysis. |
| 445 | if (!RS) |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 446 | return 0; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 447 | |
| 448 | // Check double free. |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 449 | if (RS->isReleased()) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 450 | if (ExplodedNode *N = C.generateSink()) { |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 451 | if (!BT_DoubleFree) |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 452 | BT_DoubleFree.reset( |
| 453 | new BuiltinBug("Double free", |
| 454 | "Try to free a memory block that has been released")); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 455 | BugReport *R = new BugReport(*BT_DoubleFree, |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 456 | BT_DoubleFree->getDescription(), N); |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 457 | R->addVisitor(new MallocBugVisitor(Sym)); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 458 | C.EmitReport(R); |
| 459 | } |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 460 | return 0; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | // Normal free. |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 464 | if (Hold) |
| 465 | return notNullState->set<RegionState>(Sym, RefState::getRelinquished(CE)); |
| 466 | return notNullState->set<RegionState>(Sym, RefState::getReleased(CE)); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 469 | bool MallocChecker::SummarizeValue(raw_ostream &os, SVal V) { |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 470 | if (nonloc::ConcreteInt *IntVal = dyn_cast<nonloc::ConcreteInt>(&V)) |
| 471 | os << "an integer (" << IntVal->getValue() << ")"; |
| 472 | else if (loc::ConcreteInt *ConstAddr = dyn_cast<loc::ConcreteInt>(&V)) |
| 473 | os << "a constant address (" << ConstAddr->getValue() << ")"; |
| 474 | else if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&V)) |
Chris Lattner | 6810630 | 2011-02-17 05:38:27 +0000 | [diff] [blame] | 475 | os << "the address of the label '" << Label->getLabel()->getName() << "'"; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 476 | else |
| 477 | return false; |
| 478 | |
| 479 | return true; |
| 480 | } |
| 481 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 482 | bool MallocChecker::SummarizeRegion(raw_ostream &os, |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 483 | const MemRegion *MR) { |
| 484 | switch (MR->getKind()) { |
| 485 | case MemRegion::FunctionTextRegionKind: { |
| 486 | const FunctionDecl *FD = cast<FunctionTextRegion>(MR)->getDecl(); |
| 487 | if (FD) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 488 | os << "the address of the function '" << *FD << '\''; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 489 | else |
| 490 | os << "the address of a function"; |
| 491 | return true; |
| 492 | } |
| 493 | case MemRegion::BlockTextRegionKind: |
| 494 | os << "block text"; |
| 495 | return true; |
| 496 | case MemRegion::BlockDataRegionKind: |
| 497 | // FIXME: where the block came from? |
| 498 | os << "a block"; |
| 499 | return true; |
| 500 | default: { |
| 501 | const MemSpaceRegion *MS = MR->getMemorySpace(); |
| 502 | |
Anna Zaks | eb31a76 | 2012-01-04 23:54:01 +0000 | [diff] [blame] | 503 | if (isa<StackLocalsSpaceRegion>(MS)) { |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 504 | const VarRegion *VR = dyn_cast<VarRegion>(MR); |
| 505 | const VarDecl *VD; |
| 506 | if (VR) |
| 507 | VD = VR->getDecl(); |
| 508 | else |
| 509 | VD = NULL; |
| 510 | |
| 511 | if (VD) |
| 512 | os << "the address of the local variable '" << VD->getName() << "'"; |
| 513 | else |
| 514 | os << "the address of a local stack variable"; |
| 515 | return true; |
| 516 | } |
Anna Zaks | eb31a76 | 2012-01-04 23:54:01 +0000 | [diff] [blame] | 517 | |
| 518 | if (isa<StackArgumentsSpaceRegion>(MS)) { |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 519 | const VarRegion *VR = dyn_cast<VarRegion>(MR); |
| 520 | const VarDecl *VD; |
| 521 | if (VR) |
| 522 | VD = VR->getDecl(); |
| 523 | else |
| 524 | VD = NULL; |
| 525 | |
| 526 | if (VD) |
| 527 | os << "the address of the parameter '" << VD->getName() << "'"; |
| 528 | else |
| 529 | os << "the address of a parameter"; |
| 530 | return true; |
| 531 | } |
Anna Zaks | eb31a76 | 2012-01-04 23:54:01 +0000 | [diff] [blame] | 532 | |
| 533 | if (isa<GlobalsSpaceRegion>(MS)) { |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 534 | const VarRegion *VR = dyn_cast<VarRegion>(MR); |
| 535 | const VarDecl *VD; |
| 536 | if (VR) |
| 537 | VD = VR->getDecl(); |
| 538 | else |
| 539 | VD = NULL; |
| 540 | |
| 541 | if (VD) { |
| 542 | if (VD->isStaticLocal()) |
| 543 | os << "the address of the static variable '" << VD->getName() << "'"; |
| 544 | else |
| 545 | os << "the address of the global variable '" << VD->getName() << "'"; |
| 546 | } else |
| 547 | os << "the address of a global variable"; |
| 548 | return true; |
| 549 | } |
Anna Zaks | eb31a76 | 2012-01-04 23:54:01 +0000 | [diff] [blame] | 550 | |
| 551 | return false; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal, |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 557 | SourceRange range) const { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 558 | if (ExplodedNode *N = C.generateSink()) { |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 559 | if (!BT_BadFree) |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 560 | BT_BadFree.reset(new BuiltinBug("Bad free")); |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 561 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 562 | SmallString<100> buf; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 563 | llvm::raw_svector_ostream os(buf); |
| 564 | |
| 565 | const MemRegion *MR = ArgVal.getAsRegion(); |
| 566 | if (MR) { |
| 567 | while (const ElementRegion *ER = dyn_cast<ElementRegion>(MR)) |
| 568 | MR = ER->getSuperRegion(); |
| 569 | |
| 570 | // Special case for alloca() |
| 571 | if (isa<AllocaRegion>(MR)) |
| 572 | os << "Argument to free() was allocated by alloca(), not malloc()"; |
| 573 | else { |
| 574 | os << "Argument to free() is "; |
| 575 | if (SummarizeRegion(os, MR)) |
| 576 | os << ", which is not memory allocated by malloc()"; |
| 577 | else |
| 578 | os << "not memory allocated by malloc()"; |
| 579 | } |
| 580 | } else { |
| 581 | os << "Argument to free() is "; |
| 582 | if (SummarizeValue(os, ArgVal)) |
| 583 | os << ", which is not memory allocated by malloc()"; |
| 584 | else |
| 585 | os << "not memory allocated by malloc()"; |
| 586 | } |
| 587 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 588 | BugReport *R = new BugReport(*BT_BadFree, os.str(), N); |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 589 | R->addRange(range); |
| 590 | C.EmitReport(R); |
| 591 | } |
| 592 | } |
| 593 | |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 594 | void MallocChecker::ReallocMem(CheckerContext &C, const CallExpr *CE) const { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 595 | ProgramStateRef state = C.getState(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 596 | const Expr *arg0Expr = CE->getArg(0); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 597 | const LocationContext *LCtx = C.getLocationContext(); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 598 | SVal Arg0Val = state->getSVal(arg0Expr, LCtx); |
| 599 | if (!isa<DefinedOrUnknownSVal>(Arg0Val)) |
| 600 | return; |
| 601 | DefinedOrUnknownSVal arg0Val = cast<DefinedOrUnknownSVal>(Arg0Val); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 602 | |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 603 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 604 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 605 | DefinedOrUnknownSVal PtrEQ = |
| 606 | svalBuilder.evalEQ(state, arg0Val, svalBuilder.makeNull()); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 607 | |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 608 | // Get the size argument. If there is no size arg then give up. |
| 609 | const Expr *Arg1 = CE->getArg(1); |
| 610 | if (!Arg1) |
| 611 | return; |
| 612 | |
| 613 | // Get the value of the size argument. |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 614 | SVal Arg1ValG = state->getSVal(Arg1, LCtx); |
| 615 | if (!isa<DefinedOrUnknownSVal>(Arg1ValG)) |
| 616 | return; |
| 617 | DefinedOrUnknownSVal Arg1Val = cast<DefinedOrUnknownSVal>(Arg1ValG); |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 618 | |
| 619 | // Compare the size argument to 0. |
| 620 | DefinedOrUnknownSVal SizeZero = |
| 621 | svalBuilder.evalEQ(state, Arg1Val, |
| 622 | svalBuilder.makeIntValWithPtrWidth(0, false)); |
| 623 | |
| 624 | // If the ptr is NULL and the size is not 0, the call is equivalent to |
| 625 | // malloc(size). |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 626 | ProgramStateRef stateEqual = state->assume(PtrEQ, true); |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 627 | if (stateEqual && state->assume(SizeZero, false)) { |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 628 | // Hack: set the NULL symbolic region to released to suppress false warning. |
| 629 | // In the future we should add more states for allocated regions, e.g., |
| 630 | // CheckedNull, CheckedNonNull. |
| 631 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 632 | SymbolRef Sym = arg0Val.getAsLocSymbol(); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 633 | if (Sym) |
| 634 | stateEqual = stateEqual->set<RegionState>(Sym, RefState::getReleased(CE)); |
| 635 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 636 | ProgramStateRef stateMalloc = MallocMemAux(C, CE, CE->getArg(1), |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 637 | UndefinedVal(), stateEqual); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 638 | C.addTransition(stateMalloc); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 641 | if (ProgramStateRef stateNotEqual = state->assume(PtrEQ, false)) { |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 642 | // If the size is 0, free the memory. |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 643 | if (ProgramStateRef stateSizeZero = |
Ted Kremenek | e3659a7 | 2012-01-04 23:48:37 +0000 | [diff] [blame] | 644 | stateNotEqual->assume(SizeZero, true)) |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 645 | if (ProgramStateRef stateFree = |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 646 | FreeMemAux(C, CE, stateSizeZero, 0, false)) { |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 647 | |
Zhongxing Xu | d56763f | 2011-09-01 04:53:59 +0000 | [diff] [blame] | 648 | // Bind the return value to NULL because it is now free. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 649 | C.addTransition(stateFree->BindExpr(CE, LCtx, |
| 650 | svalBuilder.makeNull(), true)); |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 651 | } |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 652 | if (ProgramStateRef stateSizeNotZero = |
Ted Kremenek | e3659a7 | 2012-01-04 23:48:37 +0000 | [diff] [blame] | 653 | stateNotEqual->assume(SizeZero,false)) |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 654 | if (ProgramStateRef stateFree = FreeMemAux(C, CE, stateSizeNotZero, |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 655 | 0, false)) { |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 656 | // FIXME: We should copy the content of the original buffer. |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 657 | ProgramStateRef stateRealloc = MallocMemAux(C, CE, CE->getArg(1), |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 658 | UnknownVal(), stateFree); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 659 | C.addTransition(stateRealloc); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 660 | } |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 661 | } |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 662 | } |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 663 | |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 664 | void MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE) { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 665 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 666 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 667 | const LocationContext *LCtx = C.getLocationContext(); |
| 668 | SVal count = state->getSVal(CE->getArg(0), LCtx); |
| 669 | SVal elementSize = state->getSVal(CE->getArg(1), LCtx); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 670 | SVal TotalSize = svalBuilder.evalBinOp(state, BO_Mul, count, elementSize, |
| 671 | svalBuilder.getContext().getSizeType()); |
| 672 | SVal zeroVal = svalBuilder.makeZeroVal(svalBuilder.getContext().CharTy); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 673 | |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 674 | C.addTransition(MallocMemAux(C, CE, TotalSize, zeroVal, state)); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 677 | void MallocChecker::reportLeak(SymbolRef Sym, ExplodedNode *N, |
| 678 | CheckerContext &C) const { |
| 679 | assert(N); |
| 680 | if (!BT_Leak) { |
| 681 | BT_Leak.reset(new BuiltinBug("Memory leak", |
| 682 | "Allocated memory never released. Potential memory leak.")); |
| 683 | // Leaks should not be reported if they are post-dominated by a sink: |
| 684 | // (1) Sinks are higher importance bugs. |
| 685 | // (2) NoReturnFunctionChecker uses sink nodes to represent paths ending |
| 686 | // with __noreturn functions such as assert() or exit(). We choose not |
| 687 | // to report leaks on such paths. |
| 688 | BT_Leak->setSuppressOnSink(true); |
| 689 | } |
| 690 | |
| 691 | BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N); |
| 692 | R->addVisitor(new MallocBugVisitor(Sym)); |
| 693 | C.EmitReport(R); |
| 694 | } |
| 695 | |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 696 | void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper, |
| 697 | CheckerContext &C) const |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 698 | { |
Zhongxing Xu | 173ff56 | 2010-08-15 08:19:57 +0000 | [diff] [blame] | 699 | if (!SymReaper.hasDeadSymbols()) |
| 700 | return; |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 701 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 702 | ProgramStateRef state = C.getState(); |
Zhongxing Xu | 173ff56 | 2010-08-15 08:19:57 +0000 | [diff] [blame] | 703 | RegionStateTy RS = state->get<RegionState>(); |
Jordy Rose | 9076014 | 2010-08-18 04:33:47 +0000 | [diff] [blame] | 704 | RegionStateTy::Factory &F = state->get_context<RegionState>(); |
Zhongxing Xu | 173ff56 | 2010-08-15 08:19:57 +0000 | [diff] [blame] | 705 | |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 706 | bool generateReport = false; |
Anna Zaks | f8c17b7 | 2012-02-09 06:48:19 +0000 | [diff] [blame] | 707 | llvm::SmallVector<SymbolRef, 2> Errors; |
Zhongxing Xu | 173ff56 | 2010-08-15 08:19:57 +0000 | [diff] [blame] | 708 | for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) { |
| 709 | if (SymReaper.isDead(I->first)) { |
Anna Zaks | f8c17b7 | 2012-02-09 06:48:19 +0000 | [diff] [blame] | 710 | if (I->second.isAllocated()) { |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 711 | generateReport = true; |
Anna Zaks | f8c17b7 | 2012-02-09 06:48:19 +0000 | [diff] [blame] | 712 | Errors.push_back(I->first); |
| 713 | } |
Jordy Rose | 9076014 | 2010-08-18 04:33:47 +0000 | [diff] [blame] | 714 | // Remove the dead symbol from the map. |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 715 | RS = F.remove(RS, I->first); |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 716 | |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 719 | |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 720 | ExplodedNode *N = C.addTransition(state->set<RegionState>(RS)); |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 721 | |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 722 | if (N && generateReport) { |
Anna Zaks | f8c17b7 | 2012-02-09 06:48:19 +0000 | [diff] [blame] | 723 | for (llvm::SmallVector<SymbolRef, 2>::iterator |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 724 | I = Errors.begin(), E = Errors.end(); I != E; ++I) { |
| 725 | reportLeak(*I, N, C); |
Anna Zaks | f8c17b7 | 2012-02-09 06:48:19 +0000 | [diff] [blame] | 726 | } |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 727 | } |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 728 | } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 729 | |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 730 | void MallocChecker::checkEndPath(CheckerContext &C) const { |
| 731 | ProgramStateRef state = C.getState(); |
Jordy Rose | 09cef09 | 2010-08-18 04:26:59 +0000 | [diff] [blame] | 732 | RegionStateTy M = state->get<RegionState>(); |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 733 | |
Jordy Rose | 09cef09 | 2010-08-18 04:26:59 +0000 | [diff] [blame] | 734 | for (RegionStateTy::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 735 | RefState RS = I->second; |
| 736 | if (RS.isAllocated()) { |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 737 | ExplodedNode *N = C.addTransition(state); |
| 738 | if (N) |
| 739 | reportLeak(I->first, N, C); |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | } |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 743 | |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 744 | bool MallocChecker::checkEscape(SymbolRef Sym, const Stmt *S, |
| 745 | CheckerContext &C) const { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 746 | ProgramStateRef state = C.getState(); |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 747 | const RefState *RS = state->get<RegionState>(Sym); |
| 748 | if (!RS) |
| 749 | return false; |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 750 | |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 751 | if (RS->isAllocated()) { |
| 752 | state = state->set<RegionState>(Sym, RefState::getEscaped(S)); |
| 753 | C.addTransition(state); |
| 754 | return true; |
| 755 | } |
| 756 | return false; |
| 757 | } |
| 758 | |
| 759 | void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const { |
| 760 | const Expr *E = S->getRetValue(); |
| 761 | if (!E) |
| 762 | return; |
Anna Zaks | 0860cd0 | 2012-02-11 21:44:39 +0000 | [diff] [blame^] | 763 | |
| 764 | // Check if we are returning a symbol. |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 765 | SymbolRef Sym = C.getState()->getSVal(E, C.getLocationContext()).getAsSymbol(); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 766 | if (!Sym) |
| 767 | return; |
| 768 | |
Anna Zaks | 0860cd0 | 2012-02-11 21:44:39 +0000 | [diff] [blame^] | 769 | // Check if we are returning freed memory. |
| 770 | checkUseAfterFree(Sym, C, S); |
| 771 | |
| 772 | // Check if the symbol is escaping. |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 773 | checkEscape(Sym, S, C); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 774 | } |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 775 | |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 776 | bool MallocChecker::checkUseAfterFree(SymbolRef Sym, CheckerContext &C, |
| 777 | const Stmt *S) const { |
| 778 | assert(Sym); |
| 779 | const RefState *RS = C.getState()->get<RegionState>(Sym); |
| 780 | if (RS && RS->isReleased()) { |
| 781 | if (ExplodedNode *N = C.addTransition()) { |
| 782 | if (!BT_UseFree) |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 783 | BT_UseFree.reset(new BuiltinBug("Use of dynamically allocated memory " |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 784 | "after it is freed.")); |
| 785 | |
| 786 | BugReport *R = new BugReport(*BT_UseFree, BT_UseFree->getDescription(),N); |
| 787 | if (S) |
| 788 | R->addRange(S->getSourceRange()); |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 789 | R->addVisitor(new MallocBugVisitor(Sym)); |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 790 | C.EmitReport(R); |
| 791 | return true; |
| 792 | } |
| 793 | } |
| 794 | return false; |
| 795 | } |
| 796 | |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 797 | // Check if the location is a freed symbolic region. |
Anna Zaks | 390909c | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 798 | void MallocChecker::checkLocation(SVal l, bool isLoad, const Stmt *S, |
| 799 | CheckerContext &C) const { |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 800 | SymbolRef Sym = l.getLocSymbolInBase(); |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 801 | if (Sym) |
| 802 | checkUseAfterFree(Sym, C); |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 803 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 804 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 805 | //===----------------------------------------------------------------------===// |
| 806 | // Check various ways a symbol can be invalidated. |
| 807 | // TODO: This logic (the next 3 functions) is copied/similar to the |
| 808 | // RetainRelease checker. We might want to factor this out. |
| 809 | //===----------------------------------------------------------------------===// |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 810 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 811 | // Stop tracking symbols when a value escapes as a result of checkBind. |
| 812 | // A value escapes in three possible cases: |
| 813 | // (1) we are binding to something that is not a memory region. |
| 814 | // (2) we are binding to a memregion that does not have stack storage |
| 815 | // (3) we are binding to a memregion with stack storage that the store |
| 816 | // does not understand. |
| 817 | void MallocChecker::checkBind(SVal loc, SVal val, const Stmt *S, |
| 818 | CheckerContext &C) const { |
| 819 | // Are we storing to something that causes the value to "escape"? |
| 820 | bool escapes = true; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 821 | ProgramStateRef state = C.getState(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 822 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 823 | if (loc::MemRegionVal *regionLoc = dyn_cast<loc::MemRegionVal>(&loc)) { |
| 824 | escapes = !regionLoc->getRegion()->hasStackStorage(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 825 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 826 | if (!escapes) { |
| 827 | // To test (3), generate a new state with the binding added. If it is |
| 828 | // the same state, then it escapes (since the store cannot represent |
| 829 | // the binding). |
| 830 | escapes = (state == (state->bindLoc(*regionLoc, val))); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 831 | } |
| 832 | } |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 833 | |
| 834 | // If our store can represent the binding and we aren't storing to something |
| 835 | // that doesn't have local storage then just return and have the simulation |
| 836 | // state continue as is. |
| 837 | if (!escapes) |
| 838 | return; |
| 839 | |
| 840 | // Otherwise, find all symbols referenced by 'val' that we are tracking |
| 841 | // and stop tracking them. |
| 842 | state = state->scanReachableSymbols<StopTrackingCallback>(val).getState(); |
| 843 | C.addTransition(state); |
| 844 | } |
| 845 | |
| 846 | // If a symbolic region is assumed to NULL (or another constant), stop tracking |
| 847 | // it - assuming that allocation failed on this path. |
| 848 | ProgramStateRef MallocChecker::evalAssume(ProgramStateRef state, |
| 849 | SVal Cond, |
| 850 | bool Assumption) const { |
| 851 | RegionStateTy RS = state->get<RegionState>(); |
| 852 | |
| 853 | for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) { |
| 854 | // If the symbol is assumed to NULL or another constant, this will |
| 855 | // return an APSInt*. |
| 856 | if (state->getSymVal(I.getKey())) |
| 857 | state = state->remove<RegionState>(I.getKey()); |
| 858 | } |
| 859 | |
| 860 | return state; |
| 861 | } |
| 862 | |
| 863 | // If the symbol we are tracking is invalidated, but not explicitly (ex: the &p |
| 864 | // escapes, when we are tracking p), do not track the symbol as we cannot reason |
| 865 | // about it anymore. |
| 866 | ProgramStateRef |
| 867 | MallocChecker::checkRegionChanges(ProgramStateRef state, |
| 868 | const StoreManager::InvalidatedSymbols *invalidated, |
| 869 | ArrayRef<const MemRegion *> ExplicitRegions, |
| 870 | ArrayRef<const MemRegion *> Regions) const { |
| 871 | if (!invalidated) |
| 872 | return state; |
| 873 | |
| 874 | llvm::SmallPtrSet<SymbolRef, 8> WhitelistedSymbols; |
| 875 | for (ArrayRef<const MemRegion *>::iterator I = ExplicitRegions.begin(), |
| 876 | E = ExplicitRegions.end(); I != E; ++I) { |
| 877 | if (const SymbolicRegion *SR = (*I)->StripCasts()->getAs<SymbolicRegion>()) |
| 878 | WhitelistedSymbols.insert(SR->getSymbol()); |
| 879 | } |
| 880 | |
| 881 | for (StoreManager::InvalidatedSymbols::const_iterator I=invalidated->begin(), |
| 882 | E = invalidated->end(); I!=E; ++I) { |
| 883 | SymbolRef sym = *I; |
| 884 | if (WhitelistedSymbols.count(sym)) |
| 885 | continue; |
| 886 | // Don't track the symbol. |
| 887 | state = state->remove<RegionState>(sym); |
| 888 | } |
| 889 | return state; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 890 | } |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 891 | |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 892 | PathDiagnosticPiece * |
| 893 | MallocChecker::MallocBugVisitor::VisitNode(const ExplodedNode *N, |
| 894 | const ExplodedNode *PrevN, |
| 895 | BugReporterContext &BRC, |
| 896 | BugReport &BR) { |
| 897 | const RefState *RS = N->getState()->get<RegionState>(Sym); |
| 898 | const RefState *RSPrev = PrevN->getState()->get<RegionState>(Sym); |
| 899 | if (!RS && !RSPrev) |
| 900 | return 0; |
| 901 | |
| 902 | // We expect the interesting locations be StmtPoints corresponding to call |
| 903 | // expressions. We do not support indirect function calls as of now. |
| 904 | const CallExpr *CE = 0; |
| 905 | if (isa<StmtPoint>(N->getLocation())) |
| 906 | CE = dyn_cast<CallExpr>(cast<StmtPoint>(N->getLocation()).getStmt()); |
| 907 | if (!CE) |
| 908 | return 0; |
| 909 | const FunctionDecl *funDecl = CE->getDirectCallee(); |
| 910 | if (!funDecl) |
| 911 | return 0; |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 912 | |
| 913 | // Find out if this is an interesting point and what is the kind. |
| 914 | const char *Msg = 0; |
| 915 | if (isAllocated(RS, RSPrev)) |
| 916 | Msg = "Memory is allocated here"; |
| 917 | else if (isReleased(RS, RSPrev)) |
| 918 | Msg = "Memory is released here"; |
| 919 | if (!Msg) |
| 920 | return 0; |
| 921 | |
| 922 | // Generate the extra diagnostic. |
| 923 | PathDiagnosticLocation Pos(CE, BRC.getSourceManager(), |
| 924 | N->getLocationContext()); |
| 925 | return new PathDiagnosticEventPiece(Pos, Msg); |
| 926 | } |
| 927 | |
| 928 | |
Anna Zaks | 231361a | 2012-02-08 23:16:52 +0000 | [diff] [blame] | 929 | #define REGISTER_CHECKER(name) \ |
| 930 | void ento::register##name(CheckerManager &mgr) {\ |
| 931 | mgr.registerChecker<MallocChecker>()->Filter.C##name = true;\ |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 932 | } |
Anna Zaks | 231361a | 2012-02-08 23:16:52 +0000 | [diff] [blame] | 933 | |
| 934 | REGISTER_CHECKER(MallocPessimistic) |
| 935 | REGISTER_CHECKER(MallocOptimistic) |