blob: 0fd81b4b77eb5e143fbe16ff9447c68a2c5f7f67 [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 Kremenek8bef8232012-01-26 21:29:00 +000089 ProgramStateRef evalAssume(ProgramStateRef 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 Kremenek8bef8232012-01-26 21:29:00 +0000100 static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000101 const Expr *SizeEx, SVal Init,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000102 ProgramStateRef state) {
Ted Kremenek5eca4822012-01-06 22:09:28 +0000103 return MallocMemAux(C, CE,
104 state->getSVal(SizeEx, C.getLocationContext()),
105 Init, state);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000106 }
Ted Kremenek8bef8232012-01-26 21:29:00 +0000107 static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000108 SVal SizeEx, SVal Init,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000109 ProgramStateRef state);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000110
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000111 void FreeMem(CheckerContext &C, const CallExpr *CE) const;
Jordy Rose2a479922010-08-12 08:54:03 +0000112 void FreeMemAttr(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000113 const OwnershipAttr* Att) const;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000114 ProgramStateRef FreeMemAux(CheckerContext &C, const CallExpr *CE,
115 ProgramStateRef state, unsigned Num,
Ted Kremeneke3659a72012-01-04 23:48:37 +0000116 bool Hold) const;
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000117
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000118 void ReallocMem(CheckerContext &C, const CallExpr *CE) const;
119 static void CallocMem(CheckerContext &C, const CallExpr *CE);
Jordy Rose43859f62010-06-07 19:32:37 +0000120
Ted Kremenek9c378f72011-08-12 23:37:29 +0000121 static bool SummarizeValue(raw_ostream &os, SVal V);
122 static bool SummarizeRegion(raw_ostream &os, const MemRegion *MR);
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000123 void ReportBadFree(CheckerContext &C, SVal ArgVal, SourceRange range) const;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000124};
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000125} // end anonymous namespace
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000126
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000127typedef llvm::ImmutableMap<SymbolRef, RefState> RegionStateTy;
128
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000129namespace clang {
Ted Kremenek9ef65372010-12-23 07:20:52 +0000130namespace ento {
Zhongxing Xu243fde92009-11-17 07:54:15 +0000131 template <>
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000132 struct ProgramStateTrait<RegionState>
133 : public ProgramStatePartialTrait<RegionStateTy> {
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000134 static void *GDMIndex() { static int x; return &x; }
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000135 };
136}
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000137}
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000138
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000139bool MallocChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
Anna Zaksb805c8f2011-12-01 05:57:37 +0000140 const FunctionDecl *FD = C.getCalleeDecl(CE);
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000141 if (!FD)
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000142 return false;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000143
144 ASTContext &Ctx = C.getASTContext();
145 if (!II_malloc)
146 II_malloc = &Ctx.Idents.get("malloc");
147 if (!II_free)
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000148 II_free = &Ctx.Idents.get("free");
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000149 if (!II_realloc)
150 II_realloc = &Ctx.Idents.get("realloc");
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000151 if (!II_calloc)
152 II_calloc = &Ctx.Idents.get("calloc");
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000153
154 if (FD->getIdentifier() == II_malloc) {
155 MallocMem(C, CE);
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000156 return true;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000157 }
158
159 if (FD->getIdentifier() == II_free) {
160 FreeMem(C, CE);
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000161 return true;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000162 }
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000163
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000164 if (FD->getIdentifier() == II_realloc) {
165 ReallocMem(C, CE);
166 return true;
167 }
168
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000169 if (FD->getIdentifier() == II_calloc) {
170 CallocMem(C, CE);
171 return true;
172 }
173
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000174 // Check all the attributes, if there are any.
175 // There can be multiple of these attributes.
176 bool rv = false;
177 if (FD->hasAttrs()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000178 for (specific_attr_iterator<OwnershipAttr>
179 i = FD->specific_attr_begin<OwnershipAttr>(),
180 e = FD->specific_attr_end<OwnershipAttr>();
181 i != e; ++i) {
182 switch ((*i)->getOwnKind()) {
183 case OwnershipAttr::Returns: {
184 MallocMemReturnsAttr(C, CE, *i);
Jordy Rose2a479922010-08-12 08:54:03 +0000185 rv = true;
186 break;
Sean Huntcf807c42010-08-18 23:23:40 +0000187 }
188 case OwnershipAttr::Takes:
189 case OwnershipAttr::Holds: {
190 FreeMemAttr(C, CE, *i);
Jordy Rose2a479922010-08-12 08:54:03 +0000191 rv = true;
192 break;
Sean Huntcf807c42010-08-18 23:23:40 +0000193 }
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 Kremenek8bef8232012-01-26 21:29:00 +0000201 ProgramStateRef 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 Kremenek8bef8232012-01-26 21:29:00 +0000213 ProgramStateRef 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 Kremenek8bef8232012-01-26 21:29:00 +0000218 ProgramStateRef 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 Kremenek8bef8232012-01-26 21:29:00 +0000223ProgramStateRef 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 Kremenek8bef8232012-01-26 21:29:00 +0000226 ProgramStateRef 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 Kremenek5eca4822012-01-06 22:09:28 +0000233 state = state->BindExpr(CE, C.getLocationContext(), 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 Kremenek8bef8232012-01-26 21:29:00 +0000256 ProgramStateRef 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 Kremenek8bef8232012-01-26 21:29:00 +0000269 ProgramStateRef state =
Ted Kremeneke3659a72012-01-04 23:48:37 +0000270 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 Kremenek8bef8232012-01-26 21:29:00 +0000277ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C,
Ted Kremeneke3659a72012-01-04 23:48:37 +0000278 const CallExpr *CE,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000279 ProgramStateRef state,
Ted Kremeneke3659a72012-01-04 23:48:37 +0000280 unsigned Num,
281 bool Hold) const {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000282 const Expr *ArgExpr = CE->getArg(Num);
Ted Kremenek5eca4822012-01-06 22:09:28 +0000283 SVal ArgVal = state->getSVal(ArgExpr, C.getLocationContext());
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 Kremenek8bef8232012-01-26 21:29:00 +0000293 ProgramStateRef 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 Kremenek8bef8232012-01-26 21:29:00 +0000502 ProgramStateRef state = C.getState();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000503 const Expr *arg0Expr = CE->getArg(0);
Ted Kremenek5eca4822012-01-06 22:09:28 +0000504 const LocationContext *LCtx = C.getLocationContext();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000505 DefinedOrUnknownSVal arg0Val
Ted Kremenek5eca4822012-01-06 22:09:28 +0000506 = cast<DefinedOrUnknownSVal>(state->getSVal(arg0Expr, LCtx));
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000507
Ted Kremenek846eabd2010-12-01 21:28:31 +0000508 SValBuilder &svalBuilder = C.getSValBuilder();
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000509
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000510 DefinedOrUnknownSVal PtrEQ =
511 svalBuilder.evalEQ(state, arg0Val, svalBuilder.makeNull());
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000512
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000513 // Get the size argument. If there is no size arg then give up.
514 const Expr *Arg1 = CE->getArg(1);
515 if (!Arg1)
516 return;
517
518 // Get the value of the size argument.
519 DefinedOrUnknownSVal Arg1Val =
Ted Kremenek5eca4822012-01-06 22:09:28 +0000520 cast<DefinedOrUnknownSVal>(state->getSVal(Arg1, LCtx));
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000521
522 // Compare the size argument to 0.
523 DefinedOrUnknownSVal SizeZero =
524 svalBuilder.evalEQ(state, Arg1Val,
525 svalBuilder.makeIntValWithPtrWidth(0, false));
526
527 // If the ptr is NULL and the size is not 0, the call is equivalent to
528 // malloc(size).
Ted Kremenek8bef8232012-01-26 21:29:00 +0000529 ProgramStateRef stateEqual = state->assume(PtrEQ, true);
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000530 if (stateEqual && state->assume(SizeZero, false)) {
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000531 // Hack: set the NULL symbolic region to released to suppress false warning.
532 // In the future we should add more states for allocated regions, e.g.,
533 // CheckedNull, CheckedNonNull.
534
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000535 SymbolRef Sym = arg0Val.getAsLocSymbol();
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000536 if (Sym)
537 stateEqual = stateEqual->set<RegionState>(Sym, RefState::getReleased(CE));
538
Ted Kremenek8bef8232012-01-26 21:29:00 +0000539 ProgramStateRef stateMalloc = MallocMemAux(C, CE, CE->getArg(1),
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000540 UndefinedVal(), stateEqual);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000541 C.addTransition(stateMalloc);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000542 }
543
Ted Kremenek8bef8232012-01-26 21:29:00 +0000544 if (ProgramStateRef stateNotEqual = state->assume(PtrEQ, false)) {
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000545 // If the size is 0, free the memory.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000546 if (ProgramStateRef stateSizeZero =
Ted Kremeneke3659a72012-01-04 23:48:37 +0000547 stateNotEqual->assume(SizeZero, true))
Ted Kremenek8bef8232012-01-26 21:29:00 +0000548 if (ProgramStateRef stateFree =
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000549 FreeMemAux(C, CE, stateSizeZero, 0, false)) {
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000550
Zhongxing Xud56763f2011-09-01 04:53:59 +0000551 // Bind the return value to NULL because it is now free.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000552 C.addTransition(stateFree->BindExpr(CE, LCtx,
553 svalBuilder.makeNull(), true));
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000554 }
Ted Kremenek8bef8232012-01-26 21:29:00 +0000555 if (ProgramStateRef stateSizeNotZero =
Ted Kremeneke3659a72012-01-04 23:48:37 +0000556 stateNotEqual->assume(SizeZero,false))
Ted Kremenek8bef8232012-01-26 21:29:00 +0000557 if (ProgramStateRef stateFree = FreeMemAux(C, CE, stateSizeNotZero,
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000558 0, false)) {
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000559 // FIXME: We should copy the content of the original buffer.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000560 ProgramStateRef stateRealloc = MallocMemAux(C, CE, CE->getArg(1),
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000561 UnknownVal(), stateFree);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000562 C.addTransition(stateRealloc);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000563 }
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000564 }
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000565}
Zhongxing Xu7b760962009-11-13 07:25:27 +0000566
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000567void MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE) {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000568 ProgramStateRef state = C.getState();
Ted Kremenek846eabd2010-12-01 21:28:31 +0000569 SValBuilder &svalBuilder = C.getSValBuilder();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000570 const LocationContext *LCtx = C.getLocationContext();
571 SVal count = state->getSVal(CE->getArg(0), LCtx);
572 SVal elementSize = state->getSVal(CE->getArg(1), LCtx);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000573 SVal TotalSize = svalBuilder.evalBinOp(state, BO_Mul, count, elementSize,
574 svalBuilder.getContext().getSizeType());
575 SVal zeroVal = svalBuilder.makeZeroVal(svalBuilder.getContext().CharTy);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000576
Anna Zaks0bd6b112011-10-26 21:06:34 +0000577 C.addTransition(MallocMemAux(C, CE, TotalSize, zeroVal, state));
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000578}
579
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000580void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper,
581 CheckerContext &C) const
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000582{
Zhongxing Xu173ff562010-08-15 08:19:57 +0000583 if (!SymReaper.hasDeadSymbols())
584 return;
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000585
Ted Kremenek8bef8232012-01-26 21:29:00 +0000586 ProgramStateRef state = C.getState();
Zhongxing Xu173ff562010-08-15 08:19:57 +0000587 RegionStateTy RS = state->get<RegionState>();
Jordy Rose90760142010-08-18 04:33:47 +0000588 RegionStateTy::Factory &F = state->get_context<RegionState>();
Zhongxing Xu173ff562010-08-15 08:19:57 +0000589
Ted Kremenek217470e2011-07-28 23:07:51 +0000590 bool generateReport = false;
591
Zhongxing Xu173ff562010-08-15 08:19:57 +0000592 for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
593 if (SymReaper.isDead(I->first)) {
Ted Kremenek217470e2011-07-28 23:07:51 +0000594 if (I->second.isAllocated())
595 generateReport = true;
Jordy Rose90760142010-08-18 04:33:47 +0000596
597 // Remove the dead symbol from the map.
Ted Kremenek3baf6722010-11-24 00:54:37 +0000598 RS = F.remove(RS, I->first);
Ted Kremenek217470e2011-07-28 23:07:51 +0000599
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000600 }
601 }
Ted Kremenek217470e2011-07-28 23:07:51 +0000602
Anna Zaks0bd6b112011-10-26 21:06:34 +0000603 ExplodedNode *N = C.addTransition(state->set<RegionState>(RS));
Ted Kremenek217470e2011-07-28 23:07:51 +0000604
605 // FIXME: This does not handle when we have multiple leaks at a single
606 // place.
607 if (N && generateReport) {
608 if (!BT_Leak)
609 BT_Leak.reset(new BuiltinBug("Memory leak",
610 "Allocated memory never released. Potential memory leak."));
611 // FIXME: where it is allocated.
612 BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
613 C.EmitReport(R);
614 }
Zhongxing Xu7b760962009-11-13 07:25:27 +0000615}
Zhongxing Xu243fde92009-11-17 07:54:15 +0000616
Anna Zaksaf498a22011-10-25 19:56:48 +0000617void MallocChecker::checkEndPath(CheckerContext &Ctx) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000618 ProgramStateRef state = Ctx.getState();
Jordy Rose09cef092010-08-18 04:26:59 +0000619 RegionStateTy M = state->get<RegionState>();
Zhongxing Xu243fde92009-11-17 07:54:15 +0000620
Jordy Rose09cef092010-08-18 04:26:59 +0000621 for (RegionStateTy::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Zhongxing Xu243fde92009-11-17 07:54:15 +0000622 RefState RS = I->second;
623 if (RS.isAllocated()) {
Anna Zaks0bd6b112011-10-26 21:06:34 +0000624 ExplodedNode *N = Ctx.addTransition(state);
Zhongxing Xu243fde92009-11-17 07:54:15 +0000625 if (N) {
626 if (!BT_Leak)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000627 BT_Leak.reset(new BuiltinBug("Memory leak",
628 "Allocated memory never released. Potential memory leak."));
Zhongxing Xu243fde92009-11-17 07:54:15 +0000629 BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
Anna Zaksaf498a22011-10-25 19:56:48 +0000630 Ctx.EmitReport(R);
Zhongxing Xu243fde92009-11-17 07:54:15 +0000631 }
632 }
633 }
634}
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000635
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000636void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000637 const Expr *retExpr = S->getRetValue();
638 if (!retExpr)
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000639 return;
640
Ted Kremenek8bef8232012-01-26 21:29:00 +0000641 ProgramStateRef state = C.getState();
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000642
Ted Kremenek5eca4822012-01-06 22:09:28 +0000643 SymbolRef Sym = state->getSVal(retExpr, C.getLocationContext()).getAsSymbol();
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000644 if (!Sym)
645 return;
646
647 const RefState *RS = state->get<RegionState>(Sym);
648 if (!RS)
649 return;
650
651 // FIXME: check other cases.
652 if (RS->isAllocated())
653 state = state->set<RegionState>(Sym, RefState::getEscaped(S));
654
Anna Zaks0bd6b112011-10-26 21:06:34 +0000655 C.addTransition(state);
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000656}
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000657
Ted Kremenek8bef8232012-01-26 21:29:00 +0000658ProgramStateRef MallocChecker::evalAssume(ProgramStateRef state,
Ted Kremeneke3659a72012-01-04 23:48:37 +0000659 SVal Cond,
660 bool Assumption) const {
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000661 // If a symblic region is assumed to NULL, set its state to AllocateFailed.
662 // FIXME: should also check symbols assumed to non-null.
663
664 RegionStateTy RS = state->get<RegionState>();
665
666 for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
Zhongxing Xu2bfa3012011-04-02 03:20:45 +0000667 // If the symbol is assumed to NULL, this will return an APSInt*.
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000668 if (state->getSymVal(I.getKey()))
669 state = state->set<RegionState>(I.getKey(),RefState::getAllocateFailed());
670 }
671
672 return state;
673}
Zhongxing Xuc8023782010-03-10 04:58:55 +0000674
675// Check if the location is a freed symbolic region.
Anna Zaks390909c2011-10-06 00:43:15 +0000676void MallocChecker::checkLocation(SVal l, bool isLoad, const Stmt *S,
677 CheckerContext &C) const {
Zhongxing Xuc8023782010-03-10 04:58:55 +0000678 SymbolRef Sym = l.getLocSymbolInBase();
679 if (Sym) {
680 const RefState *RS = C.getState()->get<RegionState>(Sym);
Ted Kremenekcea68652010-08-06 21:12:49 +0000681 if (RS && RS->isReleased()) {
Anna Zaks0bd6b112011-10-26 21:06:34 +0000682 if (ExplodedNode *N = C.addTransition()) {
Zhongxing Xuc8023782010-03-10 04:58:55 +0000683 if (!BT_UseFree)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000684 BT_UseFree.reset(new BuiltinBug("Use dynamically allocated memory "
685 "after it is freed."));
Zhongxing Xuc8023782010-03-10 04:58:55 +0000686
687 BugReport *R = new BugReport(*BT_UseFree, BT_UseFree->getDescription(),
688 N);
689 C.EmitReport(R);
690 }
Ted Kremenekcea68652010-08-06 21:12:49 +0000691 }
Zhongxing Xuc8023782010-03-10 04:58:55 +0000692 }
693}
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000694
Anna Zaks390909c2011-10-06 00:43:15 +0000695void MallocChecker::checkBind(SVal location, SVal val,
696 const Stmt *BindS, CheckerContext &C) const {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000697 // The PreVisitBind implements the same algorithm as already used by the
698 // Objective C ownership checker: if the pointer escaped from this scope by
699 // assignment, let it go. However, assigning to fields of a stack-storage
700 // structure does not transfer ownership.
701
Ted Kremenek8bef8232012-01-26 21:29:00 +0000702 ProgramStateRef state = C.getState();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000703 DefinedOrUnknownSVal l = cast<DefinedOrUnknownSVal>(location);
704
705 // Check for null dereferences.
706 if (!isa<Loc>(l))
707 return;
708
709 // Before checking if the state is null, check if 'val' has a RefState.
710 // Only then should we check for null and bifurcate the state.
711 SymbolRef Sym = val.getLocSymbolInBase();
712 if (Sym) {
713 if (const RefState *RS = state->get<RegionState>(Sym)) {
714 // If ptr is NULL, no operation is performed.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000715 ProgramStateRef notNullState, nullState;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000716 llvm::tie(notNullState, nullState) = state->assume(l);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000717
718 // Generate a transition for 'nullState' to record the assumption
719 // that the state was null.
720 if (nullState)
Anna Zaks0bd6b112011-10-26 21:06:34 +0000721 C.addTransition(nullState);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000722
723 if (!notNullState)
724 return;
725
726 if (RS->isAllocated()) {
727 // Something we presently own is being assigned somewhere.
728 const MemRegion *AR = location.getAsRegion();
729 if (!AR)
730 return;
731 AR = AR->StripCasts()->getBaseRegion();
732 do {
733 // If it is on the stack, we still own it.
734 if (AR->hasStackNonParametersStorage())
735 break;
736
737 // If the state can't represent this binding, we still own it.
Ted Kremenekdde201b2010-08-06 21:12:55 +0000738 if (notNullState == (notNullState->bindLoc(cast<Loc>(location),
739 UnknownVal())))
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000740 break;
741
742 // We no longer own this pointer.
Ted Kremenekdde201b2010-08-06 21:12:55 +0000743 notNullState =
744 notNullState->set<RegionState>(Sym,
Anna Zaks390909c2011-10-06 00:43:15 +0000745 RefState::getRelinquished(BindS));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000746 }
747 while (false);
748 }
Anna Zaks0bd6b112011-10-26 21:06:34 +0000749 C.addTransition(notNullState);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000750 }
751 }
752}
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000753
754void ento::registerMallocChecker(CheckerManager &mgr) {
755 mgr.registerChecker<MallocChecker>();
756}