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" |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/CheckerVisitor.h" |
| 17 | #include "clang/Analysis/PathSensitive/GRState.h" |
| 18 | #include "clang/Analysis/PathSensitive/GRStateTrait.h" |
| 19 | #include "clang/Analysis/PathSensitive/SymbolManager.h" |
| 20 | #include "llvm/ADT/ImmutableMap.h" |
| 21 | using namespace clang; |
| 22 | |
| 23 | namespace { |
| 24 | |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 25 | struct RefState { |
| 26 | enum Kind { Allocated, Released, Escaped } K; |
| 27 | const Stmt *S; |
| 28 | |
| 29 | RefState(Kind k, const Stmt *s) : K(k), S(s) {} |
| 30 | |
| 31 | bool isAllocated() const { return K == Allocated; } |
| 32 | bool isReleased() const { return K == Released; } |
| 33 | bool isEscaped() const { return K == Escaped; } |
| 34 | |
| 35 | bool operator==(const RefState &X) const { |
| 36 | return K == X.K && S == X.S; |
| 37 | } |
| 38 | |
| 39 | static RefState getAllocated(const Stmt *s) { return RefState(Allocated, s); } |
| 40 | static RefState getReleased(const Stmt *s) { return RefState(Released, s); } |
| 41 | static RefState getEscaped(const Stmt *s) { return RefState(Escaped, s); } |
| 42 | |
| 43 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 44 | ID.AddInteger(K); |
| 45 | ID.AddPointer(S); |
| 46 | } |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | class VISIBILITY_HIDDEN RegionState {}; |
| 50 | |
Benjamin Kramer | 221089b | 2009-11-12 12:30:05 +0000 | [diff] [blame] | 51 | class VISIBILITY_HIDDEN MallocChecker : public CheckerVisitor<MallocChecker> { |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 52 | BuiltinBug *BT_DoubleFree; |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 53 | BuiltinBug *BT_Leak; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 54 | IdentifierInfo *II_malloc; |
| 55 | IdentifierInfo *II_free; |
| 56 | |
| 57 | public: |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 58 | MallocChecker() : BT_DoubleFree(0), BT_Leak(0), II_malloc(0), II_free(0) {} |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 59 | static void *getTag(); |
| 60 | void PostVisitCallExpr(CheckerContext &C, const CallExpr *CE); |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 61 | void EvalDeadSymbols(CheckerContext &C,const Stmt *S,SymbolReaper &SymReaper); |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 62 | void EvalEndPath(GREndPathNodeBuilder &B, void *tag, GRExprEngine &Eng); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 63 | void PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *S); |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 64 | private: |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 65 | void MallocMem(CheckerContext &C, const CallExpr *CE); |
| 66 | void FreeMem(CheckerContext &C, const CallExpr *CE); |
| 67 | }; |
| 68 | } |
| 69 | |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 70 | namespace clang { |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 71 | template <> |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 72 | struct GRStateTrait<RegionState> |
| 73 | : public GRStatePartialTrait<llvm::ImmutableMap<SymbolRef, RefState> > { |
| 74 | static void *GDMIndex() { return MallocChecker::getTag(); } |
| 75 | }; |
| 76 | } |
| 77 | |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 78 | void clang::RegisterMallocChecker(GRExprEngine &Eng) { |
| 79 | Eng.registerCheck(new MallocChecker()); |
| 80 | } |
| 81 | |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 82 | void *MallocChecker::getTag() { |
| 83 | static int x; |
| 84 | return &x; |
| 85 | } |
| 86 | |
| 87 | void MallocChecker::PostVisitCallExpr(CheckerContext &C, const CallExpr *CE) { |
| 88 | const FunctionDecl *FD = CE->getDirectCallee(); |
| 89 | if (!FD) |
| 90 | return; |
| 91 | |
| 92 | ASTContext &Ctx = C.getASTContext(); |
| 93 | if (!II_malloc) |
| 94 | II_malloc = &Ctx.Idents.get("malloc"); |
| 95 | if (!II_free) |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 96 | II_free = &Ctx.Idents.get("free"); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 97 | |
| 98 | if (FD->getIdentifier() == II_malloc) { |
| 99 | MallocMem(C, CE); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (FD->getIdentifier() == II_free) { |
| 104 | FreeMem(C, CE); |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void MallocChecker::MallocMem(CheckerContext &C, const CallExpr *CE) { |
| 110 | const GRState *state = C.getState(); |
| 111 | SVal CallVal = state->getSVal(CE); |
| 112 | SymbolRef Sym = CallVal.getAsLocSymbol(); |
| 113 | assert(Sym); |
| 114 | // Set the symbol's state to Allocated. |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 115 | const GRState *AllocState |
| 116 | = state->set<RegionState>(Sym, RefState::getAllocated(CE)); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 117 | C.addTransition(C.GenerateNode(CE, AllocState)); |
| 118 | } |
| 119 | |
| 120 | void MallocChecker::FreeMem(CheckerContext &C, const CallExpr *CE) { |
| 121 | const GRState *state = C.getState(); |
| 122 | SVal ArgVal = state->getSVal(CE->getArg(0)); |
| 123 | SymbolRef Sym = ArgVal.getAsLocSymbol(); |
| 124 | assert(Sym); |
| 125 | |
| 126 | const RefState *RS = state->get<RegionState>(Sym); |
| 127 | assert(RS); |
| 128 | |
| 129 | // Check double free. |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 130 | if (RS->isReleased()) { |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 131 | ExplodedNode *N = C.GenerateNode(CE, true); |
| 132 | if (N) { |
| 133 | if (!BT_DoubleFree) |
| 134 | BT_DoubleFree = new BuiltinBug("Double free", |
| 135 | "Try to free a memory block that has been released"); |
| 136 | // FIXME: should find where it's freed last time. |
| 137 | BugReport *R = new BugReport(*BT_DoubleFree, |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 138 | BT_DoubleFree->getDescription(), N); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 139 | C.EmitReport(R); |
| 140 | } |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | // Normal free. |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 145 | const GRState *FreedState |
| 146 | = state->set<RegionState>(Sym, RefState::getReleased(CE)); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 147 | C.addTransition(C.GenerateNode(CE, FreedState)); |
| 148 | } |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 149 | |
| 150 | void MallocChecker::EvalDeadSymbols(CheckerContext &C, const Stmt *S, |
| 151 | SymbolReaper &SymReaper) { |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 152 | for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(), |
| 153 | E = SymReaper.dead_end(); I != E; ++I) { |
| 154 | SymbolRef Sym = *I; |
| 155 | const GRState *state = C.getState(); |
| 156 | const RefState *RS = state->get<RegionState>(Sym); |
| 157 | if (!RS) |
| 158 | return; |
| 159 | |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 160 | if (RS->isAllocated()) { |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 161 | ExplodedNode *N = C.GenerateNode(S, true); |
| 162 | if (N) { |
| 163 | if (!BT_Leak) |
| 164 | BT_Leak = new BuiltinBug("Memory leak", |
| 165 | "Allocated memory never released. Potential memory leak."); |
| 166 | // FIXME: where it is allocated. |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 167 | BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N); |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 168 | C.EmitReport(R); |
| 169 | } |
| 170 | } |
| 171 | } |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 172 | } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 173 | |
| 174 | void MallocChecker::EvalEndPath(GREndPathNodeBuilder &B, void *tag, |
| 175 | GRExprEngine &Eng) { |
Zhongxing Xu | f605aae | 2009-11-22 13:22:34 +0000 | [diff] [blame] | 176 | SaveAndRestore<bool> OldHasGen(B.HasGeneratedNode); |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 177 | const GRState *state = B.getState(); |
| 178 | typedef llvm::ImmutableMap<SymbolRef, RefState> SymMap; |
| 179 | SymMap M = state->get<RegionState>(); |
| 180 | |
| 181 | for (SymMap::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
| 182 | RefState RS = I->second; |
| 183 | if (RS.isAllocated()) { |
| 184 | ExplodedNode *N = B.generateNode(state, tag, B.getPredecessor()); |
| 185 | if (N) { |
| 186 | if (!BT_Leak) |
| 187 | BT_Leak = new BuiltinBug("Memory leak", |
| 188 | "Allocated memory never released. Potential memory leak."); |
| 189 | BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N); |
| 190 | Eng.getBugReporter().EmitReport(R); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 195 | |
| 196 | void MallocChecker::PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *S) { |
| 197 | const Expr *RetE = S->getRetValue(); |
| 198 | if (!RetE) |
| 199 | return; |
| 200 | |
| 201 | const GRState *state = C.getState(); |
| 202 | |
| 203 | SymbolRef Sym = state->getSVal(RetE).getAsSymbol(); |
| 204 | |
| 205 | if (!Sym) |
| 206 | return; |
| 207 | |
| 208 | const RefState *RS = state->get<RegionState>(Sym); |
| 209 | if (!RS) |
| 210 | return; |
| 211 | |
| 212 | // FIXME: check other cases. |
| 213 | if (RS->isAllocated()) |
| 214 | state = state->set<RegionState>(Sym, RefState::getEscaped(S)); |
| 215 | |
| 216 | ExplodedNode *N = C.GenerateNode(S, state); |
| 217 | if (N) |
| 218 | C.addTransition(N); |
| 219 | } |