blob: 87ccc54c646a100c5a2830a3585c00774b4f28d5 [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 Kramer8fe83e12012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Benjamin Kramer00bd44d2012-02-04 12:31:12 +000025#include "llvm/ADT/STLExtras.h"
Zhongxing Xu589c0f22009-11-12 08:38:56 +000026using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000027using namespace ento;
Zhongxing Xu589c0f22009-11-12 08:38:56 +000028
29namespace {
30
Zhongxing Xu7fb14642009-12-11 00:55:44 +000031class RefState {
Ted Kremenekdde201b2010-08-06 21:12:55 +000032 enum Kind { AllocateUnchecked, AllocateFailed, Released, Escaped,
33 Relinquished } K;
Zhongxing Xu243fde92009-11-17 07:54:15 +000034 const Stmt *S;
35
Zhongxing Xu7fb14642009-12-11 00:55:44 +000036public:
Zhongxing Xu243fde92009-11-17 07:54:15 +000037 RefState(Kind k, const Stmt *s) : K(k), S(s) {}
38
Zhongxing Xub94b81a2009-12-31 06:13:07 +000039 bool isAllocated() const { return K == AllocateUnchecked; }
Chris Lattnerfae96222010-09-03 04:34:38 +000040 //bool isFailed() const { return K == AllocateFailed; }
Zhongxing Xu243fde92009-11-17 07:54:15 +000041 bool isReleased() const { return K == Released; }
Chris Lattnerfae96222010-09-03 04:34:38 +000042 //bool isEscaped() const { return K == Escaped; }
43 //bool isRelinquished() const { return K == Relinquished; }
Zhongxing Xu243fde92009-11-17 07:54:15 +000044
45 bool operator==(const RefState &X) const {
46 return K == X.K && S == X.S;
47 }
48
Zhongxing Xub94b81a2009-12-31 06:13:07 +000049 static RefState getAllocateUnchecked(const Stmt *s) {
50 return RefState(AllocateUnchecked, s);
51 }
52 static RefState getAllocateFailed() {
53 return RefState(AllocateFailed, 0);
54 }
Zhongxing Xu243fde92009-11-17 07:54:15 +000055 static RefState getReleased(const Stmt *s) { return RefState(Released, s); }
56 static RefState getEscaped(const Stmt *s) { return RefState(Escaped, s); }
Ted Kremenekdde201b2010-08-06 21:12:55 +000057 static RefState getRelinquished(const Stmt *s) {
58 return RefState(Relinquished, s);
59 }
Zhongxing Xu243fde92009-11-17 07:54:15 +000060
61 void Profile(llvm::FoldingSetNodeID &ID) const {
62 ID.AddInteger(K);
63 ID.AddPointer(S);
64 }
Zhongxing Xu589c0f22009-11-12 08:38:56 +000065};
66
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000067class RegionState {};
Zhongxing Xu589c0f22009-11-12 08:38:56 +000068
Ted Kremeneke3659a72012-01-04 23:48:37 +000069class MallocChecker : public Checker<eval::Call,
70 check::DeadSymbols,
71 check::EndPath,
72 check::PreStmt<ReturnStmt>,
73 check::Location,
74 check::Bind,
75 eval::Assume>
76{
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000077 mutable llvm::OwningPtr<BuiltinBug> BT_DoubleFree;
78 mutable llvm::OwningPtr<BuiltinBug> BT_Leak;
79 mutable llvm::OwningPtr<BuiltinBug> BT_UseFree;
80 mutable llvm::OwningPtr<BuiltinBug> BT_UseRelinquished;
81 mutable llvm::OwningPtr<BuiltinBug> BT_BadFree;
82 mutable IdentifierInfo *II_malloc, *II_free, *II_realloc, *II_calloc;
Zhongxing Xu589c0f22009-11-12 08:38:56 +000083
84public:
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000085 MallocChecker() : II_malloc(0), II_free(0), II_realloc(0), II_calloc(0) {}
86
87 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
88 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
Anna Zaksaf498a22011-10-25 19:56:48 +000089 void checkEndPath(CheckerContext &C) const;
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000090 void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
Ted Kremenek8bef8232012-01-26 21:29:00 +000091 ProgramStateRef evalAssume(ProgramStateRef state, SVal Cond,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000092 bool Assumption) const;
Anna Zaks390909c2011-10-06 00:43:15 +000093 void checkLocation(SVal l, bool isLoad, const Stmt *S,
94 CheckerContext &C) const;
95 void checkBind(SVal location, SVal val, const Stmt*S,
96 CheckerContext &C) const;
Zhongxing Xub94b81a2009-12-31 06:13:07 +000097
Zhongxing Xu7b760962009-11-13 07:25:27 +000098private:
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +000099 static void MallocMem(CheckerContext &C, const CallExpr *CE);
100 static void MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,
101 const OwnershipAttr* Att);
Ted Kremenek8bef8232012-01-26 21:29:00 +0000102 static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000103 const Expr *SizeEx, SVal Init,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000104 ProgramStateRef state) {
Ted Kremenek5eca4822012-01-06 22:09:28 +0000105 return MallocMemAux(C, CE,
106 state->getSVal(SizeEx, C.getLocationContext()),
107 Init, state);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000108 }
Ted Kremenek8bef8232012-01-26 21:29:00 +0000109 static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000110 SVal SizeEx, SVal Init,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000111 ProgramStateRef state);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000112
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000113 void FreeMem(CheckerContext &C, const CallExpr *CE) const;
Jordy Rose2a479922010-08-12 08:54:03 +0000114 void FreeMemAttr(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000115 const OwnershipAttr* Att) const;
Ted Kremenek8bef8232012-01-26 21:29:00 +0000116 ProgramStateRef FreeMemAux(CheckerContext &C, const CallExpr *CE,
117 ProgramStateRef state, unsigned Num,
Ted Kremeneke3659a72012-01-04 23:48:37 +0000118 bool Hold) const;
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000119
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000120 void ReallocMem(CheckerContext &C, const CallExpr *CE) const;
121 static void CallocMem(CheckerContext &C, const CallExpr *CE);
Jordy Rose43859f62010-06-07 19:32:37 +0000122
Ted Kremenek9c378f72011-08-12 23:37:29 +0000123 static bool SummarizeValue(raw_ostream &os, SVal V);
124 static bool SummarizeRegion(raw_ostream &os, const MemRegion *MR);
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000125 void ReportBadFree(CheckerContext &C, SVal ArgVal, SourceRange range) const;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000126};
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000127} // end anonymous namespace
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000128
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000129typedef llvm::ImmutableMap<SymbolRef, RefState> RegionStateTy;
130
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000131namespace clang {
Ted Kremenek9ef65372010-12-23 07:20:52 +0000132namespace ento {
Zhongxing Xu243fde92009-11-17 07:54:15 +0000133 template <>
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000134 struct ProgramStateTrait<RegionState>
135 : public ProgramStatePartialTrait<RegionStateTy> {
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000136 static void *GDMIndex() { static int x; return &x; }
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000137 };
138}
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000139}
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000140
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000141bool MallocChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
Anna Zaksb805c8f2011-12-01 05:57:37 +0000142 const FunctionDecl *FD = C.getCalleeDecl(CE);
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000143 if (!FD)
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000144 return false;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000145
146 ASTContext &Ctx = C.getASTContext();
147 if (!II_malloc)
148 II_malloc = &Ctx.Idents.get("malloc");
149 if (!II_free)
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000150 II_free = &Ctx.Idents.get("free");
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000151 if (!II_realloc)
152 II_realloc = &Ctx.Idents.get("realloc");
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000153 if (!II_calloc)
154 II_calloc = &Ctx.Idents.get("calloc");
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000155
156 if (FD->getIdentifier() == II_malloc) {
157 MallocMem(C, CE);
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000158 return true;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000159 }
160
161 if (FD->getIdentifier() == II_free) {
162 FreeMem(C, CE);
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000163 return true;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000164 }
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000165
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000166 if (FD->getIdentifier() == II_realloc) {
167 ReallocMem(C, CE);
168 return true;
169 }
170
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000171 if (FD->getIdentifier() == II_calloc) {
172 CallocMem(C, CE);
173 return true;
174 }
175
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000176 // Check all the attributes, if there are any.
177 // There can be multiple of these attributes.
178 bool rv = false;
179 if (FD->hasAttrs()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000180 for (specific_attr_iterator<OwnershipAttr>
181 i = FD->specific_attr_begin<OwnershipAttr>(),
182 e = FD->specific_attr_end<OwnershipAttr>();
183 i != e; ++i) {
184 switch ((*i)->getOwnKind()) {
185 case OwnershipAttr::Returns: {
186 MallocMemReturnsAttr(C, CE, *i);
Jordy Rose2a479922010-08-12 08:54:03 +0000187 rv = true;
188 break;
Sean Huntcf807c42010-08-18 23:23:40 +0000189 }
190 case OwnershipAttr::Takes:
191 case OwnershipAttr::Holds: {
192 FreeMemAttr(C, CE, *i);
Jordy Rose2a479922010-08-12 08:54:03 +0000193 rv = true;
194 break;
Sean Huntcf807c42010-08-18 23:23:40 +0000195 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000196 }
197 }
198 }
199 return rv;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000200}
201
202void MallocChecker::MallocMem(CheckerContext &C, const CallExpr *CE) {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000203 ProgramStateRef state = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(),
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000204 C.getState());
Anna Zaks0bd6b112011-10-26 21:06:34 +0000205 C.addTransition(state);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000206}
207
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000208void MallocChecker::MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,
209 const OwnershipAttr* Att) {
Sean Huntcf807c42010-08-18 23:23:40 +0000210 if (Att->getModule() != "malloc")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000211 return;
212
Sean Huntcf807c42010-08-18 23:23:40 +0000213 OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000214 if (I != E) {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000215 ProgramStateRef state =
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000216 MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), C.getState());
Anna Zaks0bd6b112011-10-26 21:06:34 +0000217 C.addTransition(state);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000218 return;
219 }
Ted Kremenek8bef8232012-01-26 21:29:00 +0000220 ProgramStateRef state = MallocMemAux(C, CE, UnknownVal(), UndefinedVal(),
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000221 C.getState());
Anna Zaks0bd6b112011-10-26 21:06:34 +0000222 C.addTransition(state);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000223}
224
Ted Kremenek8bef8232012-01-26 21:29:00 +0000225ProgramStateRef MallocChecker::MallocMemAux(CheckerContext &C,
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000226 const CallExpr *CE,
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000227 SVal Size, SVal Init,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000228 ProgramStateRef state) {
Anna Zaks5d0ea6d2011-10-04 20:43:05 +0000229 unsigned Count = C.getCurrentBlockCount();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000230 SValBuilder &svalBuilder = C.getSValBuilder();
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000231
Jordy Rose32f26562010-07-04 00:00:41 +0000232 // Set the return value.
Ted Kremeneke3659a72012-01-04 23:48:37 +0000233 SVal retVal = svalBuilder.getConjuredSymbolVal(NULL, CE,
234 CE->getType(), Count);
Ted Kremenek5eca4822012-01-06 22:09:28 +0000235 state = state->BindExpr(CE, C.getLocationContext(), retVal);
Zhongxing Xua49c6b72009-12-11 03:09:01 +0000236
Jordy Rose32f26562010-07-04 00:00:41 +0000237 // Fill the region with the initialization value.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000238 state = state->bindDefault(retVal, Init);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000239
Jordy Rose32f26562010-07-04 00:00:41 +0000240 // Set the region's extent equal to the Size parameter.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000241 const SymbolicRegion *R = cast<SymbolicRegion>(retVal.getAsRegion());
242 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
Jordy Rose32f26562010-07-04 00:00:41 +0000243 DefinedOrUnknownSVal DefinedSize = cast<DefinedOrUnknownSVal>(Size);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000244 DefinedOrUnknownSVal extentMatchesSize =
Ted Kremenek9c149532010-12-01 21:57:22 +0000245 svalBuilder.evalEQ(state, Extent, DefinedSize);
Jordy Rose32f26562010-07-04 00:00:41 +0000246
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000247 state = state->assume(extentMatchesSize, true);
248 assert(state);
249
250 SymbolRef Sym = retVal.getAsLocSymbol();
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000251 assert(Sym);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000252
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000253 // Set the symbol's state to Allocated.
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000254 return state->set<RegionState>(Sym, RefState::getAllocateUnchecked(CE));
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000255}
256
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000257void MallocChecker::FreeMem(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000258 ProgramStateRef state = FreeMemAux(C, CE, C.getState(), 0, false);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000259
260 if (state)
Anna Zaks0bd6b112011-10-26 21:06:34 +0000261 C.addTransition(state);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000262}
263
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000264void MallocChecker::FreeMemAttr(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000265 const OwnershipAttr* Att) const {
Sean Huntcf807c42010-08-18 23:23:40 +0000266 if (Att->getModule() != "malloc")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000267 return;
268
Sean Huntcf807c42010-08-18 23:23:40 +0000269 for (OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
270 I != E; ++I) {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000271 ProgramStateRef state =
Ted Kremeneke3659a72012-01-04 23:48:37 +0000272 FreeMemAux(C, CE, C.getState(), *I,
273 Att->getOwnKind() == OwnershipAttr::Holds);
Sean Huntcf807c42010-08-18 23:23:40 +0000274 if (state)
Anna Zaks0bd6b112011-10-26 21:06:34 +0000275 C.addTransition(state);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000276 }
277}
278
Ted Kremenek8bef8232012-01-26 21:29:00 +0000279ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C,
Ted Kremeneke3659a72012-01-04 23:48:37 +0000280 const CallExpr *CE,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000281 ProgramStateRef state,
Ted Kremeneke3659a72012-01-04 23:48:37 +0000282 unsigned Num,
283 bool Hold) const {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000284 const Expr *ArgExpr = CE->getArg(Num);
Ted Kremenek5eca4822012-01-06 22:09:28 +0000285 SVal ArgVal = state->getSVal(ArgExpr, C.getLocationContext());
Zhongxing Xu181cc3d2010-02-14 06:49:48 +0000286
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000287 DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(ArgVal);
288
289 // Check for null dereferences.
290 if (!isa<Loc>(location))
Zhongxing Xu181cc3d2010-02-14 06:49:48 +0000291 return state;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000292
293 // FIXME: Technically using 'Assume' here can result in a path
294 // bifurcation. In such cases we need to return two states, not just one.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000295 ProgramStateRef notNullState, nullState;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000296 llvm::tie(notNullState, nullState) = state->assume(location);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000297
298 // The explicit NULL case, no operation is performed.
299 if (nullState && !notNullState)
300 return nullState;
301
302 assert(notNullState);
303
Jordy Rose43859f62010-06-07 19:32:37 +0000304 // Unknown values could easily be okay
305 // Undefined values are handled elsewhere
306 if (ArgVal.isUnknownOrUndef())
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000307 return notNullState;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000308
Jordy Rose43859f62010-06-07 19:32:37 +0000309 const MemRegion *R = ArgVal.getAsRegion();
310
311 // Nonlocs can't be freed, of course.
312 // Non-region locations (labels and fixed addresses) also shouldn't be freed.
313 if (!R) {
314 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
315 return NULL;
316 }
317
318 R = R->StripCasts();
319
320 // Blocks might show up as heap data, but should not be free()d
321 if (isa<BlockDataRegion>(R)) {
322 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
323 return NULL;
324 }
325
326 const MemSpaceRegion *MS = R->getMemorySpace();
327
328 // Parameters, locals, statics, and globals shouldn't be freed.
329 if (!(isa<UnknownSpaceRegion>(MS) || isa<HeapSpaceRegion>(MS))) {
330 // FIXME: at the time this code was written, malloc() regions were
331 // represented by conjured symbols, which are all in UnknownSpaceRegion.
332 // This means that there isn't actually anything from HeapSpaceRegion
333 // that should be freed, even though we allow it here.
334 // Of course, free() can work on memory allocated outside the current
335 // function, so UnknownSpaceRegion is always a possibility.
336 // False negatives are better than false positives.
337
338 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
339 return NULL;
340 }
341
342 const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R);
343 // Various cases could lead to non-symbol values here.
344 // For now, ignore them.
345 if (!SR)
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000346 return notNullState;
Jordy Rose43859f62010-06-07 19:32:37 +0000347
348 SymbolRef Sym = SR->getSymbol();
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000349 const RefState *RS = state->get<RegionState>(Sym);
Zhongxing Xu7e3cda92010-01-18 03:27:34 +0000350
351 // If the symbol has not been tracked, return. This is possible when free() is
352 // called on a pointer that does not get its pointee directly from malloc().
353 // Full support of this requires inter-procedural analysis.
354 if (!RS)
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000355 return notNullState;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000356
357 // Check double free.
Zhongxing Xu243fde92009-11-17 07:54:15 +0000358 if (RS->isReleased()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000359 if (ExplodedNode *N = C.generateSink()) {
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000360 if (!BT_DoubleFree)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000361 BT_DoubleFree.reset(
362 new BuiltinBug("Double free",
363 "Try to free a memory block that has been released"));
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000364 // FIXME: should find where it's freed last time.
365 BugReport *R = new BugReport(*BT_DoubleFree,
Benjamin Kramerd02e2322009-11-14 12:08:24 +0000366 BT_DoubleFree->getDescription(), N);
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000367 C.EmitReport(R);
368 }
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000369 return NULL;
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000370 }
371
372 // Normal free.
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000373 if (Hold)
374 return notNullState->set<RegionState>(Sym, RefState::getRelinquished(CE));
375 return notNullState->set<RegionState>(Sym, RefState::getReleased(CE));
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000376}
377
Ted Kremenek9c378f72011-08-12 23:37:29 +0000378bool MallocChecker::SummarizeValue(raw_ostream &os, SVal V) {
Jordy Rose43859f62010-06-07 19:32:37 +0000379 if (nonloc::ConcreteInt *IntVal = dyn_cast<nonloc::ConcreteInt>(&V))
380 os << "an integer (" << IntVal->getValue() << ")";
381 else if (loc::ConcreteInt *ConstAddr = dyn_cast<loc::ConcreteInt>(&V))
382 os << "a constant address (" << ConstAddr->getValue() << ")";
383 else if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&V))
Chris Lattner68106302011-02-17 05:38:27 +0000384 os << "the address of the label '" << Label->getLabel()->getName() << "'";
Jordy Rose43859f62010-06-07 19:32:37 +0000385 else
386 return false;
387
388 return true;
389}
390
Ted Kremenek9c378f72011-08-12 23:37:29 +0000391bool MallocChecker::SummarizeRegion(raw_ostream &os,
Jordy Rose43859f62010-06-07 19:32:37 +0000392 const MemRegion *MR) {
393 switch (MR->getKind()) {
394 case MemRegion::FunctionTextRegionKind: {
395 const FunctionDecl *FD = cast<FunctionTextRegion>(MR)->getDecl();
396 if (FD)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000397 os << "the address of the function '" << *FD << '\'';
Jordy Rose43859f62010-06-07 19:32:37 +0000398 else
399 os << "the address of a function";
400 return true;
401 }
402 case MemRegion::BlockTextRegionKind:
403 os << "block text";
404 return true;
405 case MemRegion::BlockDataRegionKind:
406 // FIXME: where the block came from?
407 os << "a block";
408 return true;
409 default: {
410 const MemSpaceRegion *MS = MR->getMemorySpace();
411
Anna Zakseb31a762012-01-04 23:54:01 +0000412 if (isa<StackLocalsSpaceRegion>(MS)) {
Jordy Rose43859f62010-06-07 19:32:37 +0000413 const VarRegion *VR = dyn_cast<VarRegion>(MR);
414 const VarDecl *VD;
415 if (VR)
416 VD = VR->getDecl();
417 else
418 VD = NULL;
419
420 if (VD)
421 os << "the address of the local variable '" << VD->getName() << "'";
422 else
423 os << "the address of a local stack variable";
424 return true;
425 }
Anna Zakseb31a762012-01-04 23:54:01 +0000426
427 if (isa<StackArgumentsSpaceRegion>(MS)) {
Jordy Rose43859f62010-06-07 19:32:37 +0000428 const VarRegion *VR = dyn_cast<VarRegion>(MR);
429 const VarDecl *VD;
430 if (VR)
431 VD = VR->getDecl();
432 else
433 VD = NULL;
434
435 if (VD)
436 os << "the address of the parameter '" << VD->getName() << "'";
437 else
438 os << "the address of a parameter";
439 return true;
440 }
Anna Zakseb31a762012-01-04 23:54:01 +0000441
442 if (isa<GlobalsSpaceRegion>(MS)) {
Jordy Rose43859f62010-06-07 19:32:37 +0000443 const VarRegion *VR = dyn_cast<VarRegion>(MR);
444 const VarDecl *VD;
445 if (VR)
446 VD = VR->getDecl();
447 else
448 VD = NULL;
449
450 if (VD) {
451 if (VD->isStaticLocal())
452 os << "the address of the static variable '" << VD->getName() << "'";
453 else
454 os << "the address of the global variable '" << VD->getName() << "'";
455 } else
456 os << "the address of a global variable";
457 return true;
458 }
Anna Zakseb31a762012-01-04 23:54:01 +0000459
460 return false;
Jordy Rose43859f62010-06-07 19:32:37 +0000461 }
462 }
463}
464
465void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal,
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000466 SourceRange range) const {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000467 if (ExplodedNode *N = C.generateSink()) {
Jordy Rose43859f62010-06-07 19:32:37 +0000468 if (!BT_BadFree)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000469 BT_BadFree.reset(new BuiltinBug("Bad free"));
Jordy Rose43859f62010-06-07 19:32:37 +0000470
471 llvm::SmallString<100> buf;
472 llvm::raw_svector_ostream os(buf);
473
474 const MemRegion *MR = ArgVal.getAsRegion();
475 if (MR) {
476 while (const ElementRegion *ER = dyn_cast<ElementRegion>(MR))
477 MR = ER->getSuperRegion();
478
479 // Special case for alloca()
480 if (isa<AllocaRegion>(MR))
481 os << "Argument to free() was allocated by alloca(), not malloc()";
482 else {
483 os << "Argument to free() is ";
484 if (SummarizeRegion(os, MR))
485 os << ", which is not memory allocated by malloc()";
486 else
487 os << "not memory allocated by malloc()";
488 }
489 } else {
490 os << "Argument to free() is ";
491 if (SummarizeValue(os, ArgVal))
492 os << ", which is not memory allocated by malloc()";
493 else
494 os << "not memory allocated by malloc()";
495 }
496
Anna Zakse172e8b2011-08-17 23:00:25 +0000497 BugReport *R = new BugReport(*BT_BadFree, os.str(), N);
Jordy Rose43859f62010-06-07 19:32:37 +0000498 R->addRange(range);
499 C.EmitReport(R);
500 }
501}
502
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000503void MallocChecker::ReallocMem(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000504 ProgramStateRef state = C.getState();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000505 const Expr *arg0Expr = CE->getArg(0);
Ted Kremenek5eca4822012-01-06 22:09:28 +0000506 const LocationContext *LCtx = C.getLocationContext();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000507 DefinedOrUnknownSVal arg0Val
Ted Kremenek5eca4822012-01-06 22:09:28 +0000508 = cast<DefinedOrUnknownSVal>(state->getSVal(arg0Expr, LCtx));
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000509
Ted Kremenek846eabd2010-12-01 21:28:31 +0000510 SValBuilder &svalBuilder = C.getSValBuilder();
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000511
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000512 DefinedOrUnknownSVal PtrEQ =
513 svalBuilder.evalEQ(state, arg0Val, svalBuilder.makeNull());
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000514
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000515 // Get the size argument. If there is no size arg then give up.
516 const Expr *Arg1 = CE->getArg(1);
517 if (!Arg1)
518 return;
519
520 // Get the value of the size argument.
521 DefinedOrUnknownSVal Arg1Val =
Ted Kremenek5eca4822012-01-06 22:09:28 +0000522 cast<DefinedOrUnknownSVal>(state->getSVal(Arg1, LCtx));
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000523
524 // Compare the size argument to 0.
525 DefinedOrUnknownSVal SizeZero =
526 svalBuilder.evalEQ(state, Arg1Val,
527 svalBuilder.makeIntValWithPtrWidth(0, false));
528
529 // If the ptr is NULL and the size is not 0, the call is equivalent to
530 // malloc(size).
Ted Kremenek8bef8232012-01-26 21:29:00 +0000531 ProgramStateRef stateEqual = state->assume(PtrEQ, true);
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000532 if (stateEqual && state->assume(SizeZero, false)) {
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000533 // Hack: set the NULL symbolic region to released to suppress false warning.
534 // In the future we should add more states for allocated regions, e.g.,
535 // CheckedNull, CheckedNonNull.
536
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000537 SymbolRef Sym = arg0Val.getAsLocSymbol();
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000538 if (Sym)
539 stateEqual = stateEqual->set<RegionState>(Sym, RefState::getReleased(CE));
540
Ted Kremenek8bef8232012-01-26 21:29:00 +0000541 ProgramStateRef stateMalloc = MallocMemAux(C, CE, CE->getArg(1),
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000542 UndefinedVal(), stateEqual);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000543 C.addTransition(stateMalloc);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000544 }
545
Ted Kremenek8bef8232012-01-26 21:29:00 +0000546 if (ProgramStateRef stateNotEqual = state->assume(PtrEQ, false)) {
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000547 // If the size is 0, free the memory.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000548 if (ProgramStateRef stateSizeZero =
Ted Kremeneke3659a72012-01-04 23:48:37 +0000549 stateNotEqual->assume(SizeZero, true))
Ted Kremenek8bef8232012-01-26 21:29:00 +0000550 if (ProgramStateRef stateFree =
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000551 FreeMemAux(C, CE, stateSizeZero, 0, false)) {
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000552
Zhongxing Xud56763f2011-09-01 04:53:59 +0000553 // Bind the return value to NULL because it is now free.
Ted Kremenek5eca4822012-01-06 22:09:28 +0000554 C.addTransition(stateFree->BindExpr(CE, LCtx,
555 svalBuilder.makeNull(), true));
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000556 }
Ted Kremenek8bef8232012-01-26 21:29:00 +0000557 if (ProgramStateRef stateSizeNotZero =
Ted Kremeneke3659a72012-01-04 23:48:37 +0000558 stateNotEqual->assume(SizeZero,false))
Ted Kremenek8bef8232012-01-26 21:29:00 +0000559 if (ProgramStateRef stateFree = FreeMemAux(C, CE, stateSizeNotZero,
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000560 0, false)) {
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000561 // FIXME: We should copy the content of the original buffer.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000562 ProgramStateRef stateRealloc = MallocMemAux(C, CE, CE->getArg(1),
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000563 UnknownVal(), stateFree);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000564 C.addTransition(stateRealloc);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000565 }
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000566 }
Zhongxing Xu589c0f22009-11-12 08:38:56 +0000567}
Zhongxing Xu7b760962009-11-13 07:25:27 +0000568
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000569void MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE) {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000570 ProgramStateRef state = C.getState();
Ted Kremenek846eabd2010-12-01 21:28:31 +0000571 SValBuilder &svalBuilder = C.getSValBuilder();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000572 const LocationContext *LCtx = C.getLocationContext();
573 SVal count = state->getSVal(CE->getArg(0), LCtx);
574 SVal elementSize = state->getSVal(CE->getArg(1), LCtx);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000575 SVal TotalSize = svalBuilder.evalBinOp(state, BO_Mul, count, elementSize,
576 svalBuilder.getContext().getSizeType());
577 SVal zeroVal = svalBuilder.makeZeroVal(svalBuilder.getContext().CharTy);
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000578
Anna Zaks0bd6b112011-10-26 21:06:34 +0000579 C.addTransition(MallocMemAux(C, CE, TotalSize, zeroVal, state));
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000580}
581
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000582void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper,
583 CheckerContext &C) const
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000584{
Zhongxing Xu173ff562010-08-15 08:19:57 +0000585 if (!SymReaper.hasDeadSymbols())
586 return;
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000587
Ted Kremenek8bef8232012-01-26 21:29:00 +0000588 ProgramStateRef state = C.getState();
Zhongxing Xu173ff562010-08-15 08:19:57 +0000589 RegionStateTy RS = state->get<RegionState>();
Jordy Rose90760142010-08-18 04:33:47 +0000590 RegionStateTy::Factory &F = state->get_context<RegionState>();
Zhongxing Xu173ff562010-08-15 08:19:57 +0000591
Ted Kremenek217470e2011-07-28 23:07:51 +0000592 bool generateReport = false;
593
Zhongxing Xu173ff562010-08-15 08:19:57 +0000594 for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
595 if (SymReaper.isDead(I->first)) {
Ted Kremenek217470e2011-07-28 23:07:51 +0000596 if (I->second.isAllocated())
597 generateReport = true;
Jordy Rose90760142010-08-18 04:33:47 +0000598
599 // Remove the dead symbol from the map.
Ted Kremenek3baf6722010-11-24 00:54:37 +0000600 RS = F.remove(RS, I->first);
Ted Kremenek217470e2011-07-28 23:07:51 +0000601
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +0000602 }
603 }
Ted Kremenek217470e2011-07-28 23:07:51 +0000604
Anna Zaks0bd6b112011-10-26 21:06:34 +0000605 ExplodedNode *N = C.addTransition(state->set<RegionState>(RS));
Ted Kremenek217470e2011-07-28 23:07:51 +0000606
607 // FIXME: This does not handle when we have multiple leaks at a single
608 // place.
609 if (N && generateReport) {
610 if (!BT_Leak)
611 BT_Leak.reset(new BuiltinBug("Memory leak",
612 "Allocated memory never released. Potential memory leak."));
613 // FIXME: where it is allocated.
614 BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
615 C.EmitReport(R);
616 }
Zhongxing Xu7b760962009-11-13 07:25:27 +0000617}
Zhongxing Xu243fde92009-11-17 07:54:15 +0000618
Anna Zaksaf498a22011-10-25 19:56:48 +0000619void MallocChecker::checkEndPath(CheckerContext &Ctx) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +0000620 ProgramStateRef state = Ctx.getState();
Jordy Rose09cef092010-08-18 04:26:59 +0000621 RegionStateTy M = state->get<RegionState>();
Zhongxing Xu243fde92009-11-17 07:54:15 +0000622
Jordy Rose09cef092010-08-18 04:26:59 +0000623 for (RegionStateTy::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Zhongxing Xu243fde92009-11-17 07:54:15 +0000624 RefState RS = I->second;
625 if (RS.isAllocated()) {
Anna Zaks0bd6b112011-10-26 21:06:34 +0000626 ExplodedNode *N = Ctx.addTransition(state);
Zhongxing Xu243fde92009-11-17 07:54:15 +0000627 if (N) {
628 if (!BT_Leak)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000629 BT_Leak.reset(new BuiltinBug("Memory leak",
630 "Allocated memory never released. Potential memory leak."));
Zhongxing Xu243fde92009-11-17 07:54:15 +0000631 BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
Anna Zaksaf498a22011-10-25 19:56:48 +0000632 Ctx.EmitReport(R);
Zhongxing Xu243fde92009-11-17 07:54:15 +0000633 }
634 }
635 }
636}
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000637
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000638void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000639 const Expr *retExpr = S->getRetValue();
640 if (!retExpr)
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000641 return;
642
Ted Kremenek8bef8232012-01-26 21:29:00 +0000643 ProgramStateRef state = C.getState();
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000644
Ted Kremenek5eca4822012-01-06 22:09:28 +0000645 SymbolRef Sym = state->getSVal(retExpr, C.getLocationContext()).getAsSymbol();
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000646 if (!Sym)
647 return;
648
649 const RefState *RS = state->get<RegionState>(Sym);
650 if (!RS)
651 return;
652
653 // FIXME: check other cases.
654 if (RS->isAllocated())
655 state = state->set<RegionState>(Sym, RefState::getEscaped(S));
656
Anna Zaks0bd6b112011-10-26 21:06:34 +0000657 C.addTransition(state);
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000658}
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000659
Ted Kremenek8bef8232012-01-26 21:29:00 +0000660ProgramStateRef MallocChecker::evalAssume(ProgramStateRef state,
Ted Kremeneke3659a72012-01-04 23:48:37 +0000661 SVal Cond,
662 bool Assumption) const {
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000663 // If a symblic region is assumed to NULL, set its state to AllocateFailed.
664 // FIXME: should also check symbols assumed to non-null.
665
666 RegionStateTy RS = state->get<RegionState>();
667
668 for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
Zhongxing Xu2bfa3012011-04-02 03:20:45 +0000669 // If the symbol is assumed to NULL, this will return an APSInt*.
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000670 if (state->getSymVal(I.getKey()))
671 state = state->set<RegionState>(I.getKey(),RefState::getAllocateFailed());
672 }
673
674 return state;
675}
Zhongxing Xuc8023782010-03-10 04:58:55 +0000676
677// Check if the location is a freed symbolic region.
Anna Zaks390909c2011-10-06 00:43:15 +0000678void MallocChecker::checkLocation(SVal l, bool isLoad, const Stmt *S,
679 CheckerContext &C) const {
Zhongxing Xuc8023782010-03-10 04:58:55 +0000680 SymbolRef Sym = l.getLocSymbolInBase();
681 if (Sym) {
682 const RefState *RS = C.getState()->get<RegionState>(Sym);
Ted Kremenekcea68652010-08-06 21:12:49 +0000683 if (RS && RS->isReleased()) {
Anna Zaks0bd6b112011-10-26 21:06:34 +0000684 if (ExplodedNode *N = C.addTransition()) {
Zhongxing Xuc8023782010-03-10 04:58:55 +0000685 if (!BT_UseFree)
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000686 BT_UseFree.reset(new BuiltinBug("Use dynamically allocated memory "
687 "after it is freed."));
Zhongxing Xuc8023782010-03-10 04:58:55 +0000688
689 BugReport *R = new BugReport(*BT_UseFree, BT_UseFree->getDescription(),
690 N);
691 C.EmitReport(R);
692 }
Ted Kremenekcea68652010-08-06 21:12:49 +0000693 }
Zhongxing Xuc8023782010-03-10 04:58:55 +0000694 }
695}
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000696
Anna Zaks390909c2011-10-06 00:43:15 +0000697void MallocChecker::checkBind(SVal location, SVal val,
698 const Stmt *BindS, CheckerContext &C) const {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000699 // The PreVisitBind implements the same algorithm as already used by the
700 // Objective C ownership checker: if the pointer escaped from this scope by
701 // assignment, let it go. However, assigning to fields of a stack-storage
702 // structure does not transfer ownership.
703
Ted Kremenek8bef8232012-01-26 21:29:00 +0000704 ProgramStateRef state = C.getState();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000705 DefinedOrUnknownSVal l = cast<DefinedOrUnknownSVal>(location);
706
707 // Check for null dereferences.
708 if (!isa<Loc>(l))
709 return;
710
711 // Before checking if the state is null, check if 'val' has a RefState.
712 // Only then should we check for null and bifurcate the state.
713 SymbolRef Sym = val.getLocSymbolInBase();
714 if (Sym) {
715 if (const RefState *RS = state->get<RegionState>(Sym)) {
716 // If ptr is NULL, no operation is performed.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000717 ProgramStateRef notNullState, nullState;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000718 llvm::tie(notNullState, nullState) = state->assume(l);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000719
720 // Generate a transition for 'nullState' to record the assumption
721 // that the state was null.
722 if (nullState)
Anna Zaks0bd6b112011-10-26 21:06:34 +0000723 C.addTransition(nullState);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000724
725 if (!notNullState)
726 return;
727
728 if (RS->isAllocated()) {
729 // Something we presently own is being assigned somewhere.
730 const MemRegion *AR = location.getAsRegion();
731 if (!AR)
732 return;
733 AR = AR->StripCasts()->getBaseRegion();
734 do {
735 // If it is on the stack, we still own it.
736 if (AR->hasStackNonParametersStorage())
737 break;
738
739 // If the state can't represent this binding, we still own it.
Ted Kremenekdde201b2010-08-06 21:12:55 +0000740 if (notNullState == (notNullState->bindLoc(cast<Loc>(location),
741 UnknownVal())))
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000742 break;
743
744 // We no longer own this pointer.
Ted Kremenekdde201b2010-08-06 21:12:55 +0000745 notNullState =
746 notNullState->set<RegionState>(Sym,
Anna Zaks390909c2011-10-06 00:43:15 +0000747 RefState::getRelinquished(BindS));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000748 }
749 while (false);
750 }
Anna Zaks0bd6b112011-10-26 21:06:34 +0000751 C.addTransition(notNullState);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000752 }
753 }
754}
Argyrios Kyrtzidis312dbec2011-02-28 01:26:35 +0000755
756void ento::registerMallocChecker(CheckerManager &mgr) {
757 mgr.registerChecker<MallocChecker>();
758}