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