blob: edea33ea690dcc45230cccfb4dd4467ad8d8a864 [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
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000067class MallocChecker : public Checker<eval::Call, check::DeadSymbols, check::EndPath, check::PreStmt<ReturnStmt>, check::Location,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000068 check::Bind, eval::Assume> {
69 mutable llvm::OwningPtr<BuiltinBug> BT_DoubleFree;
70 mutable llvm::OwningPtr<BuiltinBug> BT_Leak;
71 mutable llvm::OwningPtr<BuiltinBug> BT_UseFree;
72 mutable llvm::OwningPtr<BuiltinBug> BT_UseRelinquished;
73 mutable llvm::OwningPtr<BuiltinBug> BT_BadFree;
74 mutable IdentifierInfo *II_malloc, *II_free, *II_realloc, *II_calloc;
Zhongxing Xu589c0f22009-11-12 08:38:56 +000075
76public:
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000077 MallocChecker() : II_malloc(0), II_free(0), II_realloc(0), II_calloc(0) {}
78
79 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
80 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
Anna Zaksaf498a22011-10-25 19:56:48 +000081 void checkEndPath(CheckerContext &C) const;
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000082 void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
Ted Kremenek18c66fd2011-08-15 22:09:50 +000083 const ProgramState *evalAssume(const ProgramState *state, SVal Cond,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000084 bool Assumption) const;
Anna Zaks390909c2011-10-06 00:43:15 +000085 void checkLocation(SVal l, bool isLoad, const Stmt *S,
86 CheckerContext &C) const;
87 void checkBind(SVal location, SVal val, const Stmt*S,
88 CheckerContext &C) const;
Zhongxing Xub94b81a2009-12-31 06:13:07 +000089
Zhongxing Xu7b760962009-11-13 07:25:27 +000090private:
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000091 static void MallocMem(CheckerContext &C, const CallExpr *CE);
92 static void MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,
93 const OwnershipAttr* Att);
Ted Kremenek18c66fd2011-08-15 22:09:50 +000094 static const ProgramState *MallocMemAux(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000095 const Expr *SizeEx, SVal Init,
Ted Kremenek18c66fd2011-08-15 22:09:50 +000096 const ProgramState *state) {
Zhongxing Xua5ce9662010-06-01 03:01:33 +000097 return MallocMemAux(C, CE, state->getSVal(SizeEx), Init, state);
98 }
Ted Kremenek18c66fd2011-08-15 22:09:50 +000099 static const ProgramState *MallocMemAux(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000100 SVal SizeEx, SVal Init,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000101 const ProgramState *state);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000102
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000103 void FreeMem(CheckerContext &C, const CallExpr *CE) const;
Jordy Rose2a479922010-08-12 08:54:03 +0000104 void FreeMemAttr(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000105 const OwnershipAttr* Att) const;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000106 const ProgramState *FreeMemAux(CheckerContext &C, const CallExpr *CE,
107 const ProgramState *state, unsigned Num, bool Hold) const;
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000108
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000109 void ReallocMem(CheckerContext &C, const CallExpr *CE) const;
110 static void CallocMem(CheckerContext &C, const CallExpr *CE);
Jordy Rose43859f62010-06-07 19:32:37 +0000111
Ted Kremenek9c378f72011-08-12 23:37:29 +0000112 static bool SummarizeValue(raw_ostream &os, SVal V);
113 static bool SummarizeRegion(raw_ostream &os, const MemRegion *MR);
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000114 void ReportBadFree(CheckerContext &C, SVal ArgVal, SourceRange range) const;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000115};
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000116} // end anonymous namespace
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000117
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000118typedef llvm::ImmutableMap<SymbolRef, RefState> RegionStateTy;
119
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000120namespace clang {
Ted Kremenek9ef65372010-12-23 07:20:52 +0000121namespace ento {
Zhongxing Xu243fde92009-11-17 07:54:15 +0000122 template <>
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000123 struct ProgramStateTrait<RegionState>
124 : public ProgramStatePartialTrait<RegionStateTy> {
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000125 static void *GDMIndex() { static int x; return &x; }
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000126 };
127}
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000128}
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000129
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000130bool MallocChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000131 const ProgramState *state = C.getState();
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000132 const Expr *Callee = CE->getCallee();
Ted Kremenek13976632010-02-08 16:18:51 +0000133 SVal L = state->getSVal(Callee);
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000134
135 const FunctionDecl *FD = L.getAsFunctionDecl();
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000136 if (!FD)
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000137 return false;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000138
139 ASTContext &Ctx = C.getASTContext();
140 if (!II_malloc)
141 II_malloc = &Ctx.Idents.get("malloc");
142 if (!II_free)
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000143 II_free = &Ctx.Idents.get("free");
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000144 if (!II_realloc)
145 II_realloc = &Ctx.Idents.get("realloc");
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000146 if (!II_calloc)
147 II_calloc = &Ctx.Idents.get("calloc");
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000148
149 if (FD->getIdentifier() == II_malloc) {
150 MallocMem(C, CE);
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000151 return true;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000152 }
153
154 if (FD->getIdentifier() == II_free) {
155 FreeMem(C, CE);
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000156 return true;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000157 }
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000158
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000159 if (FD->getIdentifier() == II_realloc) {
160 ReallocMem(C, CE);
161 return true;
162 }
163
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000164 if (FD->getIdentifier() == II_calloc) {
165 CallocMem(C, CE);
166 return true;
167 }
168
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000169 // Check all the attributes, if there are any.
170 // There can be multiple of these attributes.
171 bool rv = false;
172 if (FD->hasAttrs()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000173 for (specific_attr_iterator<OwnershipAttr>
174 i = FD->specific_attr_begin<OwnershipAttr>(),
175 e = FD->specific_attr_end<OwnershipAttr>();
176 i != e; ++i) {
177 switch ((*i)->getOwnKind()) {
178 case OwnershipAttr::Returns: {
179 MallocMemReturnsAttr(C, CE, *i);
Jordy Rose2a479922010-08-12 08:54:03 +0000180 rv = true;
181 break;
Sean Huntcf807c42010-08-18 23:23:40 +0000182 }
183 case OwnershipAttr::Takes:
184 case OwnershipAttr::Holds: {
185 FreeMemAttr(C, CE, *i);
Jordy Rose2a479922010-08-12 08:54:03 +0000186 rv = true;
187 break;
Sean Huntcf807c42010-08-18 23:23:40 +0000188 }
Jordy Rose2a479922010-08-12 08:54:03 +0000189 default:
Jordy Rose2a479922010-08-12 08:54:03 +0000190 break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000191 }
192 }
193 }
194 return rv;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000195}
196
197void MallocChecker::MallocMem(CheckerContext &C, const CallExpr *CE) {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000198 const ProgramState *state = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(),
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000199 C.getState());
Anna Zaks063e0882011-10-25 19:57:06 +0000200 C.generateNode(state);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000201}
202
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000203void MallocChecker::MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,
204 const OwnershipAttr* Att) {
Sean Huntcf807c42010-08-18 23:23:40 +0000205 if (Att->getModule() != "malloc")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000206 return;
207
Sean Huntcf807c42010-08-18 23:23:40 +0000208 OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000209 if (I != E) {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000210 const ProgramState *state =
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000211 MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), C.getState());
Anna Zaks063e0882011-10-25 19:57:06 +0000212 C.generateNode(state);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000213 return;
214 }
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000215 const ProgramState *state = MallocMemAux(C, CE, UnknownVal(), UndefinedVal(),
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000216 C.getState());
Anna Zaks063e0882011-10-25 19:57:06 +0000217 C.generateNode(state);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000218}
219
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000220const ProgramState *MallocChecker::MallocMemAux(CheckerContext &C,
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000221 const CallExpr *CE,
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000222 SVal Size, SVal Init,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000223 const ProgramState *state) {
Anna Zaks5d0ea6d2011-10-04 20:43:05 +0000224 unsigned Count = C.getCurrentBlockCount();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000225 SValBuilder &svalBuilder = C.getSValBuilder();
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000226
Jordy Rose32f26562010-07-04 00:00:41 +0000227 // Set the return value.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000228 SVal retVal = svalBuilder.getConjuredSymbolVal(NULL, CE, CE->getType(), Count);
229 state = state->BindExpr(CE, retVal);
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000230
Jordy Rose32f26562010-07-04 00:00:41 +0000231 // Fill the region with the initialization value.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000232 state = state->bindDefault(retVal, Init);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000233
Jordy Rose32f26562010-07-04 00:00:41 +0000234 // Set the region's extent equal to the Size parameter.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000235 const SymbolicRegion *R = cast<SymbolicRegion>(retVal.getAsRegion());
236 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
Jordy Rose32f26562010-07-04 00:00:41 +0000237 DefinedOrUnknownSVal DefinedSize = cast<DefinedOrUnknownSVal>(Size);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000238 DefinedOrUnknownSVal extentMatchesSize =
Ted Kremenek9c149532010-12-01 21:57:22 +0000239 svalBuilder.evalEQ(state, Extent, DefinedSize);
Jordy Rose32f26562010-07-04 00:00:41 +0000240
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000241 state = state->assume(extentMatchesSize, true);
242 assert(state);
243
244 SymbolRef Sym = retVal.getAsLocSymbol();
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000245 assert(Sym);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000246
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000247 // Set the symbol's state to Allocated.
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000248 return state->set<RegionState>(Sym, RefState::getAllocateUnchecked(CE));
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000249}
250
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000251void MallocChecker::FreeMem(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000252 const ProgramState *state = FreeMemAux(C, CE, C.getState(), 0, false);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000253
254 if (state)
Anna Zaks063e0882011-10-25 19:57:06 +0000255 C.generateNode(state);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000256}
257
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000258void MallocChecker::FreeMemAttr(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000259 const OwnershipAttr* Att) const {
Sean Huntcf807c42010-08-18 23:23:40 +0000260 if (Att->getModule() != "malloc")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000261 return;
262
Sean Huntcf807c42010-08-18 23:23:40 +0000263 for (OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
264 I != E; ++I) {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000265 const ProgramState *state = FreeMemAux(C, CE, C.getState(), *I,
Sean Huntcf807c42010-08-18 23:23:40 +0000266 Att->getOwnKind() == OwnershipAttr::Holds);
267 if (state)
Anna Zaks063e0882011-10-25 19:57:06 +0000268 C.generateNode(state);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000269 }
270}
271
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000272const ProgramState *MallocChecker::FreeMemAux(CheckerContext &C, const CallExpr *CE,
273 const ProgramState *state, unsigned Num,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000274 bool Hold) const {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000275 const Expr *ArgExpr = CE->getArg(Num);
Jordy Rose43859f62010-06-07 19:32:37 +0000276 SVal ArgVal = state->getSVal(ArgExpr);
Zhongxing Xu181cc3d2010-02-14 06:49:48 +0000277
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000278 DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(ArgVal);
279
280 // Check for null dereferences.
281 if (!isa<Loc>(location))
Zhongxing Xu181cc3d2010-02-14 06:49:48 +0000282 return state;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000283
284 // FIXME: Technically using 'Assume' here can result in a path
285 // bifurcation. In such cases we need to return two states, not just one.
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000286 const ProgramState *notNullState, *nullState;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000287 llvm::tie(notNullState, nullState) = state->assume(location);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000288
289 // The explicit NULL case, no operation is performed.
290 if (nullState && !notNullState)
291 return nullState;
292
293 assert(notNullState);
294
Jordy Rose43859f62010-06-07 19:32:37 +0000295 // Unknown values could easily be okay
296 // Undefined values are handled elsewhere
297 if (ArgVal.isUnknownOrUndef())
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000298 return notNullState;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000299
Jordy Rose43859f62010-06-07 19:32:37 +0000300 const MemRegion *R = ArgVal.getAsRegion();
301
302 // Nonlocs can't be freed, of course.
303 // Non-region locations (labels and fixed addresses) also shouldn't be freed.
304 if (!R) {
305 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
306 return NULL;
307 }
308
309 R = R->StripCasts();
310
311 // Blocks might show up as heap data, but should not be free()d
312 if (isa<BlockDataRegion>(R)) {
313 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
314 return NULL;
315 }
316
317 const MemSpaceRegion *MS = R->getMemorySpace();
318
319 // Parameters, locals, statics, and globals shouldn't be freed.
320 if (!(isa<UnknownSpaceRegion>(MS) || isa<HeapSpaceRegion>(MS))) {
321 // FIXME: at the time this code was written, malloc() regions were
322 // represented by conjured symbols, which are all in UnknownSpaceRegion.
323 // This means that there isn't actually anything from HeapSpaceRegion
324 // that should be freed, even though we allow it here.
325 // Of course, free() can work on memory allocated outside the current
326 // function, so UnknownSpaceRegion is always a possibility.
327 // False negatives are better than false positives.
328
329 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
330 return NULL;
331 }
332
333 const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R);
334 // Various cases could lead to non-symbol values here.
335 // For now, ignore them.
336 if (!SR)
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000337 return notNullState;
Jordy Rose43859f62010-06-07 19:32:37 +0000338
339 SymbolRef Sym = SR->getSymbol();
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000340 const RefState *RS = state->get<RegionState>(Sym);
Zhongxing Xu7e3cda92010-01-18 03:27:34 +0000341
342 // If the symbol has not been tracked, return. This is possible when free() is
343 // called on a pointer that does not get its pointee directly from malloc().
344 // Full support of this requires inter-procedural analysis.
345 if (!RS)
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000346 return notNullState;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000347
348 // Check double free.
Zhongxing Xu243fde92009-11-17 07:54:15 +0000349 if (RS->isReleased()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000350 if (ExplodedNode *N = C.generateSink()) {
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000351 if (!BT_DoubleFree)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000352 BT_DoubleFree.reset(
353 new BuiltinBug("Double free",
354 "Try to free a memory block that has been released"));
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000355 // FIXME: should find where it's freed last time.
356 BugReport *R = new BugReport(*BT_DoubleFree,
Benjamin Kramerd02e2322009-11-14 12:08:24 +0000357 BT_DoubleFree->getDescription(), N);
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000358 C.EmitReport(R);
359 }
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000360 return NULL;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000361 }
362
363 // Normal free.
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000364 if (Hold)
365 return notNullState->set<RegionState>(Sym, RefState::getRelinquished(CE));
366 return notNullState->set<RegionState>(Sym, RefState::getReleased(CE));
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000367}
368
Ted Kremenek9c378f72011-08-12 23:37:29 +0000369bool MallocChecker::SummarizeValue(raw_ostream &os, SVal V) {
Jordy Rose43859f62010-06-07 19:32:37 +0000370 if (nonloc::ConcreteInt *IntVal = dyn_cast<nonloc::ConcreteInt>(&V))
371 os << "an integer (" << IntVal->getValue() << ")";
372 else if (loc::ConcreteInt *ConstAddr = dyn_cast<loc::ConcreteInt>(&V))
373 os << "a constant address (" << ConstAddr->getValue() << ")";
374 else if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&V))
Chris Lattner68106302011-02-17 05:38:27 +0000375 os << "the address of the label '" << Label->getLabel()->getName() << "'";
Jordy Rose43859f62010-06-07 19:32:37 +0000376 else
377 return false;
378
379 return true;
380}
381
Ted Kremenek9c378f72011-08-12 23:37:29 +0000382bool MallocChecker::SummarizeRegion(raw_ostream &os,
Jordy Rose43859f62010-06-07 19:32:37 +0000383 const MemRegion *MR) {
384 switch (MR->getKind()) {
385 case MemRegion::FunctionTextRegionKind: {
386 const FunctionDecl *FD = cast<FunctionTextRegion>(MR)->getDecl();
387 if (FD)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000388 os << "the address of the function '" << *FD << '\'';
Jordy Rose43859f62010-06-07 19:32:37 +0000389 else
390 os << "the address of a function";
391 return true;
392 }
393 case MemRegion::BlockTextRegionKind:
394 os << "block text";
395 return true;
396 case MemRegion::BlockDataRegionKind:
397 // FIXME: where the block came from?
398 os << "a block";
399 return true;
400 default: {
401 const MemSpaceRegion *MS = MR->getMemorySpace();
402
403 switch (MS->getKind()) {
404 case MemRegion::StackLocalsSpaceRegionKind: {
405 const VarRegion *VR = dyn_cast<VarRegion>(MR);
406 const VarDecl *VD;
407 if (VR)
408 VD = VR->getDecl();
409 else
410 VD = NULL;
411
412 if (VD)
413 os << "the address of the local variable '" << VD->getName() << "'";
414 else
415 os << "the address of a local stack variable";
416 return true;
417 }
418 case MemRegion::StackArgumentsSpaceRegionKind: {
419 const VarRegion *VR = dyn_cast<VarRegion>(MR);
420 const VarDecl *VD;
421 if (VR)
422 VD = VR->getDecl();
423 else
424 VD = NULL;
425
426 if (VD)
427 os << "the address of the parameter '" << VD->getName() << "'";
428 else
429 os << "the address of a parameter";
430 return true;
431 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000432 case MemRegion::NonStaticGlobalSpaceRegionKind:
433 case MemRegion::StaticGlobalSpaceRegionKind: {
Jordy Rose43859f62010-06-07 19:32:37 +0000434 const VarRegion *VR = dyn_cast<VarRegion>(MR);
435 const VarDecl *VD;
436 if (VR)
437 VD = VR->getDecl();
438 else
439 VD = NULL;
440
441 if (VD) {
442 if (VD->isStaticLocal())
443 os << "the address of the static variable '" << VD->getName() << "'";
444 else
445 os << "the address of the global variable '" << VD->getName() << "'";
446 } else
447 os << "the address of a global variable";
448 return true;
449 }
450 default:
451 return false;
452 }
453 }
454 }
455}
456
457void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000458 SourceRange range) const {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000459 if (ExplodedNode *N = C.generateSink()) {
Jordy Rose43859f62010-06-07 19:32:37 +0000460 if (!BT_BadFree)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000461 BT_BadFree.reset(new BuiltinBug("Bad free"));
Jordy Rose43859f62010-06-07 19:32:37 +0000462
463 llvm::SmallString<100> buf;
464 llvm::raw_svector_ostream os(buf);
465
466 const MemRegion *MR = ArgVal.getAsRegion();
467 if (MR) {
468 while (const ElementRegion *ER = dyn_cast<ElementRegion>(MR))
469 MR = ER->getSuperRegion();
470
471 // Special case for alloca()
472 if (isa<AllocaRegion>(MR))
473 os << "Argument to free() was allocated by alloca(), not malloc()";
474 else {
475 os << "Argument to free() is ";
476 if (SummarizeRegion(os, MR))
477 os << ", which is not memory allocated by malloc()";
478 else
479 os << "not memory allocated by malloc()";
480 }
481 } else {
482 os << "Argument to free() is ";
483 if (SummarizeValue(os, ArgVal))
484 os << ", which is not memory allocated by malloc()";
485 else
486 os << "not memory allocated by malloc()";
487 }
488
Anna Zakse172e8b2011-08-17 23:00:25 +0000489 BugReport *R = new BugReport(*BT_BadFree, os.str(), N);
Jordy Rose43859f62010-06-07 19:32:37 +0000490 R->addRange(range);
491 C.EmitReport(R);
492 }
493}
494
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000495void MallocChecker::ReallocMem(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000496 const ProgramState *state = C.getState();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000497 const Expr *arg0Expr = CE->getArg(0);
498 DefinedOrUnknownSVal arg0Val
499 = cast<DefinedOrUnknownSVal>(state->getSVal(arg0Expr));
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000500
Ted Kremenek846eabd2010-12-01 21:28:31 +0000501 SValBuilder &svalBuilder = C.getSValBuilder();
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000502
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000503 DefinedOrUnknownSVal PtrEQ =
504 svalBuilder.evalEQ(state, arg0Val, svalBuilder.makeNull());
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000505
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000506 // Get the size argument. If there is no size arg then give up.
507 const Expr *Arg1 = CE->getArg(1);
508 if (!Arg1)
509 return;
510
511 // Get the value of the size argument.
512 DefinedOrUnknownSVal Arg1Val =
513 cast<DefinedOrUnknownSVal>(state->getSVal(Arg1));
514
515 // Compare the size argument to 0.
516 DefinedOrUnknownSVal SizeZero =
517 svalBuilder.evalEQ(state, Arg1Val,
518 svalBuilder.makeIntValWithPtrWidth(0, false));
519
520 // If the ptr is NULL and the size is not 0, the call is equivalent to
521 // malloc(size).
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000522 const ProgramState *stateEqual = state->assume(PtrEQ, true);
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000523 if (stateEqual && state->assume(SizeZero, false)) {
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000524 // Hack: set the NULL symbolic region to released to suppress false warning.
525 // In the future we should add more states for allocated regions, e.g.,
526 // CheckedNull, CheckedNonNull.
527
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000528 SymbolRef Sym = arg0Val.getAsLocSymbol();
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000529 if (Sym)
530 stateEqual = stateEqual->set<RegionState>(Sym, RefState::getReleased(CE));
531
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000532 const ProgramState *stateMalloc = MallocMemAux(C, CE, CE->getArg(1),
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000533 UndefinedVal(), stateEqual);
Anna Zaks063e0882011-10-25 19:57:06 +0000534 C.generateNode(stateMalloc);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000535 }
536
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000537 if (const ProgramState *stateNotEqual = state->assume(PtrEQ, false)) {
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000538 // If the size is 0, free the memory.
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000539 if (const ProgramState *stateSizeZero = stateNotEqual->assume(SizeZero, true))
540 if (const ProgramState *stateFree =
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000541 FreeMemAux(C, CE, stateSizeZero, 0, false)) {
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000542
Zhongxing Xud56763f2011-09-01 04:53:59 +0000543 // Bind the return value to NULL because it is now free.
Anna Zaks063e0882011-10-25 19:57:06 +0000544 C.generateNode(stateFree->BindExpr(CE, svalBuilder.makeNull(), true));
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000545 }
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000546 if (const ProgramState *stateSizeNotZero = stateNotEqual->assume(SizeZero,false))
547 if (const ProgramState *stateFree = FreeMemAux(C, CE, stateSizeNotZero,
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000548 0, false)) {
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000549 // FIXME: We should copy the content of the original buffer.
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000550 const ProgramState *stateRealloc = MallocMemAux(C, CE, CE->getArg(1),
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000551 UnknownVal(), stateFree);
Anna Zaks063e0882011-10-25 19:57:06 +0000552 C.generateNode(stateRealloc);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000553 }
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000554 }
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000555}
Zhongxing Xu7b760962009-11-13 07:25:27 +0000556
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000557void MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE) {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000558 const ProgramState *state = C.getState();
Ted Kremenek846eabd2010-12-01 21:28:31 +0000559 SValBuilder &svalBuilder = C.getSValBuilder();
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000560
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000561 SVal count = state->getSVal(CE->getArg(0));
562 SVal elementSize = state->getSVal(CE->getArg(1));
563 SVal TotalSize = svalBuilder.evalBinOp(state, BO_Mul, count, elementSize,
564 svalBuilder.getContext().getSizeType());
565 SVal zeroVal = svalBuilder.makeZeroVal(svalBuilder.getContext().CharTy);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000566
Anna Zaks063e0882011-10-25 19:57:06 +0000567 C.generateNode(MallocMemAux(C, CE, TotalSize, zeroVal, state));
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000568}
569
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000570void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper,
571 CheckerContext &C) const
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000572{
Zhongxing Xu173ff562010-08-15 08:19:57 +0000573 if (!SymReaper.hasDeadSymbols())
574 return;
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000575
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000576 const ProgramState *state = C.getState();
Zhongxing Xu173ff562010-08-15 08:19:57 +0000577 RegionStateTy RS = state->get<RegionState>();
Jordy Rose90760142010-08-18 04:33:47 +0000578 RegionStateTy::Factory &F = state->get_context<RegionState>();
Zhongxing Xu173ff562010-08-15 08:19:57 +0000579
Ted Kremenek217470e2011-07-28 23:07:51 +0000580 bool generateReport = false;
581
Zhongxing Xu173ff562010-08-15 08:19:57 +0000582 for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
583 if (SymReaper.isDead(I->first)) {
Ted Kremenek217470e2011-07-28 23:07:51 +0000584 if (I->second.isAllocated())
585 generateReport = true;
Jordy Rose90760142010-08-18 04:33:47 +0000586
587 // Remove the dead symbol from the map.
Ted Kremenek3baf6722010-11-24 00:54:37 +0000588 RS = F.remove(RS, I->first);
Ted Kremenek217470e2011-07-28 23:07:51 +0000589
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000590 }
591 }
Ted Kremenek217470e2011-07-28 23:07:51 +0000592
593 ExplodedNode *N = C.generateNode(state->set<RegionState>(RS));
594
595 // FIXME: This does not handle when we have multiple leaks at a single
596 // place.
597 if (N && generateReport) {
598 if (!BT_Leak)
599 BT_Leak.reset(new BuiltinBug("Memory leak",
600 "Allocated memory never released. Potential memory leak."));
601 // FIXME: where it is allocated.
602 BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
603 C.EmitReport(R);
604 }
Zhongxing Xu7b760962009-11-13 07:25:27 +0000605}
Zhongxing Xu243fde92009-11-17 07:54:15 +0000606
Anna Zaksaf498a22011-10-25 19:56:48 +0000607void MallocChecker::checkEndPath(CheckerContext &Ctx) const {
608 const ProgramState *state = Ctx.getState();
Jordy Rose09cef092010-08-18 04:26:59 +0000609 RegionStateTy M = state->get<RegionState>();
Zhongxing Xu243fde92009-11-17 07:54:15 +0000610
Jordy Rose09cef092010-08-18 04:26:59 +0000611 for (RegionStateTy::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Zhongxing Xu243fde92009-11-17 07:54:15 +0000612 RefState RS = I->second;
613 if (RS.isAllocated()) {
Anna Zaksaf498a22011-10-25 19:56:48 +0000614 ExplodedNode *N = Ctx.generateNode(state);
Zhongxing Xu243fde92009-11-17 07:54:15 +0000615 if (N) {
616 if (!BT_Leak)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000617 BT_Leak.reset(new BuiltinBug("Memory leak",
618 "Allocated memory never released. Potential memory leak."));
Zhongxing Xu243fde92009-11-17 07:54:15 +0000619 BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
Anna Zaksaf498a22011-10-25 19:56:48 +0000620 Ctx.EmitReport(R);
Zhongxing Xu243fde92009-11-17 07:54:15 +0000621 }
622 }
623 }
624}
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000625
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000626void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000627 const Expr *retExpr = S->getRetValue();
628 if (!retExpr)
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000629 return;
630
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000631 const ProgramState *state = C.getState();
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000632
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000633 SymbolRef Sym = state->getSVal(retExpr).getAsSymbol();
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000634 if (!Sym)
635 return;
636
637 const RefState *RS = state->get<RegionState>(Sym);
638 if (!RS)
639 return;
640
641 // FIXME: check other cases.
642 if (RS->isAllocated())
643 state = state->set<RegionState>(Sym, RefState::getEscaped(S));
644
Anna Zaks063e0882011-10-25 19:57:06 +0000645 C.generateNode(state);
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000646}
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000647
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000648const ProgramState *MallocChecker::evalAssume(const ProgramState *state, SVal Cond,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000649 bool Assumption) const {
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000650 // If a symblic region is assumed to NULL, set its state to AllocateFailed.
651 // FIXME: should also check symbols assumed to non-null.
652
653 RegionStateTy RS = state->get<RegionState>();
654
655 for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
Zhongxing Xu2bfa3012011-04-02 03:20:45 +0000656 // If the symbol is assumed to NULL, this will return an APSInt*.
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000657 if (state->getSymVal(I.getKey()))
658 state = state->set<RegionState>(I.getKey(),RefState::getAllocateFailed());
659 }
660
661 return state;
662}
Zhongxing Xuc8023782010-03-10 04:58:55 +0000663
664// Check if the location is a freed symbolic region.
Anna Zaks390909c2011-10-06 00:43:15 +0000665void MallocChecker::checkLocation(SVal l, bool isLoad, const Stmt *S,
666 CheckerContext &C) const {
Zhongxing Xuc8023782010-03-10 04:58:55 +0000667 SymbolRef Sym = l.getLocSymbolInBase();
668 if (Sym) {
669 const RefState *RS = C.getState()->get<RegionState>(Sym);
Ted Kremenekcea68652010-08-06 21:12:49 +0000670 if (RS && RS->isReleased()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000671 if (ExplodedNode *N = C.generateNode()) {
Zhongxing Xuc8023782010-03-10 04:58:55 +0000672 if (!BT_UseFree)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000673 BT_UseFree.reset(new BuiltinBug("Use dynamically allocated memory "
674 "after it is freed."));
Zhongxing Xuc8023782010-03-10 04:58:55 +0000675
676 BugReport *R = new BugReport(*BT_UseFree, BT_UseFree->getDescription(),
677 N);
678 C.EmitReport(R);
679 }
Ted Kremenekcea68652010-08-06 21:12:49 +0000680 }
Zhongxing Xuc8023782010-03-10 04:58:55 +0000681 }
682}
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000683
Anna Zaks390909c2011-10-06 00:43:15 +0000684void MallocChecker::checkBind(SVal location, SVal val,
685 const Stmt *BindS, CheckerContext &C) const {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000686 // The PreVisitBind implements the same algorithm as already used by the
687 // Objective C ownership checker: if the pointer escaped from this scope by
688 // assignment, let it go. However, assigning to fields of a stack-storage
689 // structure does not transfer ownership.
690
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000691 const ProgramState *state = C.getState();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000692 DefinedOrUnknownSVal l = cast<DefinedOrUnknownSVal>(location);
693
694 // Check for null dereferences.
695 if (!isa<Loc>(l))
696 return;
697
698 // Before checking if the state is null, check if 'val' has a RefState.
699 // Only then should we check for null and bifurcate the state.
700 SymbolRef Sym = val.getLocSymbolInBase();
701 if (Sym) {
702 if (const RefState *RS = state->get<RegionState>(Sym)) {
703 // If ptr is NULL, no operation is performed.
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000704 const ProgramState *notNullState, *nullState;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000705 llvm::tie(notNullState, nullState) = state->assume(l);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000706
707 // Generate a transition for 'nullState' to record the assumption
708 // that the state was null.
709 if (nullState)
Anna Zaks063e0882011-10-25 19:57:06 +0000710 C.generateNode(nullState);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000711
712 if (!notNullState)
713 return;
714
715 if (RS->isAllocated()) {
716 // Something we presently own is being assigned somewhere.
717 const MemRegion *AR = location.getAsRegion();
718 if (!AR)
719 return;
720 AR = AR->StripCasts()->getBaseRegion();
721 do {
722 // If it is on the stack, we still own it.
723 if (AR->hasStackNonParametersStorage())
724 break;
725
726 // If the state can't represent this binding, we still own it.
Ted Kremenekdde201b2010-08-06 21:12:55 +0000727 if (notNullState == (notNullState->bindLoc(cast<Loc>(location),
728 UnknownVal())))
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000729 break;
730
731 // We no longer own this pointer.
Ted Kremenekdde201b2010-08-06 21:12:55 +0000732 notNullState =
733 notNullState->set<RegionState>(Sym,
Anna Zaks390909c2011-10-06 00:43:15 +0000734 RefState::getRelinquished(BindS));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000735 }
736 while (false);
737 }
Anna Zaks063e0882011-10-25 19:57:06 +0000738 C.generateNode(notNullState);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000739 }
740 }
741}
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000742
743void ento::registerMallocChecker(CheckerManager &mgr) {
744 mgr.registerChecker<MallocChecker>();
745}