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