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