2009-11-12 16:38:56 +08:00
|
|
|
//=== MallocChecker.cpp - A malloc/free checker -------------------*- C++ -*--//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines malloc/free checker, which checks for potential memory
|
|
|
|
// leaks, double free, and use-after-free problems.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-28 09:26:35 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-28 09:26:35 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
|
2009-11-12 16:38:56 +08:00
|
|
|
#include "llvm/ADT/ImmutableMap.h"
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-11-12 16:38:56 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-12-11 08:55:44 +08:00
|
|
|
class RefState {
|
2010-08-07 05:12:55 +08:00
|
|
|
enum Kind { AllocateUnchecked, AllocateFailed, Released, Escaped,
|
|
|
|
Relinquished } K;
|
2009-11-17 15:54:15 +08:00
|
|
|
const Stmt *S;
|
|
|
|
|
2009-12-11 08:55:44 +08:00
|
|
|
public:
|
2009-11-17 15:54:15 +08:00
|
|
|
RefState(Kind k, const Stmt *s) : K(k), S(s) {}
|
|
|
|
|
2009-12-31 14:13:07 +08:00
|
|
|
bool isAllocated() const { return K == AllocateUnchecked; }
|
2010-09-03 12:34:38 +08:00
|
|
|
//bool isFailed() const { return K == AllocateFailed; }
|
2009-11-17 15:54:15 +08:00
|
|
|
bool isReleased() const { return K == Released; }
|
2010-09-03 12:34:38 +08:00
|
|
|
//bool isEscaped() const { return K == Escaped; }
|
|
|
|
//bool isRelinquished() const { return K == Relinquished; }
|
2009-11-17 15:54:15 +08:00
|
|
|
|
|
|
|
bool operator==(const RefState &X) const {
|
|
|
|
return K == X.K && S == X.S;
|
|
|
|
}
|
|
|
|
|
2009-12-31 14:13:07 +08:00
|
|
|
static RefState getAllocateUnchecked(const Stmt *s) {
|
|
|
|
return RefState(AllocateUnchecked, s);
|
|
|
|
}
|
|
|
|
static RefState getAllocateFailed() {
|
|
|
|
return RefState(AllocateFailed, 0);
|
|
|
|
}
|
2009-11-17 15:54:15 +08:00
|
|
|
static RefState getReleased(const Stmt *s) { return RefState(Released, s); }
|
|
|
|
static RefState getEscaped(const Stmt *s) { return RefState(Escaped, s); }
|
2010-08-07 05:12:55 +08:00
|
|
|
static RefState getRelinquished(const Stmt *s) {
|
|
|
|
return RefState(Relinquished, s);
|
|
|
|
}
|
2009-11-17 15:54:15 +08:00
|
|
|
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
ID.AddInteger(K);
|
|
|
|
ID.AddPointer(S);
|
|
|
|
}
|
2009-11-12 16:38:56 +08:00
|
|
|
};
|
|
|
|
|
2009-11-28 14:07:30 +08:00
|
|
|
class RegionState {};
|
2009-11-12 16:38:56 +08:00
|
|
|
|
2011-03-01 09:16:21 +08:00
|
|
|
class MallocChecker : public Checker<eval::Call, check::DeadSymbols, check::EndPath, check::PreStmt<ReturnStmt>, check::Location,
|
2011-02-28 09:26:35 +08:00
|
|
|
check::Bind, eval::Assume> {
|
|
|
|
mutable llvm::OwningPtr<BuiltinBug> BT_DoubleFree;
|
|
|
|
mutable llvm::OwningPtr<BuiltinBug> BT_Leak;
|
|
|
|
mutable llvm::OwningPtr<BuiltinBug> BT_UseFree;
|
|
|
|
mutable llvm::OwningPtr<BuiltinBug> BT_UseRelinquished;
|
|
|
|
mutable llvm::OwningPtr<BuiltinBug> BT_BadFree;
|
|
|
|
mutable IdentifierInfo *II_malloc, *II_free, *II_realloc, *II_calloc;
|
2009-11-12 16:38:56 +08:00
|
|
|
|
|
|
|
public:
|
2011-02-28 09:26:35 +08:00
|
|
|
MallocChecker() : II_malloc(0), II_free(0), II_realloc(0), II_calloc(0) {}
|
|
|
|
|
|
|
|
bool evalCall(const CallExpr *CE, CheckerContext &C) const;
|
|
|
|
void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
|
2011-10-26 03:56:48 +08:00
|
|
|
void checkEndPath(CheckerContext &C) const;
|
2011-02-28 09:26:35 +08:00
|
|
|
void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *evalAssume(const ProgramState *state, SVal Cond,
|
2011-02-28 09:26:35 +08:00
|
|
|
bool Assumption) const;
|
2011-10-06 08:43:15 +08:00
|
|
|
void checkLocation(SVal l, bool isLoad, const Stmt *S,
|
|
|
|
CheckerContext &C) const;
|
|
|
|
void checkBind(SVal location, SVal val, const Stmt*S,
|
|
|
|
CheckerContext &C) const;
|
2009-12-31 14:13:07 +08:00
|
|
|
|
2009-11-13 15:25:27 +08:00
|
|
|
private:
|
2011-02-28 09:26:35 +08:00
|
|
|
static void MallocMem(CheckerContext &C, const CallExpr *CE);
|
|
|
|
static void MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,
|
|
|
|
const OwnershipAttr* Att);
|
2011-08-16 06:09:50 +08:00
|
|
|
static const ProgramState *MallocMemAux(CheckerContext &C, const CallExpr *CE,
|
2011-02-28 09:26:35 +08:00
|
|
|
const Expr *SizeEx, SVal Init,
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state) {
|
2010-06-01 11:01:33 +08:00
|
|
|
return MallocMemAux(C, CE, state->getSVal(SizeEx), Init, state);
|
|
|
|
}
|
2011-08-16 06:09:50 +08:00
|
|
|
static const ProgramState *MallocMemAux(CheckerContext &C, const CallExpr *CE,
|
2011-02-28 09:26:35 +08:00
|
|
|
SVal SizeEx, SVal Init,
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state);
|
2010-06-01 11:01:33 +08:00
|
|
|
|
2011-02-28 09:26:35 +08:00
|
|
|
void FreeMem(CheckerContext &C, const CallExpr *CE) const;
|
2010-08-12 16:54:03 +08:00
|
|
|
void FreeMemAttr(CheckerContext &C, const CallExpr *CE,
|
2011-02-28 09:26:35 +08:00
|
|
|
const OwnershipAttr* Att) const;
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *FreeMemAux(CheckerContext &C, const CallExpr *CE,
|
|
|
|
const ProgramState *state, unsigned Num, bool Hold) const;
|
2009-12-12 20:29:38 +08:00
|
|
|
|
2011-02-28 09:26:35 +08:00
|
|
|
void ReallocMem(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
static void CallocMem(CheckerContext &C, const CallExpr *CE);
|
2010-06-08 03:32:37 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
static bool SummarizeValue(raw_ostream &os, SVal V);
|
|
|
|
static bool SummarizeRegion(raw_ostream &os, const MemRegion *MR);
|
2011-02-28 09:26:35 +08:00
|
|
|
void ReportBadFree(CheckerContext &C, SVal ArgVal, SourceRange range) const;
|
2009-11-12 16:38:56 +08:00
|
|
|
};
|
2009-11-28 14:07:30 +08:00
|
|
|
} // end anonymous namespace
|
2009-11-12 16:38:56 +08:00
|
|
|
|
2009-12-31 14:13:07 +08:00
|
|
|
typedef llvm::ImmutableMap<SymbolRef, RefState> RegionStateTy;
|
|
|
|
|
2009-11-12 16:38:56 +08:00
|
|
|
namespace clang {
|
2010-12-23 15:20:52 +08:00
|
|
|
namespace ento {
|
2009-11-17 15:54:15 +08:00
|
|
|
template <>
|
2011-08-16 06:09:50 +08:00
|
|
|
struct ProgramStateTrait<RegionState>
|
|
|
|
: public ProgramStatePartialTrait<RegionStateTy> {
|
2011-02-28 09:26:35 +08:00
|
|
|
static void *GDMIndex() { static int x; return &x; }
|
2009-11-12 16:38:56 +08:00
|
|
|
};
|
|
|
|
}
|
2010-12-23 02:53:20 +08:00
|
|
|
}
|
2009-11-12 16:38:56 +08:00
|
|
|
|
2011-02-28 09:26:35 +08:00
|
|
|
bool MallocChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
|
2011-12-01 13:57:37 +08:00
|
|
|
const FunctionDecl *FD = C.getCalleeDecl(CE);
|
2009-11-12 16:38:56 +08:00
|
|
|
if (!FD)
|
2009-12-11 11:09:01 +08:00
|
|
|
return false;
|
2009-11-12 16:38:56 +08:00
|
|
|
|
|
|
|
ASTContext &Ctx = C.getASTContext();
|
|
|
|
if (!II_malloc)
|
|
|
|
II_malloc = &Ctx.Idents.get("malloc");
|
|
|
|
if (!II_free)
|
2009-11-13 15:48:11 +08:00
|
|
|
II_free = &Ctx.Idents.get("free");
|
2009-12-12 20:29:38 +08:00
|
|
|
if (!II_realloc)
|
|
|
|
II_realloc = &Ctx.Idents.get("realloc");
|
2010-06-01 11:01:33 +08:00
|
|
|
if (!II_calloc)
|
|
|
|
II_calloc = &Ctx.Idents.get("calloc");
|
2009-11-12 16:38:56 +08:00
|
|
|
|
|
|
|
if (FD->getIdentifier() == II_malloc) {
|
|
|
|
MallocMem(C, CE);
|
2009-12-11 11:09:01 +08:00
|
|
|
return true;
|
2009-11-12 16:38:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FD->getIdentifier() == II_free) {
|
|
|
|
FreeMem(C, CE);
|
2009-12-11 11:09:01 +08:00
|
|
|
return true;
|
2009-11-12 16:38:56 +08:00
|
|
|
}
|
2009-12-11 11:09:01 +08:00
|
|
|
|
2009-12-12 20:29:38 +08:00
|
|
|
if (FD->getIdentifier() == II_realloc) {
|
|
|
|
ReallocMem(C, CE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-01 11:01:33 +08:00
|
|
|
if (FD->getIdentifier() == II_calloc) {
|
|
|
|
CallocMem(C, CE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-31 09:52:11 +08:00
|
|
|
// Check all the attributes, if there are any.
|
|
|
|
// There can be multiple of these attributes.
|
|
|
|
bool rv = false;
|
|
|
|
if (FD->hasAttrs()) {
|
2010-08-19 07:23:40 +08:00
|
|
|
for (specific_attr_iterator<OwnershipAttr>
|
|
|
|
i = FD->specific_attr_begin<OwnershipAttr>(),
|
|
|
|
e = FD->specific_attr_end<OwnershipAttr>();
|
|
|
|
i != e; ++i) {
|
|
|
|
switch ((*i)->getOwnKind()) {
|
|
|
|
case OwnershipAttr::Returns: {
|
|
|
|
MallocMemReturnsAttr(C, CE, *i);
|
2010-08-12 16:54:03 +08:00
|
|
|
rv = true;
|
|
|
|
break;
|
2010-08-19 07:23:40 +08:00
|
|
|
}
|
|
|
|
case OwnershipAttr::Takes:
|
|
|
|
case OwnershipAttr::Holds: {
|
|
|
|
FreeMemAttr(C, CE, *i);
|
2010-08-12 16:54:03 +08:00
|
|
|
rv = true;
|
|
|
|
break;
|
2010-08-19 07:23:40 +08:00
|
|
|
}
|
2010-08-12 16:54:03 +08:00
|
|
|
default:
|
|
|
|
break;
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
2009-11-12 16:38:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MallocChecker::MallocMem(CheckerContext &C, const CallExpr *CE) {
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(),
|
2010-06-01 11:01:33 +08:00
|
|
|
C.getState());
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2009-12-12 20:29:38 +08:00
|
|
|
}
|
|
|
|
|
2010-07-31 09:52:11 +08:00
|
|
|
void MallocChecker::MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,
|
|
|
|
const OwnershipAttr* Att) {
|
2010-08-19 07:23:40 +08:00
|
|
|
if (Att->getModule() != "malloc")
|
2010-07-31 09:52:11 +08:00
|
|
|
return;
|
|
|
|
|
2010-08-19 07:23:40 +08:00
|
|
|
OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
|
2010-07-31 09:52:11 +08:00
|
|
|
if (I != E) {
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state =
|
2010-07-31 09:52:11 +08:00
|
|
|
MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), C.getState());
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2010-07-31 09:52:11 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = MallocMemAux(C, CE, UnknownVal(), UndefinedVal(),
|
2010-07-31 09:52:11 +08:00
|
|
|
C.getState());
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *MallocChecker::MallocMemAux(CheckerContext &C,
|
2009-12-12 20:29:38 +08:00
|
|
|
const CallExpr *CE,
|
2010-06-01 11:01:33 +08:00
|
|
|
SVal Size, SVal Init,
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state) {
|
2011-10-05 04:43:05 +08:00
|
|
|
unsigned Count = C.getCurrentBlockCount();
|
2010-12-02 15:49:45 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
2009-12-11 11:09:01 +08:00
|
|
|
|
2010-07-04 08:00:41 +08:00
|
|
|
// Set the return value.
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal retVal = svalBuilder.getConjuredSymbolVal(NULL, CE, CE->getType(), Count);
|
|
|
|
state = state->BindExpr(CE, retVal);
|
2009-12-11 11:09:01 +08:00
|
|
|
|
2010-07-04 08:00:41 +08:00
|
|
|
// Fill the region with the initialization value.
|
2010-12-02 15:49:45 +08:00
|
|
|
state = state->bindDefault(retVal, Init);
|
2010-06-01 11:01:33 +08:00
|
|
|
|
2010-07-04 08:00:41 +08:00
|
|
|
// Set the region's extent equal to the Size parameter.
|
2010-12-02 15:49:45 +08:00
|
|
|
const SymbolicRegion *R = cast<SymbolicRegion>(retVal.getAsRegion());
|
|
|
|
DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
|
2010-07-04 08:00:41 +08:00
|
|
|
DefinedOrUnknownSVal DefinedSize = cast<DefinedOrUnknownSVal>(Size);
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal extentMatchesSize =
|
2010-12-02 05:57:22 +08:00
|
|
|
svalBuilder.evalEQ(state, Extent, DefinedSize);
|
2010-07-04 08:00:41 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
state = state->assume(extentMatchesSize, true);
|
|
|
|
assert(state);
|
|
|
|
|
|
|
|
SymbolRef Sym = retVal.getAsLocSymbol();
|
2009-11-12 16:38:56 +08:00
|
|
|
assert(Sym);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2009-11-12 16:38:56 +08:00
|
|
|
// Set the symbol's state to Allocated.
|
2009-12-31 14:13:07 +08:00
|
|
|
return state->set<RegionState>(Sym, RefState::getAllocateUnchecked(CE));
|
2009-11-12 16:38:56 +08:00
|
|
|
}
|
|
|
|
|
2011-02-28 09:26:35 +08:00
|
|
|
void MallocChecker::FreeMem(CheckerContext &C, const CallExpr *CE) const {
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = FreeMemAux(C, CE, C.getState(), 0, false);
|
2009-12-12 20:29:38 +08:00
|
|
|
|
|
|
|
if (state)
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2009-12-12 20:29:38 +08:00
|
|
|
}
|
|
|
|
|
2010-07-31 09:52:11 +08:00
|
|
|
void MallocChecker::FreeMemAttr(CheckerContext &C, const CallExpr *CE,
|
2011-02-28 09:26:35 +08:00
|
|
|
const OwnershipAttr* Att) const {
|
2010-08-19 07:23:40 +08:00
|
|
|
if (Att->getModule() != "malloc")
|
2010-07-31 09:52:11 +08:00
|
|
|
return;
|
|
|
|
|
2010-08-19 07:23:40 +08:00
|
|
|
for (OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
|
|
|
|
I != E; ++I) {
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = FreeMemAux(C, CE, C.getState(), *I,
|
2010-08-19 07:23:40 +08:00
|
|
|
Att->getOwnKind() == OwnershipAttr::Holds);
|
|
|
|
if (state)
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *MallocChecker::FreeMemAux(CheckerContext &C, const CallExpr *CE,
|
|
|
|
const ProgramState *state, unsigned Num,
|
2011-02-28 09:26:35 +08:00
|
|
|
bool Hold) const {
|
2010-07-31 09:52:11 +08:00
|
|
|
const Expr *ArgExpr = CE->getArg(Num);
|
2010-06-08 03:32:37 +08:00
|
|
|
SVal ArgVal = state->getSVal(ArgExpr);
|
2010-02-14 14:49:48 +08:00
|
|
|
|
2010-07-31 09:52:11 +08:00
|
|
|
DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(ArgVal);
|
|
|
|
|
|
|
|
// Check for null dereferences.
|
|
|
|
if (!isa<Loc>(location))
|
2010-02-14 14:49:48 +08:00
|
|
|
return state;
|
2010-07-31 09:52:11 +08:00
|
|
|
|
|
|
|
// FIXME: Technically using 'Assume' here can result in a path
|
|
|
|
// bifurcation. In such cases we need to return two states, not just one.
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *notNullState, *nullState;
|
2010-12-02 06:16:56 +08:00
|
|
|
llvm::tie(notNullState, nullState) = state->assume(location);
|
2010-07-31 09:52:11 +08:00
|
|
|
|
|
|
|
// The explicit NULL case, no operation is performed.
|
|
|
|
if (nullState && !notNullState)
|
|
|
|
return nullState;
|
|
|
|
|
|
|
|
assert(notNullState);
|
|
|
|
|
2010-06-08 03:32:37 +08:00
|
|
|
// Unknown values could easily be okay
|
|
|
|
// Undefined values are handled elsewhere
|
|
|
|
if (ArgVal.isUnknownOrUndef())
|
2010-07-31 09:52:11 +08:00
|
|
|
return notNullState;
|
2010-02-14 14:49:48 +08:00
|
|
|
|
2010-06-08 03:32:37 +08:00
|
|
|
const MemRegion *R = ArgVal.getAsRegion();
|
|
|
|
|
|
|
|
// Nonlocs can't be freed, of course.
|
|
|
|
// Non-region locations (labels and fixed addresses) also shouldn't be freed.
|
|
|
|
if (!R) {
|
|
|
|
ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
R = R->StripCasts();
|
|
|
|
|
|
|
|
// Blocks might show up as heap data, but should not be free()d
|
|
|
|
if (isa<BlockDataRegion>(R)) {
|
|
|
|
ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MemSpaceRegion *MS = R->getMemorySpace();
|
|
|
|
|
|
|
|
// Parameters, locals, statics, and globals shouldn't be freed.
|
|
|
|
if (!(isa<UnknownSpaceRegion>(MS) || isa<HeapSpaceRegion>(MS))) {
|
|
|
|
// FIXME: at the time this code was written, malloc() regions were
|
|
|
|
// represented by conjured symbols, which are all in UnknownSpaceRegion.
|
|
|
|
// This means that there isn't actually anything from HeapSpaceRegion
|
|
|
|
// that should be freed, even though we allow it here.
|
|
|
|
// Of course, free() can work on memory allocated outside the current
|
|
|
|
// function, so UnknownSpaceRegion is always a possibility.
|
|
|
|
// False negatives are better than false positives.
|
|
|
|
|
|
|
|
ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R);
|
2010-05-13 16:26:32 +08:00
|
|
|
// Various cases could lead to non-symbol values here.
|
2010-06-08 03:32:37 +08:00
|
|
|
// For now, ignore them.
|
|
|
|
if (!SR)
|
2010-07-31 09:52:11 +08:00
|
|
|
return notNullState;
|
2009-11-12 16:38:56 +08:00
|
|
|
|
2010-06-08 03:32:37 +08:00
|
|
|
SymbolRef Sym = SR->getSymbol();
|
2009-11-12 16:38:56 +08:00
|
|
|
const RefState *RS = state->get<RegionState>(Sym);
|
2010-01-18 11:27:34 +08:00
|
|
|
|
|
|
|
// If the symbol has not been tracked, return. This is possible when free() is
|
|
|
|
// called on a pointer that does not get its pointee directly from malloc().
|
|
|
|
// Full support of this requires inter-procedural analysis.
|
|
|
|
if (!RS)
|
2010-07-31 09:52:11 +08:00
|
|
|
return notNullState;
|
2009-11-12 16:38:56 +08:00
|
|
|
|
|
|
|
// Check double free.
|
2009-11-17 15:54:15 +08:00
|
|
|
if (RS->isReleased()) {
|
2010-12-21 05:19:09 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
2009-11-12 16:38:56 +08:00
|
|
|
if (!BT_DoubleFree)
|
2011-02-28 09:26:35 +08:00
|
|
|
BT_DoubleFree.reset(
|
|
|
|
new BuiltinBug("Double free",
|
|
|
|
"Try to free a memory block that has been released"));
|
2009-11-12 16:38:56 +08:00
|
|
|
// FIXME: should find where it's freed last time.
|
|
|
|
BugReport *R = new BugReport(*BT_DoubleFree,
|
2009-11-14 20:08:24 +08:00
|
|
|
BT_DoubleFree->getDescription(), N);
|
2009-11-12 16:38:56 +08:00
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
2009-12-12 20:29:38 +08:00
|
|
|
return NULL;
|
2009-11-12 16:38:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Normal free.
|
2010-07-31 09:52:11 +08:00
|
|
|
if (Hold)
|
|
|
|
return notNullState->set<RegionState>(Sym, RefState::getRelinquished(CE));
|
|
|
|
return notNullState->set<RegionState>(Sym, RefState::getReleased(CE));
|
2009-12-12 20:29:38 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
bool MallocChecker::SummarizeValue(raw_ostream &os, SVal V) {
|
2010-06-08 03:32:37 +08:00
|
|
|
if (nonloc::ConcreteInt *IntVal = dyn_cast<nonloc::ConcreteInt>(&V))
|
|
|
|
os << "an integer (" << IntVal->getValue() << ")";
|
|
|
|
else if (loc::ConcreteInt *ConstAddr = dyn_cast<loc::ConcreteInt>(&V))
|
|
|
|
os << "a constant address (" << ConstAddr->getValue() << ")";
|
|
|
|
else if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&V))
|
2011-02-17 13:38:27 +08:00
|
|
|
os << "the address of the label '" << Label->getLabel()->getName() << "'";
|
2010-06-08 03:32:37 +08:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
bool MallocChecker::SummarizeRegion(raw_ostream &os,
|
2010-06-08 03:32:37 +08:00
|
|
|
const MemRegion *MR) {
|
|
|
|
switch (MR->getKind()) {
|
|
|
|
case MemRegion::FunctionTextRegionKind: {
|
|
|
|
const FunctionDecl *FD = cast<FunctionTextRegion>(MR)->getDecl();
|
|
|
|
if (FD)
|
2011-10-15 02:45:37 +08:00
|
|
|
os << "the address of the function '" << *FD << '\'';
|
2010-06-08 03:32:37 +08:00
|
|
|
else
|
|
|
|
os << "the address of a function";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case MemRegion::BlockTextRegionKind:
|
|
|
|
os << "block text";
|
|
|
|
return true;
|
|
|
|
case MemRegion::BlockDataRegionKind:
|
|
|
|
// FIXME: where the block came from?
|
|
|
|
os << "a block";
|
|
|
|
return true;
|
|
|
|
default: {
|
|
|
|
const MemSpaceRegion *MS = MR->getMemorySpace();
|
|
|
|
|
|
|
|
switch (MS->getKind()) {
|
|
|
|
case MemRegion::StackLocalsSpaceRegionKind: {
|
|
|
|
const VarRegion *VR = dyn_cast<VarRegion>(MR);
|
|
|
|
const VarDecl *VD;
|
|
|
|
if (VR)
|
|
|
|
VD = VR->getDecl();
|
|
|
|
else
|
|
|
|
VD = NULL;
|
|
|
|
|
|
|
|
if (VD)
|
|
|
|
os << "the address of the local variable '" << VD->getName() << "'";
|
|
|
|
else
|
|
|
|
os << "the address of a local stack variable";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case MemRegion::StackArgumentsSpaceRegionKind: {
|
|
|
|
const VarRegion *VR = dyn_cast<VarRegion>(MR);
|
|
|
|
const VarDecl *VD;
|
|
|
|
if (VR)
|
|
|
|
VD = VR->getDecl();
|
|
|
|
else
|
|
|
|
VD = NULL;
|
|
|
|
|
|
|
|
if (VD)
|
|
|
|
os << "the address of the parameter '" << VD->getName() << "'";
|
|
|
|
else
|
|
|
|
os << "the address of a parameter";
|
|
|
|
return true;
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
case MemRegion::NonStaticGlobalSpaceRegionKind:
|
|
|
|
case MemRegion::StaticGlobalSpaceRegionKind: {
|
2010-06-08 03:32:37 +08:00
|
|
|
const VarRegion *VR = dyn_cast<VarRegion>(MR);
|
|
|
|
const VarDecl *VD;
|
|
|
|
if (VR)
|
|
|
|
VD = VR->getDecl();
|
|
|
|
else
|
|
|
|
VD = NULL;
|
|
|
|
|
|
|
|
if (VD) {
|
|
|
|
if (VD->isStaticLocal())
|
|
|
|
os << "the address of the static variable '" << VD->getName() << "'";
|
|
|
|
else
|
|
|
|
os << "the address of the global variable '" << VD->getName() << "'";
|
|
|
|
} else
|
|
|
|
os << "the address of a global variable";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal,
|
2011-02-28 09:26:35 +08:00
|
|
|
SourceRange range) const {
|
2010-12-21 05:19:09 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
2010-06-08 03:32:37 +08:00
|
|
|
if (!BT_BadFree)
|
2011-02-28 09:26:35 +08:00
|
|
|
BT_BadFree.reset(new BuiltinBug("Bad free"));
|
2010-06-08 03:32:37 +08:00
|
|
|
|
|
|
|
llvm::SmallString<100> buf;
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
|
|
|
|
const MemRegion *MR = ArgVal.getAsRegion();
|
|
|
|
if (MR) {
|
|
|
|
while (const ElementRegion *ER = dyn_cast<ElementRegion>(MR))
|
|
|
|
MR = ER->getSuperRegion();
|
|
|
|
|
|
|
|
// Special case for alloca()
|
|
|
|
if (isa<AllocaRegion>(MR))
|
|
|
|
os << "Argument to free() was allocated by alloca(), not malloc()";
|
|
|
|
else {
|
|
|
|
os << "Argument to free() is ";
|
|
|
|
if (SummarizeRegion(os, MR))
|
|
|
|
os << ", which is not memory allocated by malloc()";
|
|
|
|
else
|
|
|
|
os << "not memory allocated by malloc()";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
os << "Argument to free() is ";
|
|
|
|
if (SummarizeValue(os, ArgVal))
|
|
|
|
os << ", which is not memory allocated by malloc()";
|
|
|
|
else
|
|
|
|
os << "not memory allocated by malloc()";
|
|
|
|
}
|
|
|
|
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *R = new BugReport(*BT_BadFree, os.str(), N);
|
2010-06-08 03:32:37 +08:00
|
|
|
R->addRange(range);
|
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-28 09:26:35 +08:00
|
|
|
void MallocChecker::ReallocMem(CheckerContext &C, const CallExpr *CE) const {
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = C.getState();
|
2010-12-02 15:49:45 +08:00
|
|
|
const Expr *arg0Expr = CE->getArg(0);
|
|
|
|
DefinedOrUnknownSVal arg0Val
|
|
|
|
= cast<DefinedOrUnknownSVal>(state->getSVal(arg0Expr));
|
2009-12-12 20:29:38 +08:00
|
|
|
|
2010-12-02 05:28:31 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
2009-12-12 20:29:38 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal PtrEQ =
|
|
|
|
svalBuilder.evalEQ(state, arg0Val, svalBuilder.makeNull());
|
2009-12-12 20:29:38 +08:00
|
|
|
|
2011-04-27 22:49:29 +08:00
|
|
|
// Get the size argument. If there is no size arg then give up.
|
|
|
|
const Expr *Arg1 = CE->getArg(1);
|
|
|
|
if (!Arg1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the value of the size argument.
|
|
|
|
DefinedOrUnknownSVal Arg1Val =
|
|
|
|
cast<DefinedOrUnknownSVal>(state->getSVal(Arg1));
|
|
|
|
|
|
|
|
// Compare the size argument to 0.
|
|
|
|
DefinedOrUnknownSVal SizeZero =
|
|
|
|
svalBuilder.evalEQ(state, Arg1Val,
|
|
|
|
svalBuilder.makeIntValWithPtrWidth(0, false));
|
|
|
|
|
|
|
|
// If the ptr is NULL and the size is not 0, the call is equivalent to
|
|
|
|
// malloc(size).
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *stateEqual = state->assume(PtrEQ, true);
|
2011-04-27 22:49:29 +08:00
|
|
|
if (stateEqual && state->assume(SizeZero, false)) {
|
2009-12-12 20:29:38 +08:00
|
|
|
// Hack: set the NULL symbolic region to released to suppress false warning.
|
|
|
|
// In the future we should add more states for allocated regions, e.g.,
|
|
|
|
// CheckedNull, CheckedNonNull.
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
SymbolRef Sym = arg0Val.getAsLocSymbol();
|
2009-12-12 20:29:38 +08:00
|
|
|
if (Sym)
|
|
|
|
stateEqual = stateEqual->set<RegionState>(Sym, RefState::getReleased(CE));
|
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *stateMalloc = MallocMemAux(C, CE, CE->getArg(1),
|
2010-06-01 11:01:33 +08:00
|
|
|
UndefinedVal(), stateEqual);
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(stateMalloc);
|
2009-12-12 20:29:38 +08:00
|
|
|
}
|
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
if (const ProgramState *stateNotEqual = state->assume(PtrEQ, false)) {
|
2011-04-27 22:49:29 +08:00
|
|
|
// If the size is 0, free the memory.
|
2011-08-16 06:09:50 +08:00
|
|
|
if (const ProgramState *stateSizeZero = stateNotEqual->assume(SizeZero, true))
|
|
|
|
if (const ProgramState *stateFree =
|
2011-04-27 22:49:29 +08:00
|
|
|
FreeMemAux(C, CE, stateSizeZero, 0, false)) {
|
|
|
|
|
2011-09-01 12:53:59 +08:00
|
|
|
// Bind the return value to NULL because it is now free.
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(stateFree->BindExpr(CE, svalBuilder.makeNull(), true));
|
2011-04-27 22:49:29 +08:00
|
|
|
}
|
2011-08-16 06:09:50 +08:00
|
|
|
if (const ProgramState *stateSizeNotZero = stateNotEqual->assume(SizeZero,false))
|
|
|
|
if (const ProgramState *stateFree = FreeMemAux(C, CE, stateSizeNotZero,
|
2010-12-02 15:49:45 +08:00
|
|
|
0, false)) {
|
2009-12-12 20:29:38 +08:00
|
|
|
// FIXME: We should copy the content of the original buffer.
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *stateRealloc = MallocMemAux(C, CE, CE->getArg(1),
|
2010-06-01 11:01:33 +08:00
|
|
|
UnknownVal(), stateFree);
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(stateRealloc);
|
2009-12-12 20:29:38 +08:00
|
|
|
}
|
|
|
|
}
|
2009-11-12 16:38:56 +08:00
|
|
|
}
|
2009-11-13 15:25:27 +08:00
|
|
|
|
2010-06-01 11:01:33 +08:00
|
|
|
void MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE) {
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = C.getState();
|
2010-12-02 05:28:31 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
2010-06-01 11:01:33 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal count = state->getSVal(CE->getArg(0));
|
|
|
|
SVal elementSize = state->getSVal(CE->getArg(1));
|
|
|
|
SVal TotalSize = svalBuilder.evalBinOp(state, BO_Mul, count, elementSize,
|
|
|
|
svalBuilder.getContext().getSizeType());
|
|
|
|
SVal zeroVal = svalBuilder.makeZeroVal(svalBuilder.getContext().CharTy);
|
2010-06-01 11:01:33 +08:00
|
|
|
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(MallocMemAux(C, CE, TotalSize, zeroVal, state));
|
2010-06-01 11:01:33 +08:00
|
|
|
}
|
|
|
|
|
2011-02-28 09:26:35 +08:00
|
|
|
void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper,
|
|
|
|
CheckerContext &C) const
|
2010-12-02 15:49:45 +08:00
|
|
|
{
|
2010-08-15 16:19:57 +08:00
|
|
|
if (!SymReaper.hasDeadSymbols())
|
|
|
|
return;
|
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = C.getState();
|
2010-08-15 16:19:57 +08:00
|
|
|
RegionStateTy RS = state->get<RegionState>();
|
2010-08-18 12:33:47 +08:00
|
|
|
RegionStateTy::Factory &F = state->get_context<RegionState>();
|
2010-08-15 16:19:57 +08:00
|
|
|
|
2011-07-29 07:07:51 +08:00
|
|
|
bool generateReport = false;
|
|
|
|
|
2010-08-15 16:19:57 +08:00
|
|
|
for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
|
|
|
|
if (SymReaper.isDead(I->first)) {
|
2011-07-29 07:07:51 +08:00
|
|
|
if (I->second.isAllocated())
|
|
|
|
generateReport = true;
|
2010-08-18 12:33:47 +08:00
|
|
|
|
|
|
|
// Remove the dead symbol from the map.
|
2010-11-24 08:54:37 +08:00
|
|
|
RS = F.remove(RS, I->first);
|
2011-07-29 07:07:51 +08:00
|
|
|
|
2009-11-13 15:48:11 +08:00
|
|
|
}
|
|
|
|
}
|
2011-07-29 07:07:51 +08:00
|
|
|
|
2011-10-27 05:06:34 +08:00
|
|
|
ExplodedNode *N = C.addTransition(state->set<RegionState>(RS));
|
2011-07-29 07:07:51 +08:00
|
|
|
|
|
|
|
// FIXME: This does not handle when we have multiple leaks at a single
|
|
|
|
// place.
|
|
|
|
if (N && generateReport) {
|
|
|
|
if (!BT_Leak)
|
|
|
|
BT_Leak.reset(new BuiltinBug("Memory leak",
|
|
|
|
"Allocated memory never released. Potential memory leak."));
|
|
|
|
// FIXME: where it is allocated.
|
|
|
|
BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
|
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
2009-11-13 15:25:27 +08:00
|
|
|
}
|
2009-11-17 15:54:15 +08:00
|
|
|
|
2011-10-26 03:56:48 +08:00
|
|
|
void MallocChecker::checkEndPath(CheckerContext &Ctx) const {
|
|
|
|
const ProgramState *state = Ctx.getState();
|
2010-08-18 12:26:59 +08:00
|
|
|
RegionStateTy M = state->get<RegionState>();
|
2009-11-17 15:54:15 +08:00
|
|
|
|
2010-08-18 12:26:59 +08:00
|
|
|
for (RegionStateTy::iterator I = M.begin(), E = M.end(); I != E; ++I) {
|
2009-11-17 15:54:15 +08:00
|
|
|
RefState RS = I->second;
|
|
|
|
if (RS.isAllocated()) {
|
2011-10-27 05:06:34 +08:00
|
|
|
ExplodedNode *N = Ctx.addTransition(state);
|
2009-11-17 15:54:15 +08:00
|
|
|
if (N) {
|
|
|
|
if (!BT_Leak)
|
2011-02-28 09:26:35 +08:00
|
|
|
BT_Leak.reset(new BuiltinBug("Memory leak",
|
|
|
|
"Allocated memory never released. Potential memory leak."));
|
2009-11-17 15:54:15 +08:00
|
|
|
BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
|
2011-10-26 03:56:48 +08:00
|
|
|
Ctx.EmitReport(R);
|
2009-11-17 15:54:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-17 16:58:18 +08:00
|
|
|
|
2011-02-28 09:26:35 +08:00
|
|
|
void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
|
2010-12-02 15:49:45 +08:00
|
|
|
const Expr *retExpr = S->getRetValue();
|
|
|
|
if (!retExpr)
|
2009-11-17 16:58:18 +08:00
|
|
|
return;
|
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = C.getState();
|
2009-11-17 16:58:18 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
SymbolRef Sym = state->getSVal(retExpr).getAsSymbol();
|
2009-11-17 16:58:18 +08:00
|
|
|
if (!Sym)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const RefState *RS = state->get<RegionState>(Sym);
|
|
|
|
if (!RS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// FIXME: check other cases.
|
|
|
|
if (RS->isAllocated())
|
|
|
|
state = state->set<RegionState>(Sym, RefState::getEscaped(S));
|
|
|
|
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2009-11-17 16:58:18 +08:00
|
|
|
}
|
2009-12-31 14:13:07 +08:00
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *MallocChecker::evalAssume(const ProgramState *state, SVal Cond,
|
2011-02-28 09:26:35 +08:00
|
|
|
bool Assumption) const {
|
2009-12-31 14:13:07 +08:00
|
|
|
// If a symblic region is assumed to NULL, set its state to AllocateFailed.
|
|
|
|
// FIXME: should also check symbols assumed to non-null.
|
|
|
|
|
|
|
|
RegionStateTy RS = state->get<RegionState>();
|
|
|
|
|
|
|
|
for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
|
2011-04-02 11:20:45 +08:00
|
|
|
// If the symbol is assumed to NULL, this will return an APSInt*.
|
2009-12-31 14:13:07 +08:00
|
|
|
if (state->getSymVal(I.getKey()))
|
|
|
|
state = state->set<RegionState>(I.getKey(),RefState::getAllocateFailed());
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
2010-03-10 12:58:55 +08:00
|
|
|
|
|
|
|
// Check if the location is a freed symbolic region.
|
2011-10-06 08:43:15 +08:00
|
|
|
void MallocChecker::checkLocation(SVal l, bool isLoad, const Stmt *S,
|
|
|
|
CheckerContext &C) const {
|
2010-03-10 12:58:55 +08:00
|
|
|
SymbolRef Sym = l.getLocSymbolInBase();
|
|
|
|
if (Sym) {
|
|
|
|
const RefState *RS = C.getState()->get<RegionState>(Sym);
|
2010-08-07 05:12:49 +08:00
|
|
|
if (RS && RS->isReleased()) {
|
2011-10-27 05:06:34 +08:00
|
|
|
if (ExplodedNode *N = C.addTransition()) {
|
2010-03-10 12:58:55 +08:00
|
|
|
if (!BT_UseFree)
|
2011-02-28 09:26:35 +08:00
|
|
|
BT_UseFree.reset(new BuiltinBug("Use dynamically allocated memory "
|
|
|
|
"after it is freed."));
|
2010-03-10 12:58:55 +08:00
|
|
|
|
|
|
|
BugReport *R = new BugReport(*BT_UseFree, BT_UseFree->getDescription(),
|
|
|
|
N);
|
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
2010-08-07 05:12:49 +08:00
|
|
|
}
|
2010-03-10 12:58:55 +08:00
|
|
|
}
|
|
|
|
}
|
2010-07-31 09:52:11 +08:00
|
|
|
|
2011-10-06 08:43:15 +08:00
|
|
|
void MallocChecker::checkBind(SVal location, SVal val,
|
|
|
|
const Stmt *BindS, CheckerContext &C) const {
|
2010-07-31 09:52:11 +08:00
|
|
|
// The PreVisitBind implements the same algorithm as already used by the
|
|
|
|
// Objective C ownership checker: if the pointer escaped from this scope by
|
|
|
|
// assignment, let it go. However, assigning to fields of a stack-storage
|
|
|
|
// structure does not transfer ownership.
|
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = C.getState();
|
2010-07-31 09:52:11 +08:00
|
|
|
DefinedOrUnknownSVal l = cast<DefinedOrUnknownSVal>(location);
|
|
|
|
|
|
|
|
// Check for null dereferences.
|
|
|
|
if (!isa<Loc>(l))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Before checking if the state is null, check if 'val' has a RefState.
|
|
|
|
// Only then should we check for null and bifurcate the state.
|
|
|
|
SymbolRef Sym = val.getLocSymbolInBase();
|
|
|
|
if (Sym) {
|
|
|
|
if (const RefState *RS = state->get<RegionState>(Sym)) {
|
|
|
|
// If ptr is NULL, no operation is performed.
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *notNullState, *nullState;
|
2010-12-02 06:16:56 +08:00
|
|
|
llvm::tie(notNullState, nullState) = state->assume(l);
|
2010-07-31 09:52:11 +08:00
|
|
|
|
|
|
|
// Generate a transition for 'nullState' to record the assumption
|
|
|
|
// that the state was null.
|
|
|
|
if (nullState)
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(nullState);
|
2010-07-31 09:52:11 +08:00
|
|
|
|
|
|
|
if (!notNullState)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (RS->isAllocated()) {
|
|
|
|
// Something we presently own is being assigned somewhere.
|
|
|
|
const MemRegion *AR = location.getAsRegion();
|
|
|
|
if (!AR)
|
|
|
|
return;
|
|
|
|
AR = AR->StripCasts()->getBaseRegion();
|
|
|
|
do {
|
|
|
|
// If it is on the stack, we still own it.
|
|
|
|
if (AR->hasStackNonParametersStorage())
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If the state can't represent this binding, we still own it.
|
2010-08-07 05:12:55 +08:00
|
|
|
if (notNullState == (notNullState->bindLoc(cast<Loc>(location),
|
|
|
|
UnknownVal())))
|
2010-07-31 09:52:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
// We no longer own this pointer.
|
2010-08-07 05:12:55 +08:00
|
|
|
notNullState =
|
|
|
|
notNullState->set<RegionState>(Sym,
|
2011-10-06 08:43:15 +08:00
|
|
|
RefState::getRelinquished(BindS));
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
|
|
|
while (false);
|
|
|
|
}
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(notNullState);
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-28 09:26:35 +08:00
|
|
|
|
|
|
|
void ento::registerMallocChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<MallocChecker>();
|
|
|
|
}
|