blob: 6fd43c11db3deeda36ae473fdc1d9d1557b8b3c1 [file] [log] [blame]
Zhongxing Xu589c0f22009-11-12 08:38:56 +00001//=== 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 Xu7b760962009-11-13 07:25:27 +000015#include "GRExprEngineExperimentalChecks.h"
Zhongxing Xu589c0f22009-11-12 08:38:56 +000016#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"
21using namespace clang;
22
23namespace {
24
Zhongxing Xu7fb14642009-12-11 00:55:44 +000025class RefState {
Zhongxing Xu243fde92009-11-17 07:54:15 +000026 enum Kind { Allocated, Released, Escaped } K;
27 const Stmt *S;
28
Zhongxing Xu7fb14642009-12-11 00:55:44 +000029public:
Zhongxing Xu243fde92009-11-17 07:54:15 +000030 RefState(Kind k, const Stmt *s) : K(k), S(s) {}
31
32 bool isAllocated() const { return K == Allocated; }
33 bool isReleased() const { return K == Released; }
34 bool isEscaped() const { return K == Escaped; }
35
36 bool operator==(const RefState &X) const {
37 return K == X.K && S == X.S;
38 }
39
40 static RefState getAllocated(const Stmt *s) { return RefState(Allocated, s); }
41 static RefState getReleased(const Stmt *s) { return RefState(Released, s); }
42 static RefState getEscaped(const Stmt *s) { return RefState(Escaped, s); }
43
44 void Profile(llvm::FoldingSetNodeID &ID) const {
45 ID.AddInteger(K);
46 ID.AddPointer(S);
47 }
Zhongxing Xu589c0f22009-11-12 08:38:56 +000048};
49
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000050class RegionState {};
Zhongxing Xu589c0f22009-11-12 08:38:56 +000051
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000052class MallocChecker : public CheckerVisitor<MallocChecker> {
Zhongxing Xu589c0f22009-11-12 08:38:56 +000053 BuiltinBug *BT_DoubleFree;
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000054 BuiltinBug *BT_Leak;
Zhongxing Xu589c0f22009-11-12 08:38:56 +000055 IdentifierInfo *II_malloc;
56 IdentifierInfo *II_free;
57
58public:
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000059 MallocChecker() : BT_DoubleFree(0), BT_Leak(0), II_malloc(0), II_free(0) {}
Zhongxing Xu589c0f22009-11-12 08:38:56 +000060 static void *getTag();
61 void PostVisitCallExpr(CheckerContext &C, const CallExpr *CE);
Zhongxing Xu7b760962009-11-13 07:25:27 +000062 void EvalDeadSymbols(CheckerContext &C,const Stmt *S,SymbolReaper &SymReaper);
Zhongxing Xu243fde92009-11-17 07:54:15 +000063 void EvalEndPath(GREndPathNodeBuilder &B, void *tag, GRExprEngine &Eng);
Zhongxing Xu4985e3e2009-11-17 08:58:18 +000064 void PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *S);
Zhongxing Xu7b760962009-11-13 07:25:27 +000065private:
Zhongxing Xu589c0f22009-11-12 08:38:56 +000066 void MallocMem(CheckerContext &C, const CallExpr *CE);
67 void FreeMem(CheckerContext &C, const CallExpr *CE);
68};
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000069} // end anonymous namespace
Zhongxing Xu589c0f22009-11-12 08:38:56 +000070
Zhongxing Xu589c0f22009-11-12 08:38:56 +000071namespace clang {
Zhongxing Xu243fde92009-11-17 07:54:15 +000072 template <>
Zhongxing Xu589c0f22009-11-12 08:38:56 +000073 struct GRStateTrait<RegionState>
74 : public GRStatePartialTrait<llvm::ImmutableMap<SymbolRef, RefState> > {
75 static void *GDMIndex() { return MallocChecker::getTag(); }
76 };
77}
78
Zhongxing Xu7b760962009-11-13 07:25:27 +000079void clang::RegisterMallocChecker(GRExprEngine &Eng) {
80 Eng.registerCheck(new MallocChecker());
81}
82
Zhongxing Xu589c0f22009-11-12 08:38:56 +000083void *MallocChecker::getTag() {
84 static int x;
85 return &x;
86}
87
88void MallocChecker::PostVisitCallExpr(CheckerContext &C, const CallExpr *CE) {
89 const FunctionDecl *FD = CE->getDirectCallee();
90 if (!FD)
91 return;
92
93 ASTContext &Ctx = C.getASTContext();
94 if (!II_malloc)
95 II_malloc = &Ctx.Idents.get("malloc");
96 if (!II_free)
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000097 II_free = &Ctx.Idents.get("free");
Zhongxing Xu589c0f22009-11-12 08:38:56 +000098
99 if (FD->getIdentifier() == II_malloc) {
100 MallocMem(C, CE);
101 return;
102 }
103
104 if (FD->getIdentifier() == II_free) {
105 FreeMem(C, CE);
106 return;
107 }
108}
109
110void MallocChecker::MallocMem(CheckerContext &C, const CallExpr *CE) {
111 const GRState *state = C.getState();
112 SVal CallVal = state->getSVal(CE);
113 SymbolRef Sym = CallVal.getAsLocSymbol();
114 assert(Sym);
115 // Set the symbol's state to Allocated.
Ted Kremenek19d67b52009-11-23 22:22:01 +0000116 C.addTransition(state->set<RegionState>(Sym, RefState::getAllocated(CE)));
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000117}
118
119void MallocChecker::FreeMem(CheckerContext &C, const CallExpr *CE) {
120 const GRState *state = C.getState();
121 SVal ArgVal = state->getSVal(CE->getArg(0));
122 SymbolRef Sym = ArgVal.getAsLocSymbol();
123 assert(Sym);
124
125 const RefState *RS = state->get<RegionState>(Sym);
126 assert(RS);
127
128 // Check double free.
Zhongxing Xu243fde92009-11-17 07:54:15 +0000129 if (RS->isReleased()) {
Ted Kremenek19d67b52009-11-23 22:22:01 +0000130 ExplodedNode *N = C.GenerateSink();
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000131 if (N) {
132 if (!BT_DoubleFree)
133 BT_DoubleFree = new BuiltinBug("Double free",
134 "Try to free a memory block that has been released");
135 // FIXME: should find where it's freed last time.
136 BugReport *R = new BugReport(*BT_DoubleFree,
Benjamin Kramerd02e2322009-11-14 12:08:24 +0000137 BT_DoubleFree->getDescription(), N);
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000138 C.EmitReport(R);
139 }
140 return;
141 }
142
143 // Normal free.
Zhongxing Xu243fde92009-11-17 07:54:15 +0000144 const GRState *FreedState
145 = state->set<RegionState>(Sym, RefState::getReleased(CE));
Ted Kremenek19d67b52009-11-23 22:22:01 +0000146 C.addTransition(FreedState);
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000147}
Zhongxing Xu7b760962009-11-13 07:25:27 +0000148
149void MallocChecker::EvalDeadSymbols(CheckerContext &C, const Stmt *S,
150 SymbolReaper &SymReaper) {
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000151 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
152 E = SymReaper.dead_end(); I != E; ++I) {
153 SymbolRef Sym = *I;
154 const GRState *state = C.getState();
155 const RefState *RS = state->get<RegionState>(Sym);
156 if (!RS)
157 return;
158
Zhongxing Xu243fde92009-11-17 07:54:15 +0000159 if (RS->isAllocated()) {
Ted Kremenek19d67b52009-11-23 22:22:01 +0000160 ExplodedNode *N = C.GenerateSink();
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000161 if (N) {
162 if (!BT_Leak)
163 BT_Leak = new BuiltinBug("Memory leak",
164 "Allocated memory never released. Potential memory leak.");
165 // FIXME: where it is allocated.
Zhongxing Xu243fde92009-11-17 07:54:15 +0000166 BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000167 C.EmitReport(R);
168 }
169 }
170 }
Zhongxing Xu7b760962009-11-13 07:25:27 +0000171}
Zhongxing Xu243fde92009-11-17 07:54:15 +0000172
173void MallocChecker::EvalEndPath(GREndPathNodeBuilder &B, void *tag,
174 GRExprEngine &Eng) {
Zhongxing Xuf605aae2009-11-22 13:22:34 +0000175 SaveAndRestore<bool> OldHasGen(B.HasGeneratedNode);
Zhongxing Xu243fde92009-11-17 07:54:15 +0000176 const GRState *state = B.getState();
177 typedef llvm::ImmutableMap<SymbolRef, RefState> SymMap;
178 SymMap M = state->get<RegionState>();
179
180 for (SymMap::iterator I = M.begin(), E = M.end(); I != E; ++I) {
181 RefState RS = I->second;
182 if (RS.isAllocated()) {
183 ExplodedNode *N = B.generateNode(state, tag, B.getPredecessor());
184 if (N) {
185 if (!BT_Leak)
186 BT_Leak = new BuiltinBug("Memory leak",
187 "Allocated memory never released. Potential memory leak.");
188 BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
189 Eng.getBugReporter().EmitReport(R);
190 }
191 }
192 }
193}
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000194
195void MallocChecker::PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *S) {
196 const Expr *RetE = S->getRetValue();
197 if (!RetE)
198 return;
199
200 const GRState *state = C.getState();
201
202 SymbolRef Sym = state->getSVal(RetE).getAsSymbol();
203
204 if (!Sym)
205 return;
206
207 const RefState *RS = state->get<RegionState>(Sym);
208 if (!RS)
209 return;
210
211 // FIXME: check other cases.
212 if (RS->isAllocated())
213 state = state->set<RegionState>(Sym, RefState::getEscaped(S));
214
Ted Kremenek19d67b52009-11-23 22:22:01 +0000215 C.addTransition(state);
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000216}