blob: dcc7ab95dc03b39d488bcf50031f45a92d074b91 [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
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000015#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
18#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Ted Kremenek18c66fd2011-08-15 22:09:50 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
21#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000022#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
Zhongxing Xu589c0f22009-11-12 08:38:56 +000023#include "llvm/ADT/ImmutableMap.h"
24using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000025using namespace ento;
Zhongxing Xu589c0f22009-11-12 08:38:56 +000026
27namespace {
28
Zhongxing Xu7fb14642009-12-11 00:55:44 +000029class RefState {
Ted Kremenekdde201b2010-08-06 21:12:55 +000030 enum Kind { AllocateUnchecked, AllocateFailed, Released, Escaped,
31 Relinquished } K;
Zhongxing Xu243fde92009-11-17 07:54:15 +000032 const Stmt *S;
33
Zhongxing Xu7fb14642009-12-11 00:55:44 +000034public:
Zhongxing Xu243fde92009-11-17 07:54:15 +000035 RefState(Kind k, const Stmt *s) : K(k), S(s) {}
36
Zhongxing Xub94b81a2009-12-31 06:13:07 +000037 bool isAllocated() const { return K == AllocateUnchecked; }
Chris Lattnerfae96222010-09-03 04:34:38 +000038 //bool isFailed() const { return K == AllocateFailed; }
Zhongxing Xu243fde92009-11-17 07:54:15 +000039 bool isReleased() const { return K == Released; }
Chris Lattnerfae96222010-09-03 04:34:38 +000040 //bool isEscaped() const { return K == Escaped; }
41 //bool isRelinquished() const { return K == Relinquished; }
Zhongxing Xu243fde92009-11-17 07:54:15 +000042
43 bool operator==(const RefState &X) const {
44 return K == X.K && S == X.S;
45 }
46
Zhongxing Xub94b81a2009-12-31 06:13:07 +000047 static RefState getAllocateUnchecked(const Stmt *s) {
48 return RefState(AllocateUnchecked, s);
49 }
50 static RefState getAllocateFailed() {
51 return RefState(AllocateFailed, 0);
52 }
Zhongxing Xu243fde92009-11-17 07:54:15 +000053 static RefState getReleased(const Stmt *s) { return RefState(Released, s); }
54 static RefState getEscaped(const Stmt *s) { return RefState(Escaped, s); }
Ted Kremenekdde201b2010-08-06 21:12:55 +000055 static RefState getRelinquished(const Stmt *s) {
56 return RefState(Relinquished, s);
57 }
Zhongxing Xu243fde92009-11-17 07:54:15 +000058
59 void Profile(llvm::FoldingSetNodeID &ID) const {
60 ID.AddInteger(K);
61 ID.AddPointer(S);
62 }
Zhongxing Xu589c0f22009-11-12 08:38:56 +000063};
64
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000065class RegionState {};
Zhongxing Xu589c0f22009-11-12 08:38:56 +000066
Ted Kremeneke3659a72012-01-04 23:48:37 +000067class MallocChecker : public Checker<eval::Call,
68 check::DeadSymbols,
69 check::EndPath,
70 check::PreStmt<ReturnStmt>,
71 check::Location,
72 check::Bind,
73 eval::Assume>
74{
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000075 mutable llvm::OwningPtr<BuiltinBug> BT_DoubleFree;
76 mutable llvm::OwningPtr<BuiltinBug> BT_Leak;
77 mutable llvm::OwningPtr<BuiltinBug> BT_UseFree;
78 mutable llvm::OwningPtr<BuiltinBug> BT_UseRelinquished;
79 mutable llvm::OwningPtr<BuiltinBug> BT_BadFree;
80 mutable IdentifierInfo *II_malloc, *II_free, *II_realloc, *II_calloc;
Zhongxing Xu589c0f22009-11-12 08:38:56 +000081
82public:
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000083 MallocChecker() : II_malloc(0), II_free(0), II_realloc(0), II_calloc(0) {}
84
85 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
86 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
Anna Zaksaf498a22011-10-25 19:56:48 +000087 void checkEndPath(CheckerContext &C) const;
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000088 void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
Ted Kremenek18c66fd2011-08-15 22:09:50 +000089 const ProgramState *evalAssume(const ProgramState *state, SVal Cond,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000090 bool Assumption) const;
Anna Zaks390909c2011-10-06 00:43:15 +000091 void checkLocation(SVal l, bool isLoad, const Stmt *S,
92 CheckerContext &C) const;
93 void checkBind(SVal location, SVal val, const Stmt*S,
94 CheckerContext &C) const;
Zhongxing Xub94b81a2009-12-31 06:13:07 +000095
Zhongxing Xu7b760962009-11-13 07:25:27 +000096private:
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000097 static void MallocMem(CheckerContext &C, const CallExpr *CE);
98 static void MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,
99 const OwnershipAttr* Att);
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000100 static const ProgramState *MallocMemAux(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000101 const Expr *SizeEx, SVal Init,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000102 const ProgramState *state) {
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000103 return MallocMemAux(C, CE, state->getSVal(SizeEx), Init, state);
104 }
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000105 static const ProgramState *MallocMemAux(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000106 SVal SizeEx, SVal Init,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000107 const ProgramState *state);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000108
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000109 void FreeMem(CheckerContext &C, const CallExpr *CE) const;
Jordy Rose2a479922010-08-12 08:54:03 +0000110 void FreeMemAttr(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000111 const OwnershipAttr* Att) const;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000112 const ProgramState *FreeMemAux(CheckerContext &C, const CallExpr *CE,
Ted Kremeneke3659a72012-01-04 23:48:37 +0000113 const ProgramState *state, unsigned Num,
114 bool Hold) const;
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000115
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000116 void ReallocMem(CheckerContext &C, const CallExpr *CE) const;
117 static void CallocMem(CheckerContext &C, const CallExpr *CE);
Jordy Rose43859f62010-06-07 19:32:37 +0000118
Ted Kremenek9c378f72011-08-12 23:37:29 +0000119 static bool SummarizeValue(raw_ostream &os, SVal V);
120 static bool SummarizeRegion(raw_ostream &os, const MemRegion *MR);
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000121 void ReportBadFree(CheckerContext &C, SVal ArgVal, SourceRange range) const;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000122};
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000123} // end anonymous namespace
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000124
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000125typedef llvm::ImmutableMap<SymbolRef, RefState> RegionStateTy;
126
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000127namespace clang {
Ted Kremenek9ef65372010-12-23 07:20:52 +0000128namespace ento {
Zhongxing Xu243fde92009-11-17 07:54:15 +0000129 template <>
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000130 struct ProgramStateTrait<RegionState>
131 : public ProgramStatePartialTrait<RegionStateTy> {
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000132 static void *GDMIndex() { static int x; return &x; }
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000133 };
134}
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000135}
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000136
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000137bool MallocChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
Anna Zaksb805c8f2011-12-01 05:57:37 +0000138 const FunctionDecl *FD = C.getCalleeDecl(CE);
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000139 if (!FD)
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000140 return false;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000141
142 ASTContext &Ctx = C.getASTContext();
143 if (!II_malloc)
144 II_malloc = &Ctx.Idents.get("malloc");
145 if (!II_free)
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000146 II_free = &Ctx.Idents.get("free");
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000147 if (!II_realloc)
148 II_realloc = &Ctx.Idents.get("realloc");
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000149 if (!II_calloc)
150 II_calloc = &Ctx.Idents.get("calloc");
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000151
152 if (FD->getIdentifier() == II_malloc) {
153 MallocMem(C, CE);
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000154 return true;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000155 }
156
157 if (FD->getIdentifier() == II_free) {
158 FreeMem(C, CE);
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000159 return true;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000160 }
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000161
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000162 if (FD->getIdentifier() == II_realloc) {
163 ReallocMem(C, CE);
164 return true;
165 }
166
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000167 if (FD->getIdentifier() == II_calloc) {
168 CallocMem(C, CE);
169 return true;
170 }
171
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000172 // Check all the attributes, if there are any.
173 // There can be multiple of these attributes.
174 bool rv = false;
175 if (FD->hasAttrs()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000176 for (specific_attr_iterator<OwnershipAttr>
177 i = FD->specific_attr_begin<OwnershipAttr>(),
178 e = FD->specific_attr_end<OwnershipAttr>();
179 i != e; ++i) {
180 switch ((*i)->getOwnKind()) {
181 case OwnershipAttr::Returns: {
182 MallocMemReturnsAttr(C, CE, *i);
Jordy Rose2a479922010-08-12 08:54:03 +0000183 rv = true;
184 break;
Sean Huntcf807c42010-08-18 23:23:40 +0000185 }
186 case OwnershipAttr::Takes:
187 case OwnershipAttr::Holds: {
188 FreeMemAttr(C, CE, *i);
Jordy Rose2a479922010-08-12 08:54:03 +0000189 rv = true;
190 break;
Sean Huntcf807c42010-08-18 23:23:40 +0000191 }
Jordy Rose2a479922010-08-12 08:54:03 +0000192 default:
Jordy Rose2a479922010-08-12 08:54:03 +0000193 break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000194 }
195 }
196 }
197 return rv;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000198}
199
200void MallocChecker::MallocMem(CheckerContext &C, const CallExpr *CE) {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000201 const ProgramState *state = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(),
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000202 C.getState());
Anna Zaks0bd6b112011-10-26 21:06:34 +0000203 C.addTransition(state);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000204}
205
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000206void MallocChecker::MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,
207 const OwnershipAttr* Att) {
Sean Huntcf807c42010-08-18 23:23:40 +0000208 if (Att->getModule() != "malloc")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000209 return;
210
Sean Huntcf807c42010-08-18 23:23:40 +0000211 OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000212 if (I != E) {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000213 const ProgramState *state =
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000214 MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), C.getState());
Anna Zaks0bd6b112011-10-26 21:06:34 +0000215 C.addTransition(state);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000216 return;
217 }
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000218 const ProgramState *state = MallocMemAux(C, CE, UnknownVal(), UndefinedVal(),
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000219 C.getState());
Anna Zaks0bd6b112011-10-26 21:06:34 +0000220 C.addTransition(state);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000221}
222
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000223const ProgramState *MallocChecker::MallocMemAux(CheckerContext &C,
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000224 const CallExpr *CE,
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000225 SVal Size, SVal Init,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000226 const ProgramState *state) {
Anna Zaks5d0ea6d2011-10-04 20:43:05 +0000227 unsigned Count = C.getCurrentBlockCount();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000228 SValBuilder &svalBuilder = C.getSValBuilder();
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000229
Jordy Rose32f26562010-07-04 00:00:41 +0000230 // Set the return value.
Ted Kremeneke3659a72012-01-04 23:48:37 +0000231 SVal retVal = svalBuilder.getConjuredSymbolVal(NULL, CE,
232 CE->getType(), Count);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000233 state = state->BindExpr(CE, retVal);
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000234
Jordy Rose32f26562010-07-04 00:00:41 +0000235 // Fill the region with the initialization value.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000236 state = state->bindDefault(retVal, Init);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000237
Jordy Rose32f26562010-07-04 00:00:41 +0000238 // Set the region's extent equal to the Size parameter.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000239 const SymbolicRegion *R = cast<SymbolicRegion>(retVal.getAsRegion());
240 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
Jordy Rose32f26562010-07-04 00:00:41 +0000241 DefinedOrUnknownSVal DefinedSize = cast<DefinedOrUnknownSVal>(Size);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000242 DefinedOrUnknownSVal extentMatchesSize =
Ted Kremenek9c149532010-12-01 21:57:22 +0000243 svalBuilder.evalEQ(state, Extent, DefinedSize);
Jordy Rose32f26562010-07-04 00:00:41 +0000244
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000245 state = state->assume(extentMatchesSize, true);
246 assert(state);
247
248 SymbolRef Sym = retVal.getAsLocSymbol();
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000249 assert(Sym);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000250
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000251 // Set the symbol's state to Allocated.
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000252 return state->set<RegionState>(Sym, RefState::getAllocateUnchecked(CE));
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000253}
254
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000255void MallocChecker::FreeMem(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000256 const ProgramState *state = FreeMemAux(C, CE, C.getState(), 0, false);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000257
258 if (state)
Anna Zaks0bd6b112011-10-26 21:06:34 +0000259 C.addTransition(state);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000260}
261
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000262void MallocChecker::FreeMemAttr(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000263 const OwnershipAttr* Att) const {
Sean Huntcf807c42010-08-18 23:23:40 +0000264 if (Att->getModule() != "malloc")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000265 return;
266
Sean Huntcf807c42010-08-18 23:23:40 +0000267 for (OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
268 I != E; ++I) {
Ted Kremeneke3659a72012-01-04 23:48:37 +0000269 const ProgramState *state =
270 FreeMemAux(C, CE, C.getState(), *I,
271 Att->getOwnKind() == OwnershipAttr::Holds);
Sean Huntcf807c42010-08-18 23:23:40 +0000272 if (state)
Anna Zaks0bd6b112011-10-26 21:06:34 +0000273 C.addTransition(state);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000274 }
275}
276
Ted Kremeneke3659a72012-01-04 23:48:37 +0000277const ProgramState *MallocChecker::FreeMemAux(CheckerContext &C,
278 const CallExpr *CE,
279 const ProgramState *state,
280 unsigned Num,
281 bool Hold) const {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000282 const Expr *ArgExpr = CE->getArg(Num);
Jordy Rose43859f62010-06-07 19:32:37 +0000283 SVal ArgVal = state->getSVal(ArgExpr);
Zhongxing Xu181cc3d2010-02-14 06:49:48 +0000284
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000285 DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(ArgVal);
286
287 // Check for null dereferences.
288 if (!isa<Loc>(location))
Zhongxing Xu181cc3d2010-02-14 06:49:48 +0000289 return state;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000290
291 // FIXME: Technically using 'Assume' here can result in a path
292 // bifurcation. In such cases we need to return two states, not just one.
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000293 const ProgramState *notNullState, *nullState;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000294 llvm::tie(notNullState, nullState) = state->assume(location);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000295
296 // The explicit NULL case, no operation is performed.
297 if (nullState && !notNullState)
298 return nullState;
299
300 assert(notNullState);
301
Jordy Rose43859f62010-06-07 19:32:37 +0000302 // Unknown values could easily be okay
303 // Undefined values are handled elsewhere
304 if (ArgVal.isUnknownOrUndef())
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000305 return notNullState;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000306
Jordy Rose43859f62010-06-07 19:32:37 +0000307 const MemRegion *R = ArgVal.getAsRegion();
308
309 // Nonlocs can't be freed, of course.
310 // Non-region locations (labels and fixed addresses) also shouldn't be freed.
311 if (!R) {
312 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
313 return NULL;
314 }
315
316 R = R->StripCasts();
317
318 // Blocks might show up as heap data, but should not be free()d
319 if (isa<BlockDataRegion>(R)) {
320 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
321 return NULL;
322 }
323
324 const MemSpaceRegion *MS = R->getMemorySpace();
325
326 // Parameters, locals, statics, and globals shouldn't be freed.
327 if (!(isa<UnknownSpaceRegion>(MS) || isa<HeapSpaceRegion>(MS))) {
328 // FIXME: at the time this code was written, malloc() regions were
329 // represented by conjured symbols, which are all in UnknownSpaceRegion.
330 // This means that there isn't actually anything from HeapSpaceRegion
331 // that should be freed, even though we allow it here.
332 // Of course, free() can work on memory allocated outside the current
333 // function, so UnknownSpaceRegion is always a possibility.
334 // False negatives are better than false positives.
335
336 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
337 return NULL;
338 }
339
340 const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R);
341 // Various cases could lead to non-symbol values here.
342 // For now, ignore them.
343 if (!SR)
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000344 return notNullState;
Jordy Rose43859f62010-06-07 19:32:37 +0000345
346 SymbolRef Sym = SR->getSymbol();
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000347 const RefState *RS = state->get<RegionState>(Sym);
Zhongxing Xu7e3cda92010-01-18 03:27:34 +0000348
349 // If the symbol has not been tracked, return. This is possible when free() is
350 // called on a pointer that does not get its pointee directly from malloc().
351 // Full support of this requires inter-procedural analysis.
352 if (!RS)
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000353 return notNullState;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000354
355 // Check double free.
Zhongxing Xu243fde92009-11-17 07:54:15 +0000356 if (RS->isReleased()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000357 if (ExplodedNode *N = C.generateSink()) {
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000358 if (!BT_DoubleFree)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000359 BT_DoubleFree.reset(
360 new BuiltinBug("Double free",
361 "Try to free a memory block that has been released"));
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000362 // FIXME: should find where it's freed last time.
363 BugReport *R = new BugReport(*BT_DoubleFree,
Benjamin Kramerd02e2322009-11-14 12:08:24 +0000364 BT_DoubleFree->getDescription(), N);
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000365 C.EmitReport(R);
366 }
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000367 return NULL;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000368 }
369
370 // Normal free.
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000371 if (Hold)
372 return notNullState->set<RegionState>(Sym, RefState::getRelinquished(CE));
373 return notNullState->set<RegionState>(Sym, RefState::getReleased(CE));
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000374}
375
Ted Kremenek9c378f72011-08-12 23:37:29 +0000376bool MallocChecker::SummarizeValue(raw_ostream &os, SVal V) {
Jordy Rose43859f62010-06-07 19:32:37 +0000377 if (nonloc::ConcreteInt *IntVal = dyn_cast<nonloc::ConcreteInt>(&V))
378 os << "an integer (" << IntVal->getValue() << ")";
379 else if (loc::ConcreteInt *ConstAddr = dyn_cast<loc::ConcreteInt>(&V))
380 os << "a constant address (" << ConstAddr->getValue() << ")";
381 else if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&V))
Chris Lattner68106302011-02-17 05:38:27 +0000382 os << "the address of the label '" << Label->getLabel()->getName() << "'";
Jordy Rose43859f62010-06-07 19:32:37 +0000383 else
384 return false;
385
386 return true;
387}
388
Ted Kremenek9c378f72011-08-12 23:37:29 +0000389bool MallocChecker::SummarizeRegion(raw_ostream &os,
Jordy Rose43859f62010-06-07 19:32:37 +0000390 const MemRegion *MR) {
391 switch (MR->getKind()) {
392 case MemRegion::FunctionTextRegionKind: {
393 const FunctionDecl *FD = cast<FunctionTextRegion>(MR)->getDecl();
394 if (FD)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000395 os << "the address of the function '" << *FD << '\'';
Jordy Rose43859f62010-06-07 19:32:37 +0000396 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
Anna Zakseb31a762012-01-04 23:54:01 +0000410 if (isa<StackLocalsSpaceRegion>(MS)) {
Jordy Rose43859f62010-06-07 19:32:37 +0000411 const VarRegion *VR = dyn_cast<VarRegion>(MR);
412 const VarDecl *VD;
413 if (VR)
414 VD = VR->getDecl();
415 else
416 VD = NULL;
417
418 if (VD)
419 os << "the address of the local variable '" << VD->getName() << "'";
420 else
421 os << "the address of a local stack variable";
422 return true;
423 }
Anna Zakseb31a762012-01-04 23:54:01 +0000424
425 if (isa<StackArgumentsSpaceRegion>(MS)) {
Jordy Rose43859f62010-06-07 19:32:37 +0000426 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 }
Anna Zakseb31a762012-01-04 23:54:01 +0000439
440 if (isa<GlobalsSpaceRegion>(MS)) {
Jordy Rose43859f62010-06-07 19:32:37 +0000441 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 }
Anna Zakseb31a762012-01-04 23:54:01 +0000457
458 return false;
Jordy Rose43859f62010-06-07 19:32:37 +0000459 }
460 }
461}
462
463void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000464 SourceRange range) const {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000465 if (ExplodedNode *N = C.generateSink()) {
Jordy Rose43859f62010-06-07 19:32:37 +0000466 if (!BT_BadFree)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000467 BT_BadFree.reset(new BuiltinBug("Bad free"));
Jordy Rose43859f62010-06-07 19:32:37 +0000468
469 llvm::SmallString<100> buf;
470 llvm::raw_svector_ostream os(buf);
471
472 const MemRegion *MR = ArgVal.getAsRegion();
473 if (MR) {
474 while (const ElementRegion *ER = dyn_cast<ElementRegion>(MR))
475 MR = ER->getSuperRegion();
476
477 // Special case for alloca()
478 if (isa<AllocaRegion>(MR))
479 os << "Argument to free() was allocated by alloca(), not malloc()";
480 else {
481 os << "Argument to free() is ";
482 if (SummarizeRegion(os, MR))
483 os << ", which is not memory allocated by malloc()";
484 else
485 os << "not memory allocated by malloc()";
486 }
487 } else {
488 os << "Argument to free() is ";
489 if (SummarizeValue(os, ArgVal))
490 os << ", which is not memory allocated by malloc()";
491 else
492 os << "not memory allocated by malloc()";
493 }
494
Anna Zakse172e8b2011-08-17 23:00:25 +0000495 BugReport *R = new BugReport(*BT_BadFree, os.str(), N);
Jordy Rose43859f62010-06-07 19:32:37 +0000496 R->addRange(range);
497 C.EmitReport(R);
498 }
499}
500
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000501void MallocChecker::ReallocMem(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000502 const ProgramState *state = C.getState();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000503 const Expr *arg0Expr = CE->getArg(0);
504 DefinedOrUnknownSVal arg0Val
505 = cast<DefinedOrUnknownSVal>(state->getSVal(arg0Expr));
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000506
Ted Kremenek846eabd2010-12-01 21:28:31 +0000507 SValBuilder &svalBuilder = C.getSValBuilder();
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000508
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000509 DefinedOrUnknownSVal PtrEQ =
510 svalBuilder.evalEQ(state, arg0Val, svalBuilder.makeNull());
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000511
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000512 // Get the size argument. If there is no size arg then give up.
513 const Expr *Arg1 = CE->getArg(1);
514 if (!Arg1)
515 return;
516
517 // Get the value of the size argument.
518 DefinedOrUnknownSVal Arg1Val =
519 cast<DefinedOrUnknownSVal>(state->getSVal(Arg1));
520
521 // Compare the size argument to 0.
522 DefinedOrUnknownSVal SizeZero =
523 svalBuilder.evalEQ(state, Arg1Val,
524 svalBuilder.makeIntValWithPtrWidth(0, false));
525
526 // If the ptr is NULL and the size is not 0, the call is equivalent to
527 // malloc(size).
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000528 const ProgramState *stateEqual = state->assume(PtrEQ, true);
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000529 if (stateEqual && state->assume(SizeZero, false)) {
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000530 // Hack: set the NULL symbolic region to released to suppress false warning.
531 // In the future we should add more states for allocated regions, e.g.,
532 // CheckedNull, CheckedNonNull.
533
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000534 SymbolRef Sym = arg0Val.getAsLocSymbol();
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000535 if (Sym)
536 stateEqual = stateEqual->set<RegionState>(Sym, RefState::getReleased(CE));
537
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000538 const ProgramState *stateMalloc = MallocMemAux(C, CE, CE->getArg(1),
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000539 UndefinedVal(), stateEqual);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000540 C.addTransition(stateMalloc);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000541 }
542
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000543 if (const ProgramState *stateNotEqual = state->assume(PtrEQ, false)) {
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000544 // If the size is 0, free the memory.
Ted Kremeneke3659a72012-01-04 23:48:37 +0000545 if (const ProgramState *stateSizeZero =
546 stateNotEqual->assume(SizeZero, true))
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000547 if (const ProgramState *stateFree =
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000548 FreeMemAux(C, CE, stateSizeZero, 0, false)) {
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000549
Zhongxing Xud56763f2011-09-01 04:53:59 +0000550 // Bind the return value to NULL because it is now free.
Anna Zaks0bd6b112011-10-26 21:06:34 +0000551 C.addTransition(stateFree->BindExpr(CE, svalBuilder.makeNull(), true));
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000552 }
Ted Kremeneke3659a72012-01-04 23:48:37 +0000553 if (const ProgramState *stateSizeNotZero =
554 stateNotEqual->assume(SizeZero,false))
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000555 if (const ProgramState *stateFree = FreeMemAux(C, CE, stateSizeNotZero,
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000556 0, false)) {
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000557 // FIXME: We should copy the content of the original buffer.
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000558 const ProgramState *stateRealloc = MallocMemAux(C, CE, CE->getArg(1),
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000559 UnknownVal(), stateFree);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000560 C.addTransition(stateRealloc);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000561 }
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000562 }
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000563}
Zhongxing Xu7b760962009-11-13 07:25:27 +0000564
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000565void MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE) {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000566 const ProgramState *state = C.getState();
Ted Kremenek846eabd2010-12-01 21:28:31 +0000567 SValBuilder &svalBuilder = C.getSValBuilder();
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000568
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000569 SVal count = state->getSVal(CE->getArg(0));
570 SVal elementSize = state->getSVal(CE->getArg(1));
571 SVal TotalSize = svalBuilder.evalBinOp(state, BO_Mul, count, elementSize,
572 svalBuilder.getContext().getSizeType());
573 SVal zeroVal = svalBuilder.makeZeroVal(svalBuilder.getContext().CharTy);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000574
Anna Zaks0bd6b112011-10-26 21:06:34 +0000575 C.addTransition(MallocMemAux(C, CE, TotalSize, zeroVal, state));
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000576}
577
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000578void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper,
579 CheckerContext &C) const
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000580{
Zhongxing Xu173ff562010-08-15 08:19:57 +0000581 if (!SymReaper.hasDeadSymbols())
582 return;
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000583
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000584 const ProgramState *state = C.getState();
Zhongxing Xu173ff562010-08-15 08:19:57 +0000585 RegionStateTy RS = state->get<RegionState>();
Jordy Rose90760142010-08-18 04:33:47 +0000586 RegionStateTy::Factory &F = state->get_context<RegionState>();
Zhongxing Xu173ff562010-08-15 08:19:57 +0000587
Ted Kremenek217470e2011-07-28 23:07:51 +0000588 bool generateReport = false;
589
Zhongxing Xu173ff562010-08-15 08:19:57 +0000590 for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
591 if (SymReaper.isDead(I->first)) {
Ted Kremenek217470e2011-07-28 23:07:51 +0000592 if (I->second.isAllocated())
593 generateReport = true;
Jordy Rose90760142010-08-18 04:33:47 +0000594
595 // Remove the dead symbol from the map.
Ted Kremenek3baf6722010-11-24 00:54:37 +0000596 RS = F.remove(RS, I->first);
Ted Kremenek217470e2011-07-28 23:07:51 +0000597
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000598 }
599 }
Ted Kremenek217470e2011-07-28 23:07:51 +0000600
Anna Zaks0bd6b112011-10-26 21:06:34 +0000601 ExplodedNode *N = C.addTransition(state->set<RegionState>(RS));
Ted Kremenek217470e2011-07-28 23:07:51 +0000602
603 // FIXME: This does not handle when we have multiple leaks at a single
604 // place.
605 if (N && generateReport) {
606 if (!BT_Leak)
607 BT_Leak.reset(new BuiltinBug("Memory leak",
608 "Allocated memory never released. Potential memory leak."));
609 // FIXME: where it is allocated.
610 BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
611 C.EmitReport(R);
612 }
Zhongxing Xu7b760962009-11-13 07:25:27 +0000613}
Zhongxing Xu243fde92009-11-17 07:54:15 +0000614
Anna Zaksaf498a22011-10-25 19:56:48 +0000615void MallocChecker::checkEndPath(CheckerContext &Ctx) const {
616 const ProgramState *state = Ctx.getState();
Jordy Rose09cef092010-08-18 04:26:59 +0000617 RegionStateTy M = state->get<RegionState>();
Zhongxing Xu243fde92009-11-17 07:54:15 +0000618
Jordy Rose09cef092010-08-18 04:26:59 +0000619 for (RegionStateTy::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Zhongxing Xu243fde92009-11-17 07:54:15 +0000620 RefState RS = I->second;
621 if (RS.isAllocated()) {
Anna Zaks0bd6b112011-10-26 21:06:34 +0000622 ExplodedNode *N = Ctx.addTransition(state);
Zhongxing Xu243fde92009-11-17 07:54:15 +0000623 if (N) {
624 if (!BT_Leak)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000625 BT_Leak.reset(new BuiltinBug("Memory leak",
626 "Allocated memory never released. Potential memory leak."));
Zhongxing Xu243fde92009-11-17 07:54:15 +0000627 BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
Anna Zaksaf498a22011-10-25 19:56:48 +0000628 Ctx.EmitReport(R);
Zhongxing Xu243fde92009-11-17 07:54:15 +0000629 }
630 }
631 }
632}
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000633
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000634void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000635 const Expr *retExpr = S->getRetValue();
636 if (!retExpr)
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000637 return;
638
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000639 const ProgramState *state = C.getState();
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000640
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000641 SymbolRef Sym = state->getSVal(retExpr).getAsSymbol();
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000642 if (!Sym)
643 return;
644
645 const RefState *RS = state->get<RegionState>(Sym);
646 if (!RS)
647 return;
648
649 // FIXME: check other cases.
650 if (RS->isAllocated())
651 state = state->set<RegionState>(Sym, RefState::getEscaped(S));
652
Anna Zaks0bd6b112011-10-26 21:06:34 +0000653 C.addTransition(state);
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000654}
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000655
Ted Kremeneke3659a72012-01-04 23:48:37 +0000656const ProgramState *MallocChecker::evalAssume(const ProgramState *state,
657 SVal Cond,
658 bool Assumption) const {
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000659 // If a symblic region is assumed to NULL, set its state to AllocateFailed.
660 // FIXME: should also check symbols assumed to non-null.
661
662 RegionStateTy RS = state->get<RegionState>();
663
664 for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
Zhongxing Xu2bfa3012011-04-02 03:20:45 +0000665 // If the symbol is assumed to NULL, this will return an APSInt*.
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000666 if (state->getSymVal(I.getKey()))
667 state = state->set<RegionState>(I.getKey(),RefState::getAllocateFailed());
668 }
669
670 return state;
671}
Zhongxing Xuc8023782010-03-10 04:58:55 +0000672
673// Check if the location is a freed symbolic region.
Anna Zaks390909c2011-10-06 00:43:15 +0000674void MallocChecker::checkLocation(SVal l, bool isLoad, const Stmt *S,
675 CheckerContext &C) const {
Zhongxing Xuc8023782010-03-10 04:58:55 +0000676 SymbolRef Sym = l.getLocSymbolInBase();
677 if (Sym) {
678 const RefState *RS = C.getState()->get<RegionState>(Sym);
Ted Kremenekcea68652010-08-06 21:12:49 +0000679 if (RS && RS->isReleased()) {
Anna Zaks0bd6b112011-10-26 21:06:34 +0000680 if (ExplodedNode *N = C.addTransition()) {
Zhongxing Xuc8023782010-03-10 04:58:55 +0000681 if (!BT_UseFree)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000682 BT_UseFree.reset(new BuiltinBug("Use dynamically allocated memory "
683 "after it is freed."));
Zhongxing Xuc8023782010-03-10 04:58:55 +0000684
685 BugReport *R = new BugReport(*BT_UseFree, BT_UseFree->getDescription(),
686 N);
687 C.EmitReport(R);
688 }
Ted Kremenekcea68652010-08-06 21:12:49 +0000689 }
Zhongxing Xuc8023782010-03-10 04:58:55 +0000690 }
691}
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000692
Anna Zaks390909c2011-10-06 00:43:15 +0000693void MallocChecker::checkBind(SVal location, SVal val,
694 const Stmt *BindS, CheckerContext &C) const {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000695 // The PreVisitBind implements the same algorithm as already used by the
696 // Objective C ownership checker: if the pointer escaped from this scope by
697 // assignment, let it go. However, assigning to fields of a stack-storage
698 // structure does not transfer ownership.
699
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000700 const ProgramState *state = C.getState();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000701 DefinedOrUnknownSVal l = cast<DefinedOrUnknownSVal>(location);
702
703 // Check for null dereferences.
704 if (!isa<Loc>(l))
705 return;
706
707 // Before checking if the state is null, check if 'val' has a RefState.
708 // Only then should we check for null and bifurcate the state.
709 SymbolRef Sym = val.getLocSymbolInBase();
710 if (Sym) {
711 if (const RefState *RS = state->get<RegionState>(Sym)) {
712 // If ptr is NULL, no operation is performed.
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000713 const ProgramState *notNullState, *nullState;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000714 llvm::tie(notNullState, nullState) = state->assume(l);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000715
716 // Generate a transition for 'nullState' to record the assumption
717 // that the state was null.
718 if (nullState)
Anna Zaks0bd6b112011-10-26 21:06:34 +0000719 C.addTransition(nullState);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000720
721 if (!notNullState)
722 return;
723
724 if (RS->isAllocated()) {
725 // Something we presently own is being assigned somewhere.
726 const MemRegion *AR = location.getAsRegion();
727 if (!AR)
728 return;
729 AR = AR->StripCasts()->getBaseRegion();
730 do {
731 // If it is on the stack, we still own it.
732 if (AR->hasStackNonParametersStorage())
733 break;
734
735 // If the state can't represent this binding, we still own it.
Ted Kremenekdde201b2010-08-06 21:12:55 +0000736 if (notNullState == (notNullState->bindLoc(cast<Loc>(location),
737 UnknownVal())))
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000738 break;
739
740 // We no longer own this pointer.
Ted Kremenekdde201b2010-08-06 21:12:55 +0000741 notNullState =
742 notNullState->set<RegionState>(Sym,
Anna Zaks390909c2011-10-06 00:43:15 +0000743 RefState::getRelinquished(BindS));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000744 }
745 while (false);
746 }
Anna Zaks0bd6b112011-10-26 21:06:34 +0000747 C.addTransition(notNullState);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000748 }
749 }
750}
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000751
752void ento::registerMallocChecker(CheckerManager &mgr) {
753 mgr.registerChecker<MallocChecker>();
754}