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