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 { |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 27 | enum Kind { AllocateUnchecked, AllocateFailed, Released, Escaped } K; |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 28 | const Stmt *S; |
| 29 | |
Zhongxing Xu | 7fb1464 | 2009-12-11 00:55:44 +0000 | [diff] [blame] | 30 | public: |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 31 | RefState(Kind k, const Stmt *s) : K(k), S(s) {} |
| 32 | |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 33 | bool isAllocated() const { return K == AllocateUnchecked; } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 34 | bool isReleased() const { return K == Released; } |
| 35 | bool isEscaped() const { return K == Escaped; } |
| 36 | |
| 37 | bool operator==(const RefState &X) const { |
| 38 | return K == X.K && S == X.S; |
| 39 | } |
| 40 | |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 41 | static RefState getAllocateUnchecked(const Stmt *s) { |
| 42 | return RefState(AllocateUnchecked, s); |
| 43 | } |
| 44 | static RefState getAllocateFailed() { |
| 45 | return RefState(AllocateFailed, 0); |
| 46 | } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 47 | static RefState getReleased(const Stmt *s) { return RefState(Released, s); } |
| 48 | static RefState getEscaped(const Stmt *s) { return RefState(Escaped, s); } |
| 49 | |
| 50 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 51 | ID.AddInteger(K); |
| 52 | ID.AddPointer(S); |
| 53 | } |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 56 | class RegionState {}; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 57 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 58 | class MallocChecker : public CheckerVisitor<MallocChecker> { |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 59 | BuiltinBug *BT_DoubleFree; |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 60 | BuiltinBug *BT_Leak; |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 61 | BuiltinBug *BT_UseFree; |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 62 | IdentifierInfo *II_malloc, *II_free, *II_realloc; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 63 | |
| 64 | public: |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 65 | MallocChecker() |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 66 | : BT_DoubleFree(0), BT_Leak(0), BT_UseFree(0), |
| 67 | II_malloc(0), II_free(0), II_realloc(0) {} |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 68 | static void *getTag(); |
Zhongxing Xu | a49c6b7 | 2009-12-11 03:09:01 +0000 | [diff] [blame] | 69 | bool EvalCallExpr(CheckerContext &C, const CallExpr *CE); |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 70 | void EvalDeadSymbols(CheckerContext &C,const Stmt *S,SymbolReaper &SymReaper); |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 71 | void EvalEndPath(GREndPathNodeBuilder &B, void *tag, GRExprEngine &Eng); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 72 | void PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *S); |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 73 | const GRState *EvalAssume(const GRState *state, SVal Cond, bool Assumption); |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 74 | void VisitLocation(CheckerContext &C, const Stmt *S, SVal l); |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 75 | |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 76 | private: |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 77 | void MallocMem(CheckerContext &C, const CallExpr *CE); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 78 | const GRState *MallocMemAux(CheckerContext &C, const CallExpr *CE, |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 79 | const Expr *SizeEx, const GRState *state); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 80 | void FreeMem(CheckerContext &C, const CallExpr *CE); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 81 | const GRState *FreeMemAux(CheckerContext &C, const CallExpr *CE, |
| 82 | const GRState *state); |
| 83 | |
| 84 | void ReallocMem(CheckerContext &C, const CallExpr *CE); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 85 | }; |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 86 | } // end anonymous namespace |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 87 | |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 88 | typedef llvm::ImmutableMap<SymbolRef, RefState> RegionStateTy; |
| 89 | |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 90 | namespace clang { |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 91 | template <> |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 92 | struct GRStateTrait<RegionState> |
| 93 | : public GRStatePartialTrait<llvm::ImmutableMap<SymbolRef, RefState> > { |
| 94 | static void *GDMIndex() { return MallocChecker::getTag(); } |
| 95 | }; |
| 96 | } |
| 97 | |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 98 | void clang::RegisterMallocChecker(GRExprEngine &Eng) { |
| 99 | Eng.registerCheck(new MallocChecker()); |
| 100 | } |
| 101 | |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 102 | void *MallocChecker::getTag() { |
| 103 | static int x; |
| 104 | return &x; |
| 105 | } |
| 106 | |
Zhongxing Xu | a49c6b7 | 2009-12-11 03:09:01 +0000 | [diff] [blame] | 107 | bool MallocChecker::EvalCallExpr(CheckerContext &C, const CallExpr *CE) { |
| 108 | const GRState *state = C.getState(); |
| 109 | const Expr *Callee = CE->getCallee(); |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 110 | SVal L = state->getSVal(Callee); |
Zhongxing Xu | a49c6b7 | 2009-12-11 03:09:01 +0000 | [diff] [blame] | 111 | |
| 112 | const FunctionDecl *FD = L.getAsFunctionDecl(); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 113 | if (!FD) |
Zhongxing Xu | a49c6b7 | 2009-12-11 03:09:01 +0000 | [diff] [blame] | 114 | return false; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 115 | |
| 116 | ASTContext &Ctx = C.getASTContext(); |
| 117 | if (!II_malloc) |
| 118 | II_malloc = &Ctx.Idents.get("malloc"); |
| 119 | if (!II_free) |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 120 | II_free = &Ctx.Idents.get("free"); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 121 | if (!II_realloc) |
| 122 | II_realloc = &Ctx.Idents.get("realloc"); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 123 | |
| 124 | if (FD->getIdentifier() == II_malloc) { |
| 125 | MallocMem(C, CE); |
Zhongxing Xu | a49c6b7 | 2009-12-11 03:09:01 +0000 | [diff] [blame] | 126 | return true; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | if (FD->getIdentifier() == II_free) { |
| 130 | FreeMem(C, CE); |
Zhongxing Xu | a49c6b7 | 2009-12-11 03:09:01 +0000 | [diff] [blame] | 131 | return true; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 132 | } |
Zhongxing Xu | a49c6b7 | 2009-12-11 03:09:01 +0000 | [diff] [blame] | 133 | |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 134 | if (FD->getIdentifier() == II_realloc) { |
| 135 | ReallocMem(C, CE); |
| 136 | return true; |
| 137 | } |
| 138 | |
Zhongxing Xu | a49c6b7 | 2009-12-11 03:09:01 +0000 | [diff] [blame] | 139 | return false; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void MallocChecker::MallocMem(CheckerContext &C, const CallExpr *CE) { |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 143 | const GRState *state = MallocMemAux(C, CE, CE->getArg(0), C.getState()); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 144 | C.addTransition(state); |
| 145 | } |
| 146 | |
| 147 | const GRState *MallocChecker::MallocMemAux(CheckerContext &C, |
| 148 | const CallExpr *CE, |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 149 | const Expr *SizeEx, |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 150 | const GRState *state) { |
Zhongxing Xu | a49c6b7 | 2009-12-11 03:09:01 +0000 | [diff] [blame] | 151 | unsigned Count = C.getNodeBuilder().getCurrentBlockCount(); |
| 152 | ValueManager &ValMgr = C.getValueManager(); |
| 153 | |
| 154 | SVal RetVal = ValMgr.getConjuredSymbolVal(NULL, CE, CE->getType(), Count); |
| 155 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 156 | SVal Size = state->getSVal(SizeEx); |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 157 | |
| 158 | state = C.getEngine().getStoreManager().setExtent(state, RetVal.getAsRegion(), |
| 159 | Size); |
| 160 | |
Zhongxing Xu | a49c6b7 | 2009-12-11 03:09:01 +0000 | [diff] [blame] | 161 | state = state->BindExpr(CE, RetVal); |
| 162 | |
| 163 | SymbolRef Sym = RetVal.getAsLocSymbol(); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 164 | assert(Sym); |
| 165 | // Set the symbol's state to Allocated. |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 166 | return state->set<RegionState>(Sym, RefState::getAllocateUnchecked(CE)); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void MallocChecker::FreeMem(CheckerContext &C, const CallExpr *CE) { |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 170 | const GRState *state = FreeMemAux(C, CE, C.getState()); |
| 171 | |
| 172 | if (state) |
| 173 | C.addTransition(state); |
| 174 | } |
| 175 | |
| 176 | const GRState *MallocChecker::FreeMemAux(CheckerContext &C, const CallExpr *CE, |
| 177 | const GRState *state) { |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 178 | SVal ArgVal = state->getSVal(CE->getArg(0)); |
Zhongxing Xu | 181cc3d | 2010-02-14 06:49:48 +0000 | [diff] [blame] | 179 | |
| 180 | // If ptr is NULL, no operation is preformed. |
| 181 | if (ArgVal.isZeroConstant()) |
| 182 | return state; |
| 183 | |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 184 | SymbolRef Sym = ArgVal.getAsLocSymbol(); |
| 185 | assert(Sym); |
| 186 | |
| 187 | const RefState *RS = state->get<RegionState>(Sym); |
Zhongxing Xu | 7e3cda9 | 2010-01-18 03:27:34 +0000 | [diff] [blame] | 188 | |
| 189 | // If the symbol has not been tracked, return. This is possible when free() is |
| 190 | // called on a pointer that does not get its pointee directly from malloc(). |
| 191 | // Full support of this requires inter-procedural analysis. |
| 192 | if (!RS) |
| 193 | return state; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 194 | |
| 195 | // Check double free. |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 196 | if (RS->isReleased()) { |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 197 | ExplodedNode *N = C.GenerateSink(); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 198 | if (N) { |
| 199 | if (!BT_DoubleFree) |
| 200 | BT_DoubleFree = new BuiltinBug("Double free", |
| 201 | "Try to free a memory block that has been released"); |
| 202 | // FIXME: should find where it's freed last time. |
| 203 | BugReport *R = new BugReport(*BT_DoubleFree, |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 204 | BT_DoubleFree->getDescription(), N); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 205 | C.EmitReport(R); |
| 206 | } |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 207 | return NULL; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Normal free. |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 211 | return state->set<RegionState>(Sym, RefState::getReleased(CE)); |
| 212 | } |
| 213 | |
| 214 | void MallocChecker::ReallocMem(CheckerContext &C, const CallExpr *CE) { |
| 215 | const GRState *state = C.getState(); |
| 216 | const Expr *Arg0 = CE->getArg(0); |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 217 | DefinedOrUnknownSVal Arg0Val=cast<DefinedOrUnknownSVal>(state->getSVal(Arg0)); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 218 | |
| 219 | ValueManager &ValMgr = C.getValueManager(); |
| 220 | SValuator &SVator = C.getSValuator(); |
| 221 | |
| 222 | DefinedOrUnknownSVal PtrEQ = SVator.EvalEQ(state, Arg0Val, ValMgr.makeNull()); |
| 223 | |
| 224 | // If the ptr is NULL, the call is equivalent to malloc(size). |
| 225 | if (const GRState *stateEqual = state->Assume(PtrEQ, true)) { |
| 226 | // Hack: set the NULL symbolic region to released to suppress false warning. |
| 227 | // In the future we should add more states for allocated regions, e.g., |
| 228 | // CheckedNull, CheckedNonNull. |
| 229 | |
| 230 | SymbolRef Sym = Arg0Val.getAsLocSymbol(); |
| 231 | if (Sym) |
| 232 | stateEqual = stateEqual->set<RegionState>(Sym, RefState::getReleased(CE)); |
| 233 | |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 234 | const GRState *stateMalloc = MallocMemAux(C, CE, CE->getArg(1), stateEqual); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 235 | C.addTransition(stateMalloc); |
| 236 | } |
| 237 | |
| 238 | if (const GRState *stateNotEqual = state->Assume(PtrEQ, false)) { |
| 239 | const Expr *Arg1 = CE->getArg(1); |
| 240 | DefinedOrUnknownSVal Arg1Val = |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 241 | cast<DefinedOrUnknownSVal>(stateNotEqual->getSVal(Arg1)); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 242 | DefinedOrUnknownSVal SizeZero = SVator.EvalEQ(stateNotEqual, Arg1Val, |
| 243 | ValMgr.makeIntValWithPtrWidth(0, false)); |
| 244 | |
| 245 | if (const GRState *stateSizeZero = stateNotEqual->Assume(SizeZero, true)) { |
| 246 | const GRState *stateFree = FreeMemAux(C, CE, stateSizeZero); |
| 247 | if (stateFree) |
| 248 | C.addTransition(stateFree->BindExpr(CE, UndefinedVal(), true)); |
| 249 | } |
| 250 | |
| 251 | if (const GRState *stateSizeNotZero=stateNotEqual->Assume(SizeZero,false)) { |
| 252 | const GRState *stateFree = FreeMemAux(C, CE, stateSizeNotZero); |
| 253 | if (stateFree) { |
| 254 | // FIXME: We should copy the content of the original buffer. |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 255 | const GRState *stateRealloc = MallocMemAux(C, CE, CE->getArg(1), |
| 256 | stateFree); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 257 | C.addTransition(stateRealloc); |
| 258 | } |
| 259 | } |
| 260 | } |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 261 | } |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 262 | |
| 263 | void MallocChecker::EvalDeadSymbols(CheckerContext &C, const Stmt *S, |
| 264 | SymbolReaper &SymReaper) { |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 265 | for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(), |
| 266 | E = SymReaper.dead_end(); I != E; ++I) { |
| 267 | SymbolRef Sym = *I; |
| 268 | const GRState *state = C.getState(); |
| 269 | const RefState *RS = state->get<RegionState>(Sym); |
| 270 | if (!RS) |
| 271 | return; |
| 272 | |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 273 | if (RS->isAllocated()) { |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 274 | ExplodedNode *N = C.GenerateSink(); |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 275 | if (N) { |
| 276 | if (!BT_Leak) |
| 277 | BT_Leak = new BuiltinBug("Memory leak", |
| 278 | "Allocated memory never released. Potential memory leak."); |
| 279 | // FIXME: where it is allocated. |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 280 | BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N); |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 281 | C.EmitReport(R); |
| 282 | } |
| 283 | } |
| 284 | } |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 285 | } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 286 | |
| 287 | void MallocChecker::EvalEndPath(GREndPathNodeBuilder &B, void *tag, |
| 288 | GRExprEngine &Eng) { |
Zhongxing Xu | f605aae | 2009-11-22 13:22:34 +0000 | [diff] [blame] | 289 | SaveAndRestore<bool> OldHasGen(B.HasGeneratedNode); |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 290 | const GRState *state = B.getState(); |
| 291 | typedef llvm::ImmutableMap<SymbolRef, RefState> SymMap; |
| 292 | SymMap M = state->get<RegionState>(); |
| 293 | |
| 294 | for (SymMap::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
| 295 | RefState RS = I->second; |
| 296 | if (RS.isAllocated()) { |
| 297 | ExplodedNode *N = B.generateNode(state, tag, B.getPredecessor()); |
| 298 | if (N) { |
| 299 | if (!BT_Leak) |
| 300 | BT_Leak = new BuiltinBug("Memory leak", |
| 301 | "Allocated memory never released. Potential memory leak."); |
| 302 | BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N); |
| 303 | Eng.getBugReporter().EmitReport(R); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | } |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 308 | |
| 309 | void MallocChecker::PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *S) { |
| 310 | const Expr *RetE = S->getRetValue(); |
| 311 | if (!RetE) |
| 312 | return; |
| 313 | |
| 314 | const GRState *state = C.getState(); |
| 315 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 316 | SymbolRef Sym = state->getSVal(RetE).getAsSymbol(); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 317 | |
| 318 | if (!Sym) |
| 319 | return; |
| 320 | |
| 321 | const RefState *RS = state->get<RegionState>(Sym); |
| 322 | if (!RS) |
| 323 | return; |
| 324 | |
| 325 | // FIXME: check other cases. |
| 326 | if (RS->isAllocated()) |
| 327 | state = state->set<RegionState>(Sym, RefState::getEscaped(S)); |
| 328 | |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 329 | C.addTransition(state); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 330 | } |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 331 | |
| 332 | const GRState *MallocChecker::EvalAssume(const GRState *state, SVal Cond, |
| 333 | bool Assumption) { |
| 334 | // If a symblic region is assumed to NULL, set its state to AllocateFailed. |
| 335 | // FIXME: should also check symbols assumed to non-null. |
| 336 | |
| 337 | RegionStateTy RS = state->get<RegionState>(); |
| 338 | |
| 339 | for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) { |
| 340 | if (state->getSymVal(I.getKey())) |
| 341 | state = state->set<RegionState>(I.getKey(),RefState::getAllocateFailed()); |
| 342 | } |
| 343 | |
| 344 | return state; |
| 345 | } |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 346 | |
| 347 | // Check if the location is a freed symbolic region. |
| 348 | void MallocChecker::VisitLocation(CheckerContext &C, const Stmt *S, SVal l) { |
| 349 | SymbolRef Sym = l.getLocSymbolInBase(); |
| 350 | if (Sym) { |
| 351 | const RefState *RS = C.getState()->get<RegionState>(Sym); |
| 352 | if (RS) |
| 353 | if (RS->isReleased()) { |
| 354 | ExplodedNode *N = C.GenerateSink(); |
| 355 | if (!BT_UseFree) |
| 356 | BT_UseFree = new BuiltinBug("Use dynamically allocated memory after" |
| 357 | " it is freed."); |
| 358 | |
| 359 | BugReport *R = new BugReport(*BT_UseFree, BT_UseFree->getDescription(), |
| 360 | N); |
| 361 | C.EmitReport(R); |
| 362 | } |
| 363 | } |
| 364 | } |