blob: 4035c7a544e2c5e9423afd800c2d71efbdf6ad55 [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"
Benjamin Kramer00bd44d2012-02-04 12:31:12 +000024#include "llvm/ADT/STLExtras.h"
Zhongxing Xu589c0f22009-11-12 08:38:56 +000025using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000026using namespace ento;
Zhongxing Xu589c0f22009-11-12 08:38:56 +000027
28namespace {
29
Zhongxing Xu7fb14642009-12-11 00:55:44 +000030class RefState {
Ted Kremenekdde201b2010-08-06 21:12:55 +000031 enum Kind { AllocateUnchecked, AllocateFailed, Released, Escaped,
32 Relinquished } K;
Zhongxing Xu243fde92009-11-17 07:54:15 +000033 const Stmt *S;
34
Zhongxing Xu7fb14642009-12-11 00:55:44 +000035public:
Zhongxing Xu243fde92009-11-17 07:54:15 +000036 RefState(Kind k, const Stmt *s) : K(k), S(s) {}
37
Zhongxing Xub94b81a2009-12-31 06:13:07 +000038 bool isAllocated() const { return K == AllocateUnchecked; }
Chris Lattnerfae96222010-09-03 04:34:38 +000039 //bool isFailed() const { return K == AllocateFailed; }
Zhongxing Xu243fde92009-11-17 07:54:15 +000040 bool isReleased() const { return K == Released; }
Chris Lattnerfae96222010-09-03 04:34:38 +000041 //bool isEscaped() const { return K == Escaped; }
42 //bool isRelinquished() const { return K == Relinquished; }
Zhongxing Xu243fde92009-11-17 07:54:15 +000043
44 bool operator==(const RefState &X) const {
45 return K == X.K && S == X.S;
46 }
47
Zhongxing Xub94b81a2009-12-31 06:13:07 +000048 static RefState getAllocateUnchecked(const Stmt *s) {
49 return RefState(AllocateUnchecked, s);
50 }
51 static RefState getAllocateFailed() {
52 return RefState(AllocateFailed, 0);
53 }
Zhongxing Xu243fde92009-11-17 07:54:15 +000054 static RefState getReleased(const Stmt *s) { return RefState(Released, s); }
55 static RefState getEscaped(const Stmt *s) { return RefState(Escaped, s); }
Ted Kremenekdde201b2010-08-06 21:12:55 +000056 static RefState getRelinquished(const Stmt *s) {
57 return RefState(Relinquished, s);
58 }
Zhongxing Xu243fde92009-11-17 07:54:15 +000059
60 void Profile(llvm::FoldingSetNodeID &ID) const {
61 ID.AddInteger(K);
62 ID.AddPointer(S);
63 }
Zhongxing Xu589c0f22009-11-12 08:38:56 +000064};
65
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000066class RegionState {};
Zhongxing Xu589c0f22009-11-12 08:38:56 +000067
Ted Kremeneke3659a72012-01-04 23:48:37 +000068class MallocChecker : public Checker<eval::Call,
69 check::DeadSymbols,
70 check::EndPath,
71 check::PreStmt<ReturnStmt>,
72 check::Location,
73 check::Bind,
74 eval::Assume>
75{
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000076 mutable llvm::OwningPtr<BuiltinBug> BT_DoubleFree;
77 mutable llvm::OwningPtr<BuiltinBug> BT_Leak;
78 mutable llvm::OwningPtr<BuiltinBug> BT_UseFree;
79 mutable llvm::OwningPtr<BuiltinBug> BT_UseRelinquished;
80 mutable llvm::OwningPtr<BuiltinBug> BT_BadFree;
81 mutable IdentifierInfo *II_malloc, *II_free, *II_realloc, *II_calloc;
Zhongxing Xu589c0f22009-11-12 08:38:56 +000082
83public:
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000084 MallocChecker() : II_malloc(0), II_free(0), II_realloc(0), II_calloc(0) {}
85
86 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
87 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
Anna Zaksaf498a22011-10-25 19:56:48 +000088 void checkEndPath(CheckerContext &C) const;
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000089 void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
Ted Kremenek8bef8232012-01-26 21:29:00 +000090 ProgramStateRef evalAssume(ProgramStateRef state, SVal Cond,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000091 bool Assumption) const;
Anna Zaks390909c2011-10-06 00:43:15 +000092 void checkLocation(SVal l, bool isLoad, const Stmt *S,
93 CheckerContext &C) const;
94 void checkBind(SVal location, SVal val, const Stmt*S,
95 CheckerContext &C) const;
Zhongxing Xub94b81a2009-12-31 06:13:07 +000096
Zhongxing Xu7b760962009-11-13 07:25:27 +000097private:
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000098 static void MallocMem(CheckerContext &C, const CallExpr *CE);
99 static void MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,
100 const OwnershipAttr* Att);
Ted Kremenek8bef8232012-01-26 21:29:00 +0000101 static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000102 const Expr *SizeEx, SVal Init,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000103 ProgramStateRef state) {
Ted Kremenek5eca4822012-01-06 22:09:28 +0000104 return MallocMemAux(C, CE,
105 state->getSVal(SizeEx, C.getLocationContext()),
106 Init, state);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000107 }
Ted Kremenek8bef8232012-01-26 21:29:00 +0000108 static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000109 SVal SizeEx, SVal Init,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000110 ProgramStateRef state);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000111
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000112 void FreeMem(CheckerContext &C, const CallExpr *CE) const;
Jordy Rose2a479922010-08-12 08:54:03 +0000113 void FreeMemAttr(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000114 const OwnershipAttr* Att) const;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000115 ProgramStateRef FreeMemAux(CheckerContext &C, const CallExpr *CE,
116 ProgramStateRef state, unsigned Num,
Ted Kremeneke3659a72012-01-04 23:48:37 +0000117 bool Hold) const;
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000118
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000119 void ReallocMem(CheckerContext &C, const CallExpr *CE) const;
120 static void CallocMem(CheckerContext &C, const CallExpr *CE);
Jordy Rose43859f62010-06-07 19:32:37 +0000121
Ted Kremenek9c378f72011-08-12 23:37:29 +0000122 static bool SummarizeValue(raw_ostream &os, SVal V);
123 static bool SummarizeRegion(raw_ostream &os, const MemRegion *MR);
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000124 void ReportBadFree(CheckerContext &C, SVal ArgVal, SourceRange range) const;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000125};
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000126} // end anonymous namespace
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000127
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000128typedef llvm::ImmutableMap<SymbolRef, RefState> RegionStateTy;
129
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000130namespace clang {
Ted Kremenek9ef65372010-12-23 07:20:52 +0000131namespace ento {
Zhongxing Xu243fde92009-11-17 07:54:15 +0000132 template <>
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000133 struct ProgramStateTrait<RegionState>
134 : public ProgramStatePartialTrait<RegionStateTy> {
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000135 static void *GDMIndex() { static int x; return &x; }
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000136 };
137}
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000138}
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000139
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000140bool MallocChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
Anna Zaksb805c8f2011-12-01 05:57:37 +0000141 const FunctionDecl *FD = C.getCalleeDecl(CE);
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000142 if (!FD)
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000143 return false;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000144
145 ASTContext &Ctx = C.getASTContext();
146 if (!II_malloc)
147 II_malloc = &Ctx.Idents.get("malloc");
148 if (!II_free)
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000149 II_free = &Ctx.Idents.get("free");
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000150 if (!II_realloc)
151 II_realloc = &Ctx.Idents.get("realloc");
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000152 if (!II_calloc)
153 II_calloc = &Ctx.Idents.get("calloc");
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000154
155 if (FD->getIdentifier() == II_malloc) {
156 MallocMem(C, CE);
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000157 return true;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000158 }
159
160 if (FD->getIdentifier() == II_free) {
161 FreeMem(C, CE);
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000162 return true;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000163 }
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000164
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000165 if (FD->getIdentifier() == II_realloc) {
166 ReallocMem(C, CE);
167 return true;
168 }
169
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000170 if (FD->getIdentifier() == II_calloc) {
171 CallocMem(C, CE);
172 return true;
173 }
174
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000175 // Check all the attributes, if there are any.
176 // There can be multiple of these attributes.
177 bool rv = false;
178 if (FD->hasAttrs()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000179 for (specific_attr_iterator<OwnershipAttr>
180 i = FD->specific_attr_begin<OwnershipAttr>(),
181 e = FD->specific_attr_end<OwnershipAttr>();
182 i != e; ++i) {
183 switch ((*i)->getOwnKind()) {
184 case OwnershipAttr::Returns: {
185 MallocMemReturnsAttr(C, CE, *i);
Jordy Rose2a479922010-08-12 08:54:03 +0000186 rv = true;
187 break;
Sean Huntcf807c42010-08-18 23:23:40 +0000188 }
189 case OwnershipAttr::Takes:
190 case OwnershipAttr::Holds: {
191 FreeMemAttr(C, CE, *i);
Jordy Rose2a479922010-08-12 08:54:03 +0000192 rv = true;
193 break;
Sean Huntcf807c42010-08-18 23:23:40 +0000194 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000195 }
196 }
197 }
198 return rv;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000199}
200
201void MallocChecker::MallocMem(CheckerContext &C, const CallExpr *CE) {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000202 ProgramStateRef state = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(),
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000203 C.getState());
Anna Zaks0bd6b112011-10-26 21:06:34 +0000204 C.addTransition(state);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000205}
206
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000207void MallocChecker::MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,
208 const OwnershipAttr* Att) {
Sean Huntcf807c42010-08-18 23:23:40 +0000209 if (Att->getModule() != "malloc")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000210 return;
211
Sean Huntcf807c42010-08-18 23:23:40 +0000212 OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000213 if (I != E) {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000214 ProgramStateRef state =
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000215 MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), C.getState());
Anna Zaks0bd6b112011-10-26 21:06:34 +0000216 C.addTransition(state);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000217 return;
218 }
Ted Kremenek8bef8232012-01-26 21:29:00 +0000219 ProgramStateRef state = MallocMemAux(C, CE, UnknownVal(), UndefinedVal(),
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000220 C.getState());
Anna Zaks0bd6b112011-10-26 21:06:34 +0000221 C.addTransition(state);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000222}
223
Ted Kremenek8bef8232012-01-26 21:29:00 +0000224ProgramStateRef MallocChecker::MallocMemAux(CheckerContext &C,
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000225 const CallExpr *CE,
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000226 SVal Size, SVal Init,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000227 ProgramStateRef state) {
Anna Zaks5d0ea6d2011-10-04 20:43:05 +0000228 unsigned Count = C.getCurrentBlockCount();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000229 SValBuilder &svalBuilder = C.getSValBuilder();
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000230
Jordy Rose32f26562010-07-04 00:00:41 +0000231 // Set the return value.
Ted Kremeneke3659a72012-01-04 23:48:37 +0000232 SVal retVal = svalBuilder.getConjuredSymbolVal(NULL, CE,
233 CE->getType(), Count);
Ted Kremenek5eca4822012-01-06 22:09:28 +0000234 state = state->BindExpr(CE, C.getLocationContext(), retVal);
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000235
Jordy Rose32f26562010-07-04 00:00:41 +0000236 // Fill the region with the initialization value.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000237 state = state->bindDefault(retVal, Init);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000238
Jordy Rose32f26562010-07-04 00:00:41 +0000239 // Set the region's extent equal to the Size parameter.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000240 const SymbolicRegion *R = cast<SymbolicRegion>(retVal.getAsRegion());
241 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
Jordy Rose32f26562010-07-04 00:00:41 +0000242 DefinedOrUnknownSVal DefinedSize = cast<DefinedOrUnknownSVal>(Size);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000243 DefinedOrUnknownSVal extentMatchesSize =
Ted Kremenek9c149532010-12-01 21:57:22 +0000244 svalBuilder.evalEQ(state, Extent, DefinedSize);
Jordy Rose32f26562010-07-04 00:00:41 +0000245
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000246 state = state->assume(extentMatchesSize, true);
247 assert(state);
248
249 SymbolRef Sym = retVal.getAsLocSymbol();
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000250 assert(Sym);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000251
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000252 // Set the symbol's state to Allocated.
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000253 return state->set<RegionState>(Sym, RefState::getAllocateUnchecked(CE));
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000254}
255
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000256void MallocChecker::FreeMem(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000257 ProgramStateRef state = FreeMemAux(C, CE, C.getState(), 0, false);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000258
259 if (state)
Anna Zaks0bd6b112011-10-26 21:06:34 +0000260 C.addTransition(state);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000261}
262
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000263void MallocChecker::FreeMemAttr(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000264 const OwnershipAttr* Att) const {
Sean Huntcf807c42010-08-18 23:23:40 +0000265 if (Att->getModule() != "malloc")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000266 return;
267
Sean Huntcf807c42010-08-18 23:23:40 +0000268 for (OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
269 I != E; ++I) {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000270 ProgramStateRef state =
Ted Kremeneke3659a72012-01-04 23:48:37 +0000271 FreeMemAux(C, CE, C.getState(), *I,
272 Att->getOwnKind() == OwnershipAttr::Holds);
Sean Huntcf807c42010-08-18 23:23:40 +0000273 if (state)
Anna Zaks0bd6b112011-10-26 21:06:34 +0000274 C.addTransition(state);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000275 }
276}
277
Ted Kremenek8bef8232012-01-26 21:29:00 +0000278ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C,
Ted Kremeneke3659a72012-01-04 23:48:37 +0000279 const CallExpr *CE,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000280 ProgramStateRef state,
Ted Kremeneke3659a72012-01-04 23:48:37 +0000281 unsigned Num,
282 bool Hold) const {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000283 const Expr *ArgExpr = CE->getArg(Num);
Ted Kremenek5eca4822012-01-06 22:09:28 +0000284 SVal ArgVal = state->getSVal(ArgExpr, C.getLocationContext());
Zhongxing Xu181cc3d2010-02-14 06:49:48 +0000285
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000286 DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(ArgVal);
287
288 // Check for null dereferences.
289 if (!isa<Loc>(location))
Zhongxing Xu181cc3d2010-02-14 06:49:48 +0000290 return state;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000291
292 // FIXME: Technically using 'Assume' here can result in a path
293 // bifurcation. In such cases we need to return two states, not just one.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000294 ProgramStateRef notNullState, nullState;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000295 llvm::tie(notNullState, nullState) = state->assume(location);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000296
297 // The explicit NULL case, no operation is performed.
298 if (nullState && !notNullState)
299 return nullState;
300
301 assert(notNullState);
302
Jordy Rose43859f62010-06-07 19:32:37 +0000303 // Unknown values could easily be okay
304 // Undefined values are handled elsewhere
305 if (ArgVal.isUnknownOrUndef())
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000306 return notNullState;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000307
Jordy Rose43859f62010-06-07 19:32:37 +0000308 const MemRegion *R = ArgVal.getAsRegion();
309
310 // Nonlocs can't be freed, of course.
311 // Non-region locations (labels and fixed addresses) also shouldn't be freed.
312 if (!R) {
313 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
314 return NULL;
315 }
316
317 R = R->StripCasts();
318
319 // Blocks might show up as heap data, but should not be free()d
320 if (isa<BlockDataRegion>(R)) {
321 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
322 return NULL;
323 }
324
325 const MemSpaceRegion *MS = R->getMemorySpace();
326
327 // Parameters, locals, statics, and globals shouldn't be freed.
328 if (!(isa<UnknownSpaceRegion>(MS) || isa<HeapSpaceRegion>(MS))) {
329 // FIXME: at the time this code was written, malloc() regions were
330 // represented by conjured symbols, which are all in UnknownSpaceRegion.
331 // This means that there isn't actually anything from HeapSpaceRegion
332 // that should be freed, even though we allow it here.
333 // Of course, free() can work on memory allocated outside the current
334 // function, so UnknownSpaceRegion is always a possibility.
335 // False negatives are better than false positives.
336
337 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
338 return NULL;
339 }
340
341 const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R);
342 // Various cases could lead to non-symbol values here.
343 // For now, ignore them.
344 if (!SR)
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000345 return notNullState;
Jordy Rose43859f62010-06-07 19:32:37 +0000346
347 SymbolRef Sym = SR->getSymbol();
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000348 const RefState *RS = state->get<RegionState>(Sym);
Zhongxing Xu7e3cda92010-01-18 03:27:34 +0000349
350 // If the symbol has not been tracked, return. This is possible when free() is
351 // called on a pointer that does not get its pointee directly from malloc().
352 // Full support of this requires inter-procedural analysis.
353 if (!RS)
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000354 return notNullState;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000355
356 // Check double free.
Zhongxing Xu243fde92009-11-17 07:54:15 +0000357 if (RS->isReleased()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000358 if (ExplodedNode *N = C.generateSink()) {
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000359 if (!BT_DoubleFree)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000360 BT_DoubleFree.reset(
361 new BuiltinBug("Double free",
362 "Try to free a memory block that has been released"));
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000363 // FIXME: should find where it's freed last time.
364 BugReport *R = new BugReport(*BT_DoubleFree,
Benjamin Kramerd02e2322009-11-14 12:08:24 +0000365 BT_DoubleFree->getDescription(), N);
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000366 C.EmitReport(R);
367 }
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000368 return NULL;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000369 }
370
371 // Normal free.
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000372 if (Hold)
373 return notNullState->set<RegionState>(Sym, RefState::getRelinquished(CE));
374 return notNullState->set<RegionState>(Sym, RefState::getReleased(CE));
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000375}
376
Ted Kremenek9c378f72011-08-12 23:37:29 +0000377bool MallocChecker::SummarizeValue(raw_ostream &os, SVal V) {
Jordy Rose43859f62010-06-07 19:32:37 +0000378 if (nonloc::ConcreteInt *IntVal = dyn_cast<nonloc::ConcreteInt>(&V))
379 os << "an integer (" << IntVal->getValue() << ")";
380 else if (loc::ConcreteInt *ConstAddr = dyn_cast<loc::ConcreteInt>(&V))
381 os << "a constant address (" << ConstAddr->getValue() << ")";
382 else if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&V))
Chris Lattner68106302011-02-17 05:38:27 +0000383 os << "the address of the label '" << Label->getLabel()->getName() << "'";
Jordy Rose43859f62010-06-07 19:32:37 +0000384 else
385 return false;
386
387 return true;
388}
389
Ted Kremenek9c378f72011-08-12 23:37:29 +0000390bool MallocChecker::SummarizeRegion(raw_ostream &os,
Jordy Rose43859f62010-06-07 19:32:37 +0000391 const MemRegion *MR) {
392 switch (MR->getKind()) {
393 case MemRegion::FunctionTextRegionKind: {
394 const FunctionDecl *FD = cast<FunctionTextRegion>(MR)->getDecl();
395 if (FD)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000396 os << "the address of the function '" << *FD << '\'';
Jordy Rose43859f62010-06-07 19:32:37 +0000397 else
398 os << "the address of a function";
399 return true;
400 }
401 case MemRegion::BlockTextRegionKind:
402 os << "block text";
403 return true;
404 case MemRegion::BlockDataRegionKind:
405 // FIXME: where the block came from?
406 os << "a block";
407 return true;
408 default: {
409 const MemSpaceRegion *MS = MR->getMemorySpace();
410
Anna Zakseb31a762012-01-04 23:54:01 +0000411 if (isa<StackLocalsSpaceRegion>(MS)) {
Jordy Rose43859f62010-06-07 19:32:37 +0000412 const VarRegion *VR = dyn_cast<VarRegion>(MR);
413 const VarDecl *VD;
414 if (VR)
415 VD = VR->getDecl();
416 else
417 VD = NULL;
418
419 if (VD)
420 os << "the address of the local variable '" << VD->getName() << "'";
421 else
422 os << "the address of a local stack variable";
423 return true;
424 }
Anna Zakseb31a762012-01-04 23:54:01 +0000425
426 if (isa<StackArgumentsSpaceRegion>(MS)) {
Jordy Rose43859f62010-06-07 19:32:37 +0000427 const VarRegion *VR = dyn_cast<VarRegion>(MR);
428 const VarDecl *VD;
429 if (VR)
430 VD = VR->getDecl();
431 else
432 VD = NULL;
433
434 if (VD)
435 os << "the address of the parameter '" << VD->getName() << "'";
436 else
437 os << "the address of a parameter";
438 return true;
439 }
Anna Zakseb31a762012-01-04 23:54:01 +0000440
441 if (isa<GlobalsSpaceRegion>(MS)) {
Jordy Rose43859f62010-06-07 19:32:37 +0000442 const VarRegion *VR = dyn_cast<VarRegion>(MR);
443 const VarDecl *VD;
444 if (VR)
445 VD = VR->getDecl();
446 else
447 VD = NULL;
448
449 if (VD) {
450 if (VD->isStaticLocal())
451 os << "the address of the static variable '" << VD->getName() << "'";
452 else
453 os << "the address of the global variable '" << VD->getName() << "'";
454 } else
455 os << "the address of a global variable";
456 return true;
457 }
Anna Zakseb31a762012-01-04 23:54:01 +0000458
459 return false;
Jordy Rose43859f62010-06-07 19:32:37 +0000460 }
461 }
462}
463
464void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000465 SourceRange range) const {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000466 if (ExplodedNode *N = C.generateSink()) {
Jordy Rose43859f62010-06-07 19:32:37 +0000467 if (!BT_BadFree)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000468 BT_BadFree.reset(new BuiltinBug("Bad free"));
Jordy Rose43859f62010-06-07 19:32:37 +0000469
470 llvm::SmallString<100> buf;
471 llvm::raw_svector_ostream os(buf);
472
473 const MemRegion *MR = ArgVal.getAsRegion();
474 if (MR) {
475 while (const ElementRegion *ER = dyn_cast<ElementRegion>(MR))
476 MR = ER->getSuperRegion();
477
478 // Special case for alloca()
479 if (isa<AllocaRegion>(MR))
480 os << "Argument to free() was allocated by alloca(), not malloc()";
481 else {
482 os << "Argument to free() is ";
483 if (SummarizeRegion(os, MR))
484 os << ", which is not memory allocated by malloc()";
485 else
486 os << "not memory allocated by malloc()";
487 }
488 } else {
489 os << "Argument to free() is ";
490 if (SummarizeValue(os, ArgVal))
491 os << ", which is not memory allocated by malloc()";
492 else
493 os << "not memory allocated by malloc()";
494 }
495
Anna Zakse172e8b2011-08-17 23:00:25 +0000496 BugReport *R = new BugReport(*BT_BadFree, os.str(), N);
Jordy Rose43859f62010-06-07 19:32:37 +0000497 R->addRange(range);
498 C.EmitReport(R);
499 }
500}
501
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000502void MallocChecker::ReallocMem(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000503 ProgramStateRef state = C.getState();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000504 const Expr *arg0Expr = CE->getArg(0);
Ted Kremenek5eca4822012-01-06 22:09:28 +0000505 const LocationContext *LCtx = C.getLocationContext();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000506 DefinedOrUnknownSVal arg0Val
Ted Kremenek5eca4822012-01-06 22:09:28 +0000507 = cast<DefinedOrUnknownSVal>(state->getSVal(arg0Expr, LCtx));
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000508
Ted Kremenek846eabd2010-12-01 21:28:31 +0000509 SValBuilder &svalBuilder = C.getSValBuilder();
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000510
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000511 DefinedOrUnknownSVal PtrEQ =
512 svalBuilder.evalEQ(state, arg0Val, svalBuilder.makeNull());
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000513
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000514 // Get the size argument. If there is no size arg then give up.
515 const Expr *Arg1 = CE->getArg(1);
516 if (!Arg1)
517 return;
518
519 // Get the value of the size argument.
520 DefinedOrUnknownSVal Arg1Val =
Ted Kremenek5eca4822012-01-06 22:09:28 +0000521 cast<DefinedOrUnknownSVal>(state->getSVal(Arg1, LCtx));
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000522
523 // Compare the size argument to 0.
524 DefinedOrUnknownSVal SizeZero =
525 svalBuilder.evalEQ(state, Arg1Val,
526 svalBuilder.makeIntValWithPtrWidth(0, false));
527
528 // If the ptr is NULL and the size is not 0, the call is equivalent to
529 // malloc(size).
Ted Kremenek8bef8232012-01-26 21:29:00 +0000530 ProgramStateRef stateEqual = state->assume(PtrEQ, true);
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000531 if (stateEqual && state->assume(SizeZero, false)) {
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000532 // Hack: set the NULL symbolic region to released to suppress false warning.
533 // In the future we should add more states for allocated regions, e.g.,
534 // CheckedNull, CheckedNonNull.
535
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000536 SymbolRef Sym = arg0Val.getAsLocSymbol();
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000537 if (Sym)
538 stateEqual = stateEqual->set<RegionState>(Sym, RefState::getReleased(CE));
539
Ted Kremenek8bef8232012-01-26 21:29:00 +0000540 ProgramStateRef stateMalloc = MallocMemAux(C, CE, CE->getArg(1),
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000541 UndefinedVal(), stateEqual);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000542 C.addTransition(stateMalloc);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000543 }
544
Ted Kremenek8bef8232012-01-26 21:29:00 +0000545 if (ProgramStateRef stateNotEqual = state->assume(PtrEQ, false)) {
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000546 // If the size is 0, free the memory.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000547 if (ProgramStateRef stateSizeZero =
Ted Kremeneke3659a72012-01-04 23:48:37 +0000548 stateNotEqual->assume(SizeZero, true))
Ted Kremenek8bef8232012-01-26 21:29:00 +0000549 if (ProgramStateRef stateFree =
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000550 FreeMemAux(C, CE, stateSizeZero, 0, false)) {
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000551
Zhongxing Xud56763f2011-09-01 04:53:59 +0000552 // Bind the return value to NULL because it is now free.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000553 C.addTransition(stateFree->BindExpr(CE, LCtx,
554 svalBuilder.makeNull(), true));
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000555 }
Ted Kremenek8bef8232012-01-26 21:29:00 +0000556 if (ProgramStateRef stateSizeNotZero =
Ted Kremeneke3659a72012-01-04 23:48:37 +0000557 stateNotEqual->assume(SizeZero,false))
Ted Kremenek8bef8232012-01-26 21:29:00 +0000558 if (ProgramStateRef stateFree = FreeMemAux(C, CE, stateSizeNotZero,
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000559 0, false)) {
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000560 // FIXME: We should copy the content of the original buffer.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000561 ProgramStateRef stateRealloc = MallocMemAux(C, CE, CE->getArg(1),
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000562 UnknownVal(), stateFree);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000563 C.addTransition(stateRealloc);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000564 }
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000565 }
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000566}
Zhongxing Xu7b760962009-11-13 07:25:27 +0000567
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000568void MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE) {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000569 ProgramStateRef state = C.getState();
Ted Kremenek846eabd2010-12-01 21:28:31 +0000570 SValBuilder &svalBuilder = C.getSValBuilder();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000571 const LocationContext *LCtx = C.getLocationContext();
572 SVal count = state->getSVal(CE->getArg(0), LCtx);
573 SVal elementSize = state->getSVal(CE->getArg(1), LCtx);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000574 SVal TotalSize = svalBuilder.evalBinOp(state, BO_Mul, count, elementSize,
575 svalBuilder.getContext().getSizeType());
576 SVal zeroVal = svalBuilder.makeZeroVal(svalBuilder.getContext().CharTy);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000577
Anna Zaks0bd6b112011-10-26 21:06:34 +0000578 C.addTransition(MallocMemAux(C, CE, TotalSize, zeroVal, state));
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000579}
580
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000581void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper,
582 CheckerContext &C) const
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000583{
Zhongxing Xu173ff562010-08-15 08:19:57 +0000584 if (!SymReaper.hasDeadSymbols())
585 return;
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000586
Ted Kremenek8bef8232012-01-26 21:29:00 +0000587 ProgramStateRef state = C.getState();
Zhongxing Xu173ff562010-08-15 08:19:57 +0000588 RegionStateTy RS = state->get<RegionState>();
Jordy Rose90760142010-08-18 04:33:47 +0000589 RegionStateTy::Factory &F = state->get_context<RegionState>();
Zhongxing Xu173ff562010-08-15 08:19:57 +0000590
Ted Kremenek217470e2011-07-28 23:07:51 +0000591 bool generateReport = false;
592
Zhongxing Xu173ff562010-08-15 08:19:57 +0000593 for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
594 if (SymReaper.isDead(I->first)) {
Ted Kremenek217470e2011-07-28 23:07:51 +0000595 if (I->second.isAllocated())
596 generateReport = true;
Jordy Rose90760142010-08-18 04:33:47 +0000597
598 // Remove the dead symbol from the map.
Ted Kremenek3baf6722010-11-24 00:54:37 +0000599 RS = F.remove(RS, I->first);
Ted Kremenek217470e2011-07-28 23:07:51 +0000600
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000601 }
602 }
Ted Kremenek217470e2011-07-28 23:07:51 +0000603
Anna Zaks0bd6b112011-10-26 21:06:34 +0000604 ExplodedNode *N = C.addTransition(state->set<RegionState>(RS));
Ted Kremenek217470e2011-07-28 23:07:51 +0000605
606 // FIXME: This does not handle when we have multiple leaks at a single
607 // place.
608 if (N && generateReport) {
609 if (!BT_Leak)
610 BT_Leak.reset(new BuiltinBug("Memory leak",
611 "Allocated memory never released. Potential memory leak."));
612 // FIXME: where it is allocated.
613 BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
614 C.EmitReport(R);
615 }
Zhongxing Xu7b760962009-11-13 07:25:27 +0000616}
Zhongxing Xu243fde92009-11-17 07:54:15 +0000617
Anna Zaksaf498a22011-10-25 19:56:48 +0000618void MallocChecker::checkEndPath(CheckerContext &Ctx) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000619 ProgramStateRef state = Ctx.getState();
Jordy Rose09cef092010-08-18 04:26:59 +0000620 RegionStateTy M = state->get<RegionState>();
Zhongxing Xu243fde92009-11-17 07:54:15 +0000621
Jordy Rose09cef092010-08-18 04:26:59 +0000622 for (RegionStateTy::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Zhongxing Xu243fde92009-11-17 07:54:15 +0000623 RefState RS = I->second;
624 if (RS.isAllocated()) {
Anna Zaks0bd6b112011-10-26 21:06:34 +0000625 ExplodedNode *N = Ctx.addTransition(state);
Zhongxing Xu243fde92009-11-17 07:54:15 +0000626 if (N) {
627 if (!BT_Leak)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000628 BT_Leak.reset(new BuiltinBug("Memory leak",
629 "Allocated memory never released. Potential memory leak."));
Zhongxing Xu243fde92009-11-17 07:54:15 +0000630 BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
Anna Zaksaf498a22011-10-25 19:56:48 +0000631 Ctx.EmitReport(R);
Zhongxing Xu243fde92009-11-17 07:54:15 +0000632 }
633 }
634 }
635}
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000636
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000637void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000638 const Expr *retExpr = S->getRetValue();
639 if (!retExpr)
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000640 return;
641
Ted Kremenek8bef8232012-01-26 21:29:00 +0000642 ProgramStateRef state = C.getState();
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000643
Ted Kremenek5eca4822012-01-06 22:09:28 +0000644 SymbolRef Sym = state->getSVal(retExpr, C.getLocationContext()).getAsSymbol();
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000645 if (!Sym)
646 return;
647
648 const RefState *RS = state->get<RegionState>(Sym);
649 if (!RS)
650 return;
651
652 // FIXME: check other cases.
653 if (RS->isAllocated())
654 state = state->set<RegionState>(Sym, RefState::getEscaped(S));
655
Anna Zaks0bd6b112011-10-26 21:06:34 +0000656 C.addTransition(state);
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000657}
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000658
Ted Kremenek8bef8232012-01-26 21:29:00 +0000659ProgramStateRef MallocChecker::evalAssume(ProgramStateRef state,
Ted Kremeneke3659a72012-01-04 23:48:37 +0000660 SVal Cond,
661 bool Assumption) const {
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000662 // If a symblic region is assumed to NULL, set its state to AllocateFailed.
663 // FIXME: should also check symbols assumed to non-null.
664
665 RegionStateTy RS = state->get<RegionState>();
666
667 for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
Zhongxing Xu2bfa3012011-04-02 03:20:45 +0000668 // If the symbol is assumed to NULL, this will return an APSInt*.
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000669 if (state->getSymVal(I.getKey()))
670 state = state->set<RegionState>(I.getKey(),RefState::getAllocateFailed());
671 }
672
673 return state;
674}
Zhongxing Xuc8023782010-03-10 04:58:55 +0000675
676// Check if the location is a freed symbolic region.
Anna Zaks390909c2011-10-06 00:43:15 +0000677void MallocChecker::checkLocation(SVal l, bool isLoad, const Stmt *S,
678 CheckerContext &C) const {
Zhongxing Xuc8023782010-03-10 04:58:55 +0000679 SymbolRef Sym = l.getLocSymbolInBase();
680 if (Sym) {
681 const RefState *RS = C.getState()->get<RegionState>(Sym);
Ted Kremenekcea68652010-08-06 21:12:49 +0000682 if (RS && RS->isReleased()) {
Anna Zaks0bd6b112011-10-26 21:06:34 +0000683 if (ExplodedNode *N = C.addTransition()) {
Zhongxing Xuc8023782010-03-10 04:58:55 +0000684 if (!BT_UseFree)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000685 BT_UseFree.reset(new BuiltinBug("Use dynamically allocated memory "
686 "after it is freed."));
Zhongxing Xuc8023782010-03-10 04:58:55 +0000687
688 BugReport *R = new BugReport(*BT_UseFree, BT_UseFree->getDescription(),
689 N);
690 C.EmitReport(R);
691 }
Ted Kremenekcea68652010-08-06 21:12:49 +0000692 }
Zhongxing Xuc8023782010-03-10 04:58:55 +0000693 }
694}
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000695
Anna Zaks390909c2011-10-06 00:43:15 +0000696void MallocChecker::checkBind(SVal location, SVal val,
697 const Stmt *BindS, CheckerContext &C) const {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000698 // The PreVisitBind implements the same algorithm as already used by the
699 // Objective C ownership checker: if the pointer escaped from this scope by
700 // assignment, let it go. However, assigning to fields of a stack-storage
701 // structure does not transfer ownership.
702
Ted Kremenek8bef8232012-01-26 21:29:00 +0000703 ProgramStateRef state = C.getState();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000704 DefinedOrUnknownSVal l = cast<DefinedOrUnknownSVal>(location);
705
706 // Check for null dereferences.
707 if (!isa<Loc>(l))
708 return;
709
710 // Before checking if the state is null, check if 'val' has a RefState.
711 // Only then should we check for null and bifurcate the state.
712 SymbolRef Sym = val.getLocSymbolInBase();
713 if (Sym) {
714 if (const RefState *RS = state->get<RegionState>(Sym)) {
715 // If ptr is NULL, no operation is performed.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000716 ProgramStateRef notNullState, nullState;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000717 llvm::tie(notNullState, nullState) = state->assume(l);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000718
719 // Generate a transition for 'nullState' to record the assumption
720 // that the state was null.
721 if (nullState)
Anna Zaks0bd6b112011-10-26 21:06:34 +0000722 C.addTransition(nullState);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000723
724 if (!notNullState)
725 return;
726
727 if (RS->isAllocated()) {
728 // Something we presently own is being assigned somewhere.
729 const MemRegion *AR = location.getAsRegion();
730 if (!AR)
731 return;
732 AR = AR->StripCasts()->getBaseRegion();
733 do {
734 // If it is on the stack, we still own it.
735 if (AR->hasStackNonParametersStorage())
736 break;
737
738 // If the state can't represent this binding, we still own it.
Ted Kremenekdde201b2010-08-06 21:12:55 +0000739 if (notNullState == (notNullState->bindLoc(cast<Loc>(location),
740 UnknownVal())))
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000741 break;
742
743 // We no longer own this pointer.
Ted Kremenekdde201b2010-08-06 21:12:55 +0000744 notNullState =
745 notNullState->set<RegionState>(Sym,
Anna Zaks390909c2011-10-06 00:43:15 +0000746 RefState::getRelinquished(BindS));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000747 }
748 while (false);
749 }
Anna Zaks0bd6b112011-10-26 21:06:34 +0000750 C.addTransition(notNullState);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000751 }
752 }
753}
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000754
755void ento::registerMallocChecker(CheckerManager &mgr) {
756 mgr.registerChecker<MallocChecker>();
757}