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"
|
2012-02-12 07:46:36 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2009-11-12 16:38:56 +08:00
|
|
|
#include "llvm/ADT/ImmutableMap.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-02-04 20:31:12 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2009-11-12 16:38:56 +08:00
|
|
|
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; }
|
2012-02-14 02:05:39 +08:00
|
|
|
const Stmt *getStmt() const { return S; }
|
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
|
|
|
};
|
|
|
|
|
2012-02-09 04:13:28 +08:00
|
|
|
class MallocChecker : public Checker<check::DeadSymbols,
|
2012-01-05 07:48:37 +08:00
|
|
|
check::EndPath,
|
|
|
|
check::PreStmt<ReturnStmt>,
|
2012-02-09 04:13:28 +08:00
|
|
|
check::PostStmt<CallExpr>,
|
2012-01-05 07:48:37 +08:00
|
|
|
check::Location,
|
|
|
|
check::Bind,
|
2012-02-12 05:02:35 +08:00
|
|
|
eval::Assume,
|
|
|
|
check::RegionChanges>
|
2012-01-05 07:48:37 +08:00
|
|
|
{
|
2012-02-05 10:12:40 +08:00
|
|
|
mutable OwningPtr<BuiltinBug> BT_DoubleFree;
|
|
|
|
mutable OwningPtr<BuiltinBug> BT_Leak;
|
|
|
|
mutable OwningPtr<BuiltinBug> BT_UseFree;
|
|
|
|
mutable OwningPtr<BuiltinBug> BT_UseRelinquished;
|
|
|
|
mutable OwningPtr<BuiltinBug> BT_BadFree;
|
2011-02-28 09:26:35 +08:00
|
|
|
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) {}
|
2012-02-09 07:16:52 +08:00
|
|
|
|
|
|
|
/// In pessimistic mode, the checker assumes that it does not know which
|
|
|
|
/// functions might free the memory.
|
|
|
|
struct ChecksFilter {
|
|
|
|
DefaultBool CMallocPessimistic;
|
|
|
|
DefaultBool CMallocOptimistic;
|
|
|
|
};
|
|
|
|
|
|
|
|
ChecksFilter Filter;
|
|
|
|
|
2012-02-09 04:13:28 +08:00
|
|
|
void initIdentifierInfo(CheckerContext &C) const;
|
2011-02-28 09:26:35 +08:00
|
|
|
|
2012-02-09 04:13:28 +08:00
|
|
|
void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
|
2011-02-28 09:26:35 +08:00
|
|
|
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;
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef evalAssume(ProgramStateRef 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;
|
2012-02-12 05:02:35 +08:00
|
|
|
ProgramStateRef
|
|
|
|
checkRegionChanges(ProgramStateRef state,
|
|
|
|
const StoreManager::InvalidatedSymbols *invalidated,
|
|
|
|
ArrayRef<const MemRegion *> ExplicitRegions,
|
|
|
|
ArrayRef<const MemRegion *> Regions) const;
|
|
|
|
bool wantsRegionChangeUpdate(ProgramStateRef state) const {
|
|
|
|
return true;
|
|
|
|
}
|
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);
|
2012-01-27 05:29:00 +08:00
|
|
|
static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE,
|
2011-02-28 09:26:35 +08:00
|
|
|
const Expr *SizeEx, SVal Init,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state) {
|
2012-01-07 06:09:28 +08:00
|
|
|
return MallocMemAux(C, CE,
|
|
|
|
state->getSVal(SizeEx, C.getLocationContext()),
|
|
|
|
Init, state);
|
2010-06-01 11:01:33 +08:00
|
|
|
}
|
2012-01-27 05:29:00 +08:00
|
|
|
static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE,
|
2011-02-28 09:26:35 +08:00
|
|
|
SVal SizeEx, SVal Init,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef 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;
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef FreeMemAux(CheckerContext &C, const CallExpr *CE,
|
|
|
|
ProgramStateRef state, unsigned Num,
|
2012-01-05 07:48:37 +08:00
|
|
|
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
|
|
|
|
2012-02-09 07:16:56 +08:00
|
|
|
bool checkEscape(SymbolRef Sym, const Stmt *S, CheckerContext &C) const;
|
|
|
|
bool checkUseAfterFree(SymbolRef Sym, CheckerContext &C,
|
|
|
|
const Stmt *S = 0) const;
|
|
|
|
|
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;
|
2012-02-09 14:25:51 +08:00
|
|
|
|
2012-02-12 05:02:40 +08:00
|
|
|
void reportLeak(SymbolRef Sym, ExplodedNode *N, CheckerContext &C) const;
|
|
|
|
|
2012-02-09 14:25:51 +08:00
|
|
|
/// The bug visitor which allows us to print extra diagnostics along the
|
|
|
|
/// BugReport path. For example, showing the allocation site of the leaked
|
|
|
|
/// region.
|
|
|
|
class MallocBugVisitor : public BugReporterVisitor {
|
|
|
|
protected:
|
|
|
|
// The allocated region symbol tracked by the main analysis.
|
|
|
|
SymbolRef Sym;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MallocBugVisitor(SymbolRef S) : Sym(S) {}
|
|
|
|
virtual ~MallocBugVisitor() {}
|
|
|
|
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
static int X = 0;
|
|
|
|
ID.AddPointer(&X);
|
|
|
|
ID.AddPointer(Sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isAllocated(const RefState *S, const RefState *SPrev) {
|
|
|
|
// Did not track -> allocated. Other state (released) -> allocated.
|
|
|
|
return ((S && S->isAllocated()) && (!SPrev || !SPrev->isAllocated()));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isReleased(const RefState *S, const RefState *SPrev) {
|
|
|
|
// Did not track -> released. Other state (allocated) -> released.
|
|
|
|
return ((S && S->isReleased()) && (!SPrev || !SPrev->isReleased()));
|
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
|
|
|
|
const ExplodedNode *PrevN,
|
|
|
|
BugReporterContext &BRC,
|
|
|
|
BugReport &BR);
|
|
|
|
};
|
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;
|
2012-02-14 02:05:39 +08:00
|
|
|
typedef llvm::ImmutableMap<SymbolRef, SymbolRef> SymRefToSymRefTy;
|
|
|
|
class RegionState {};
|
|
|
|
class ReallocPairs {};
|
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
|
|
|
};
|
2012-02-14 02:05:39 +08:00
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ProgramStateTrait<ReallocPairs>
|
|
|
|
: public ProgramStatePartialTrait<SymRefToSymRefTy> {
|
|
|
|
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
|
|
|
|
2012-02-12 05:02:35 +08:00
|
|
|
namespace {
|
|
|
|
class StopTrackingCallback : public SymbolVisitor {
|
|
|
|
ProgramStateRef state;
|
|
|
|
public:
|
|
|
|
StopTrackingCallback(ProgramStateRef st) : state(st) {}
|
|
|
|
ProgramStateRef getState() const { return state; }
|
|
|
|
|
|
|
|
bool VisitSymbol(SymbolRef sym) {
|
|
|
|
state = state->remove<RegionState>(sym);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2012-02-09 04:13:28 +08:00
|
|
|
void MallocChecker::initIdentifierInfo(CheckerContext &C) const {
|
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");
|
2012-02-09 04:13:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MallocChecker::checkPostStmt(const CallExpr *CE, CheckerContext &C) const {
|
|
|
|
const FunctionDecl *FD = C.getCalleeDecl(CE);
|
|
|
|
if (!FD)
|
|
|
|
return;
|
|
|
|
initIdentifierInfo(C);
|
2009-11-12 16:38:56 +08:00
|
|
|
|
|
|
|
if (FD->getIdentifier() == II_malloc) {
|
|
|
|
MallocMem(C, CE);
|
2012-02-09 04:13:28 +08:00
|
|
|
return;
|
2009-11-12 16:38:56 +08:00
|
|
|
}
|
2009-12-12 20:29:38 +08:00
|
|
|
if (FD->getIdentifier() == II_realloc) {
|
|
|
|
ReallocMem(C, CE);
|
2012-02-09 04:13:28 +08:00
|
|
|
return;
|
2009-12-12 20:29:38 +08:00
|
|
|
}
|
|
|
|
|
2010-06-01 11:01:33 +08:00
|
|
|
if (FD->getIdentifier() == II_calloc) {
|
|
|
|
CallocMem(C, CE);
|
2012-02-09 04:13:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD->getIdentifier() == II_free) {
|
|
|
|
FreeMem(C, CE);
|
|
|
|
return;
|
2010-06-01 11:01:33 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 07:16:56 +08:00
|
|
|
if (Filter.CMallocOptimistic)
|
2010-07-31 09:52:11 +08:00
|
|
|
// Check all the attributes, if there are any.
|
|
|
|
// There can be multiple of these attributes.
|
|
|
|
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);
|
2012-02-12 07:46:36 +08:00
|
|
|
return;
|
2010-08-19 07:23:40 +08:00
|
|
|
}
|
|
|
|
case OwnershipAttr::Takes:
|
|
|
|
case OwnershipAttr::Holds: {
|
|
|
|
FreeMemAttr(C, CE, *i);
|
2012-02-12 07:46:36 +08:00
|
|
|
return;
|
2010-08-19 07:23:40 +08:00
|
|
|
}
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-09 07:16:56 +08:00
|
|
|
|
2012-02-12 07:46:36 +08:00
|
|
|
// Check use after free, when a freed pointer is passed to a call.
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
for (CallExpr::const_arg_iterator I = CE->arg_begin(),
|
|
|
|
E = CE->arg_end(); I != E; ++I) {
|
|
|
|
const Expr *A = *I;
|
|
|
|
if (A->getType().getTypePtr()->isAnyPointerType()) {
|
|
|
|
SymbolRef Sym = State->getSVal(A, C.getLocationContext()).getAsSymbol();
|
|
|
|
if (!Sym)
|
|
|
|
continue;
|
|
|
|
if (checkUseAfterFree(Sym, C, A))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The pointer might escape through a function call.
|
|
|
|
// TODO: This should be rewritten to take into account inlining.
|
2012-02-09 07:16:56 +08:00
|
|
|
if (Filter.CMallocPessimistic) {
|
2012-02-12 07:46:36 +08:00
|
|
|
SourceLocation FLoc = FD->getLocation();
|
|
|
|
// We assume that the pointers cannot escape through calls to system
|
|
|
|
// functions.
|
|
|
|
if (C.getSourceManager().isInSystemHeader(FLoc))
|
|
|
|
return;
|
|
|
|
|
2012-02-09 07:16:56 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
for (CallExpr::const_arg_iterator I = CE->arg_begin(),
|
|
|
|
E = CE->arg_end(); I != E; ++I) {
|
|
|
|
const Expr *A = *I;
|
|
|
|
if (A->getType().getTypePtr()->isAnyPointerType()) {
|
|
|
|
SymbolRef Sym = State->getSVal(A, C.getLocationContext()).getAsSymbol();
|
|
|
|
if (!Sym)
|
2012-02-10 09:11:00 +08:00
|
|
|
continue;
|
2012-02-09 07:16:56 +08:00
|
|
|
checkEscape(Sym, A, C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-12 16:38:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MallocChecker::MallocMem(CheckerContext &C, const CallExpr *CE) {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef 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) {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef 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;
|
|
|
|
}
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef 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
|
|
|
}
|
|
|
|
|
2012-02-09 04:13:28 +08:00
|
|
|
ProgramStateRef 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,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state) {
|
2010-12-02 15:49:45 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
2009-12-11 11:09:01 +08:00
|
|
|
|
2012-02-09 04:13:28 +08:00
|
|
|
// Get the return value.
|
|
|
|
SVal retVal = state->getSVal(CE, C.getLocationContext());
|
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.
|
2012-02-10 09:11:00 +08:00
|
|
|
const SymbolicRegion *R =
|
|
|
|
dyn_cast_or_null<SymbolicRegion>(retVal.getAsRegion());
|
|
|
|
if (!R || !isa<DefinedOrUnknownSVal>(Size))
|
|
|
|
return 0;
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
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 {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef 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) {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state =
|
2012-01-05 07:48:37 +08:00
|
|
|
FreeMemAux(C, CE, C.getState(), *I,
|
|
|
|
Att->getOwnKind() == OwnershipAttr::Holds);
|
2010-08-19 07:23:40 +08:00
|
|
|
if (state)
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C,
|
2012-02-10 09:11:00 +08:00
|
|
|
const CallExpr *CE,
|
|
|
|
ProgramStateRef state,
|
|
|
|
unsigned Num,
|
|
|
|
bool Hold) const {
|
2010-07-31 09:52:11 +08:00
|
|
|
const Expr *ArgExpr = CE->getArg(Num);
|
2012-01-07 06:09:28 +08:00
|
|
|
SVal ArgVal = state->getSVal(ArgExpr, C.getLocationContext());
|
2012-02-10 09:11:00 +08:00
|
|
|
if (!isa<DefinedOrUnknownSVal>(ArgVal))
|
|
|
|
return 0;
|
2010-07-31 09:52:11 +08:00
|
|
|
DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(ArgVal);
|
|
|
|
|
|
|
|
// Check for null dereferences.
|
|
|
|
if (!isa<Loc>(location))
|
2012-02-09 04:13:28 +08:00
|
|
|
return 0;
|
2010-07-31 09:52:11 +08:00
|
|
|
|
2012-02-14 08:26:13 +08:00
|
|
|
// The explicit NULL case, no operation is performed.
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef 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
|
|
|
if (nullState && !notNullState)
|
2012-02-09 04:13:28 +08:00
|
|
|
return 0;
|
2010-07-31 09:52:11 +08:00
|
|
|
|
2010-06-08 03:32:37 +08:00
|
|
|
// Unknown values could easily be okay
|
|
|
|
// Undefined values are handled elsewhere
|
|
|
|
if (ArgVal.isUnknownOrUndef())
|
2012-02-09 04:13:28 +08:00
|
|
|
return 0;
|
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());
|
2012-02-09 04:13:28 +08:00
|
|
|
return 0;
|
2010-06-08 03:32:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2012-02-09 04:13:28 +08:00
|
|
|
return 0;
|
2010-06-08 03:32:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2012-02-09 04:13:28 +08:00
|
|
|
return 0;
|
2010-06-08 03:32:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2012-02-09 04:13:28 +08:00
|
|
|
return 0;
|
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)
|
2012-02-09 04:13:28 +08:00
|
|
|
return 0;
|
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
|
|
|
BugReport *R = new BugReport(*BT_DoubleFree,
|
2009-11-14 20:08:24 +08:00
|
|
|
BT_DoubleFree->getDescription(), N);
|
2012-02-09 14:25:51 +08:00
|
|
|
R->addVisitor(new MallocBugVisitor(Sym));
|
2009-11-12 16:38:56 +08:00
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
2012-02-09 04:13:28 +08:00
|
|
|
return 0;
|
2009-11-12 16:38:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Normal free.
|
2010-07-31 09:52:11 +08:00
|
|
|
if (Hold)
|
2012-02-14 08:26:13 +08:00
|
|
|
return state->set<RegionState>(Sym, RefState::getRelinquished(CE));
|
|
|
|
return state->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();
|
|
|
|
|
2012-01-05 07:54:01 +08:00
|
|
|
if (isa<StackLocalsSpaceRegion>(MS)) {
|
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)
|
|
|
|
os << "the address of the local variable '" << VD->getName() << "'";
|
|
|
|
else
|
|
|
|
os << "the address of a local stack variable";
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-05 07:54:01 +08:00
|
|
|
|
|
|
|
if (isa<StackArgumentsSpaceRegion>(MS)) {
|
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)
|
|
|
|
os << "the address of the parameter '" << VD->getName() << "'";
|
|
|
|
else
|
|
|
|
os << "the address of a parameter";
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-05 07:54:01 +08:00
|
|
|
|
|
|
|
if (isa<GlobalsSpaceRegion>(MS)) {
|
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;
|
|
|
|
}
|
2012-01-05 07:54:01 +08:00
|
|
|
|
|
|
|
return false;
|
2010-06-08 03:32:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<100> buf;
|
2010-06-08 03:32:37 +08:00
|
|
|
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 {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2010-12-02 15:49:45 +08:00
|
|
|
const Expr *arg0Expr = CE->getArg(0);
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
2012-02-10 09:11:00 +08:00
|
|
|
SVal Arg0Val = state->getSVal(arg0Expr, LCtx);
|
|
|
|
if (!isa<DefinedOrUnknownSVal>(Arg0Val))
|
|
|
|
return;
|
|
|
|
DefinedOrUnknownSVal arg0Val = cast<DefinedOrUnknownSVal>(Arg0Val);
|
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.
|
2012-02-10 09:11:00 +08:00
|
|
|
SVal Arg1ValG = state->getSVal(Arg1, LCtx);
|
|
|
|
if (!isa<DefinedOrUnknownSVal>(Arg1ValG))
|
|
|
|
return;
|
|
|
|
DefinedOrUnknownSVal Arg1Val = cast<DefinedOrUnknownSVal>(Arg1ValG);
|
2011-04-27 22:49:29 +08:00
|
|
|
|
|
|
|
// Compare the size argument to 0.
|
|
|
|
DefinedOrUnknownSVal SizeZero =
|
|
|
|
svalBuilder.evalEQ(state, Arg1Val,
|
|
|
|
svalBuilder.makeIntValWithPtrWidth(0, false));
|
|
|
|
|
2012-02-14 02:05:39 +08:00
|
|
|
ProgramStateRef StatePtrIsNull, StatePtrNotNull;
|
|
|
|
llvm::tie(StatePtrIsNull, StatePtrNotNull) = state->assume(PtrEQ);
|
|
|
|
ProgramStateRef StateSizeIsZero, StateSizeNotZero;
|
|
|
|
llvm::tie(StateSizeIsZero, StateSizeNotZero) = state->assume(SizeZero);
|
|
|
|
// We only assume exceptional states if they are definitely true; if the
|
|
|
|
// state is under-constrained, assume regular realloc behavior.
|
|
|
|
bool PrtIsNull = StatePtrIsNull && !StatePtrNotNull;
|
|
|
|
bool SizeIsZero = StateSizeIsZero && !StateSizeNotZero;
|
|
|
|
|
2011-04-27 22:49:29 +08:00
|
|
|
// If the ptr is NULL and the size is not 0, the call is equivalent to
|
|
|
|
// malloc(size).
|
2012-02-14 02:05:39 +08:00
|
|
|
if ( PrtIsNull && !SizeIsZero) {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef stateMalloc = MallocMemAux(C, CE, CE->getArg(1),
|
2012-02-14 02:05:39 +08:00
|
|
|
UndefinedVal(), StatePtrIsNull);
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(stateMalloc);
|
2012-02-14 02:05:39 +08:00
|
|
|
return;
|
2009-12-12 20:29:38 +08:00
|
|
|
}
|
|
|
|
|
2012-02-14 02:05:39 +08:00
|
|
|
if (PrtIsNull && SizeIsZero)
|
|
|
|
return;
|
2011-04-27 22:49:29 +08:00
|
|
|
|
2012-02-14 04:57:07 +08:00
|
|
|
// Get the from and to pointer symbols as in toPtr = realloc(fromPtr, size).
|
2012-02-14 02:05:39 +08:00
|
|
|
assert(!PrtIsNull);
|
2012-02-14 04:57:07 +08:00
|
|
|
SymbolRef FromPtr = arg0Val.getAsSymbol();
|
|
|
|
SVal RetVal = state->getSVal(CE, LCtx);
|
|
|
|
SymbolRef ToPtr = RetVal.getAsSymbol();
|
|
|
|
if (!FromPtr || !ToPtr)
|
|
|
|
return;
|
2012-02-14 02:05:39 +08:00
|
|
|
|
|
|
|
// If the size is 0, free the memory.
|
|
|
|
if (SizeIsZero)
|
|
|
|
if (ProgramStateRef stateFree = FreeMemAux(C, CE, StateSizeIsZero,0,false)){
|
|
|
|
// The semantics of the return value are:
|
|
|
|
// If size was equal to 0, either NULL or a pointer suitable to be passed
|
|
|
|
// to free() is returned.
|
2012-02-14 04:57:07 +08:00
|
|
|
stateFree = stateFree->set<ReallocPairs>(ToPtr, FromPtr);
|
2012-02-14 08:26:13 +08:00
|
|
|
C.getSymbolManager().addSymbolDependency(ToPtr, FromPtr);
|
2012-02-14 04:57:07 +08:00
|
|
|
C.addTransition(stateFree);
|
2012-02-14 02:05:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default behavior.
|
|
|
|
if (ProgramStateRef stateFree = FreeMemAux(C, CE, state, 0, false)) {
|
|
|
|
// FIXME: We should copy the content of the original buffer.
|
|
|
|
ProgramStateRef stateRealloc = MallocMemAux(C, CE, CE->getArg(1),
|
|
|
|
UnknownVal(), stateFree);
|
2012-02-14 04:57:07 +08:00
|
|
|
if (!stateRealloc)
|
2012-02-14 02:05:39 +08:00
|
|
|
return;
|
|
|
|
stateRealloc = stateRealloc->set<ReallocPairs>(ToPtr, FromPtr);
|
2012-02-14 08:26:13 +08:00
|
|
|
C.getSymbolManager().addSymbolDependency(ToPtr, FromPtr);
|
2012-02-14 02:05:39 +08:00
|
|
|
C.addTransition(stateRealloc);
|
|
|
|
return;
|
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) {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2010-12-02 05:28:31 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
|
|
|
SVal count = state->getSVal(CE->getArg(0), LCtx);
|
|
|
|
SVal elementSize = state->getSVal(CE->getArg(1), LCtx);
|
2010-12-02 15:49:45 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-02-12 05:02:40 +08:00
|
|
|
void MallocChecker::reportLeak(SymbolRef Sym, ExplodedNode *N,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
assert(N);
|
|
|
|
if (!BT_Leak) {
|
|
|
|
BT_Leak.reset(new BuiltinBug("Memory leak",
|
|
|
|
"Allocated memory never released. Potential memory leak."));
|
|
|
|
// Leaks should not be reported if they are post-dominated by a sink:
|
|
|
|
// (1) Sinks are higher importance bugs.
|
|
|
|
// (2) NoReturnFunctionChecker uses sink nodes to represent paths ending
|
|
|
|
// with __noreturn functions such as assert() or exit(). We choose not
|
|
|
|
// to report leaks on such paths.
|
|
|
|
BT_Leak->setSuppressOnSink(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
|
|
|
|
R->addVisitor(new MallocBugVisitor(Sym));
|
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef 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;
|
2012-02-09 14:48:19 +08:00
|
|
|
llvm::SmallVector<SymbolRef, 2> Errors;
|
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)) {
|
2012-02-09 14:48:19 +08:00
|
|
|
if (I->second.isAllocated()) {
|
2011-07-29 07:07:51 +08:00
|
|
|
generateReport = true;
|
2012-02-09 14:48:19 +08:00
|
|
|
Errors.push_back(I->first);
|
|
|
|
}
|
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
|
|
|
|
2012-02-14 02:05:39 +08:00
|
|
|
// Cleanup the Realloc Pairs Map.
|
|
|
|
SymRefToSymRefTy RP = state->get<ReallocPairs>();
|
|
|
|
for (SymRefToSymRefTy::iterator I = RP.begin(), E = RP.end(); I != E; ++I) {
|
|
|
|
if (SymReaper.isDead(I->first) || SymReaper.isDead(I->second)) {
|
|
|
|
state = state->remove<ReallocPairs>(I->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-27 05:06:34 +08:00
|
|
|
ExplodedNode *N = C.addTransition(state->set<RegionState>(RS));
|
2011-07-29 07:07:51 +08:00
|
|
|
|
|
|
|
if (N && generateReport) {
|
2012-02-09 14:48:19 +08:00
|
|
|
for (llvm::SmallVector<SymbolRef, 2>::iterator
|
2012-02-12 05:02:40 +08:00
|
|
|
I = Errors.begin(), E = Errors.end(); I != E; ++I) {
|
|
|
|
reportLeak(*I, N, C);
|
2012-02-09 14:48:19 +08:00
|
|
|
}
|
2011-07-29 07:07:51 +08:00
|
|
|
}
|
2009-11-13 15:25:27 +08:00
|
|
|
}
|
2009-11-17 15:54:15 +08:00
|
|
|
|
2012-02-12 05:02:40 +08:00
|
|
|
void MallocChecker::checkEndPath(CheckerContext &C) const {
|
|
|
|
ProgramStateRef state = C.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()) {
|
2012-02-12 05:02:40 +08:00
|
|
|
ExplodedNode *N = C.addTransition(state);
|
|
|
|
if (N)
|
|
|
|
reportLeak(I->first, N, C);
|
2009-11-17 15:54:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-17 16:58:18 +08:00
|
|
|
|
2012-02-09 07:16:56 +08:00
|
|
|
bool MallocChecker::checkEscape(SymbolRef Sym, const Stmt *S,
|
|
|
|
CheckerContext &C) const {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2009-11-17 16:58:18 +08:00
|
|
|
const RefState *RS = state->get<RegionState>(Sym);
|
|
|
|
if (!RS)
|
2012-02-09 07:16:56 +08:00
|
|
|
return false;
|
2009-11-17 16:58:18 +08:00
|
|
|
|
2012-02-09 07:16:56 +08:00
|
|
|
if (RS->isAllocated()) {
|
2009-11-17 16:58:18 +08:00
|
|
|
state = state->set<RegionState>(Sym, RefState::getEscaped(S));
|
2012-02-09 07:16:56 +08:00
|
|
|
C.addTransition(state);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-17 16:58:18 +08:00
|
|
|
|
2012-02-09 07:16:56 +08:00
|
|
|
void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
|
|
|
|
const Expr *E = S->getRetValue();
|
|
|
|
if (!E)
|
|
|
|
return;
|
2012-02-12 05:44:39 +08:00
|
|
|
|
|
|
|
// Check if we are returning a symbol.
|
2012-02-09 07:16:56 +08:00
|
|
|
SymbolRef Sym = C.getState()->getSVal(E, C.getLocationContext()).getAsSymbol();
|
|
|
|
if (!Sym)
|
|
|
|
return;
|
|
|
|
|
2012-02-12 05:44:39 +08:00
|
|
|
// Check if we are returning freed memory.
|
2012-02-12 07:46:36 +08:00
|
|
|
if (checkUseAfterFree(Sym, C, S))
|
|
|
|
return;
|
2012-02-12 05:44:39 +08:00
|
|
|
|
|
|
|
// Check if the symbol is escaping.
|
2012-02-09 07:16:56 +08:00
|
|
|
checkEscape(Sym, S, C);
|
2009-11-17 16:58:18 +08:00
|
|
|
}
|
2009-12-31 14:13:07 +08:00
|
|
|
|
2012-02-09 07:16:56 +08:00
|
|
|
bool MallocChecker::checkUseAfterFree(SymbolRef Sym, CheckerContext &C,
|
|
|
|
const Stmt *S) const {
|
|
|
|
assert(Sym);
|
|
|
|
const RefState *RS = C.getState()->get<RegionState>(Sym);
|
|
|
|
if (RS && RS->isReleased()) {
|
2012-02-12 07:46:36 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
2012-02-09 07:16:56 +08:00
|
|
|
if (!BT_UseFree)
|
2012-02-10 09:11:00 +08:00
|
|
|
BT_UseFree.reset(new BuiltinBug("Use of dynamically allocated memory "
|
2012-02-09 07:16:56 +08:00
|
|
|
"after it is freed."));
|
|
|
|
|
|
|
|
BugReport *R = new BugReport(*BT_UseFree, BT_UseFree->getDescription(),N);
|
|
|
|
if (S)
|
|
|
|
R->addRange(S->getSourceRange());
|
2012-02-09 14:25:51 +08:00
|
|
|
R->addVisitor(new MallocBugVisitor(Sym));
|
2012-02-09 07:16:56 +08:00
|
|
|
C.EmitReport(R);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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();
|
2012-02-09 07:16:56 +08:00
|
|
|
if (Sym)
|
|
|
|
checkUseAfterFree(Sym, C);
|
2010-03-10 12:58:55 +08:00
|
|
|
}
|
2010-07-31 09:52:11 +08:00
|
|
|
|
2012-02-12 05:02:35 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Check various ways a symbol can be invalidated.
|
|
|
|
// TODO: This logic (the next 3 functions) is copied/similar to the
|
|
|
|
// RetainRelease checker. We might want to factor this out.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2010-07-31 09:52:11 +08:00
|
|
|
|
2012-02-12 05:02:35 +08:00
|
|
|
// Stop tracking symbols when a value escapes as a result of checkBind.
|
|
|
|
// A value escapes in three possible cases:
|
|
|
|
// (1) we are binding to something that is not a memory region.
|
|
|
|
// (2) we are binding to a memregion that does not have stack storage
|
|
|
|
// (3) we are binding to a memregion with stack storage that the store
|
|
|
|
// does not understand.
|
|
|
|
void MallocChecker::checkBind(SVal loc, SVal val, const Stmt *S,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
// Are we storing to something that causes the value to "escape"?
|
|
|
|
bool escapes = true;
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2010-07-31 09:52:11 +08:00
|
|
|
|
2012-02-12 05:02:35 +08:00
|
|
|
if (loc::MemRegionVal *regionLoc = dyn_cast<loc::MemRegionVal>(&loc)) {
|
|
|
|
escapes = !regionLoc->getRegion()->hasStackStorage();
|
2010-07-31 09:52:11 +08:00
|
|
|
|
2012-02-12 05:02:35 +08:00
|
|
|
if (!escapes) {
|
|
|
|
// To test (3), generate a new state with the binding added. If it is
|
|
|
|
// the same state, then it escapes (since the store cannot represent
|
|
|
|
// the binding).
|
|
|
|
escapes = (state == (state->bindLoc(*regionLoc, val)));
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
|
|
|
}
|
2012-02-12 05:02:35 +08:00
|
|
|
|
|
|
|
// If our store can represent the binding and we aren't storing to something
|
|
|
|
// that doesn't have local storage then just return and have the simulation
|
|
|
|
// state continue as is.
|
|
|
|
if (!escapes)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Otherwise, find all symbols referenced by 'val' that we are tracking
|
|
|
|
// and stop tracking them.
|
|
|
|
state = state->scanReachableSymbols<StopTrackingCallback>(val).getState();
|
|
|
|
C.addTransition(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a symbolic region is assumed to NULL (or another constant), stop tracking
|
|
|
|
// it - assuming that allocation failed on this path.
|
|
|
|
ProgramStateRef MallocChecker::evalAssume(ProgramStateRef state,
|
|
|
|
SVal Cond,
|
|
|
|
bool Assumption) const {
|
|
|
|
RegionStateTy RS = state->get<RegionState>();
|
|
|
|
for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
|
|
|
|
// If the symbol is assumed to NULL or another constant, this will
|
|
|
|
// return an APSInt*.
|
|
|
|
if (state->getSymVal(I.getKey()))
|
|
|
|
state = state->remove<RegionState>(I.getKey());
|
|
|
|
}
|
|
|
|
|
2012-02-14 02:05:39 +08:00
|
|
|
// Realloc returns 0 when reallocation fails, which means that we should
|
|
|
|
// restore the state of the pointer being reallocated.
|
|
|
|
SymRefToSymRefTy RP = state->get<ReallocPairs>();
|
|
|
|
for (SymRefToSymRefTy::iterator I = RP.begin(), E = RP.end(); I != E; ++I) {
|
|
|
|
// If the symbol is assumed to NULL or another constant, this will
|
|
|
|
// return an APSInt*.
|
|
|
|
if (state->getSymVal(I.getKey())) {
|
|
|
|
const RefState *RS = state->get<RegionState>(I.getData());
|
|
|
|
if (RS) {
|
|
|
|
if (RS->isReleased())
|
|
|
|
state = state->set<RegionState>(I.getData(),
|
|
|
|
RefState::getAllocateUnchecked(RS->getStmt()));
|
2012-02-14 08:26:13 +08:00
|
|
|
else if (RS->isAllocated())
|
2012-02-14 02:05:39 +08:00
|
|
|
state = state->set<RegionState>(I.getData(),
|
|
|
|
RefState::getReleased(RS->getStmt()));
|
|
|
|
}
|
|
|
|
state = state->remove<ReallocPairs>(I.getKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-12 05:02:35 +08:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the symbol we are tracking is invalidated, but not explicitly (ex: the &p
|
|
|
|
// escapes, when we are tracking p), do not track the symbol as we cannot reason
|
|
|
|
// about it anymore.
|
|
|
|
ProgramStateRef
|
|
|
|
MallocChecker::checkRegionChanges(ProgramStateRef state,
|
|
|
|
const StoreManager::InvalidatedSymbols *invalidated,
|
|
|
|
ArrayRef<const MemRegion *> ExplicitRegions,
|
|
|
|
ArrayRef<const MemRegion *> Regions) const {
|
|
|
|
if (!invalidated)
|
|
|
|
return state;
|
|
|
|
|
|
|
|
llvm::SmallPtrSet<SymbolRef, 8> WhitelistedSymbols;
|
|
|
|
for (ArrayRef<const MemRegion *>::iterator I = ExplicitRegions.begin(),
|
|
|
|
E = ExplicitRegions.end(); I != E; ++I) {
|
|
|
|
if (const SymbolicRegion *SR = (*I)->StripCasts()->getAs<SymbolicRegion>())
|
|
|
|
WhitelistedSymbols.insert(SR->getSymbol());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (StoreManager::InvalidatedSymbols::const_iterator I=invalidated->begin(),
|
|
|
|
E = invalidated->end(); I!=E; ++I) {
|
|
|
|
SymbolRef sym = *I;
|
|
|
|
if (WhitelistedSymbols.count(sym))
|
|
|
|
continue;
|
|
|
|
// Don't track the symbol.
|
|
|
|
state = state->remove<RegionState>(sym);
|
|
|
|
}
|
|
|
|
return state;
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
2011-02-28 09:26:35 +08:00
|
|
|
|
2012-02-09 14:25:51 +08:00
|
|
|
PathDiagnosticPiece *
|
|
|
|
MallocChecker::MallocBugVisitor::VisitNode(const ExplodedNode *N,
|
|
|
|
const ExplodedNode *PrevN,
|
|
|
|
BugReporterContext &BRC,
|
|
|
|
BugReport &BR) {
|
|
|
|
const RefState *RS = N->getState()->get<RegionState>(Sym);
|
|
|
|
const RefState *RSPrev = PrevN->getState()->get<RegionState>(Sym);
|
|
|
|
if (!RS && !RSPrev)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// We expect the interesting locations be StmtPoints corresponding to call
|
|
|
|
// expressions. We do not support indirect function calls as of now.
|
|
|
|
const CallExpr *CE = 0;
|
|
|
|
if (isa<StmtPoint>(N->getLocation()))
|
|
|
|
CE = dyn_cast<CallExpr>(cast<StmtPoint>(N->getLocation()).getStmt());
|
|
|
|
if (!CE)
|
|
|
|
return 0;
|
|
|
|
const FunctionDecl *funDecl = CE->getDirectCallee();
|
|
|
|
if (!funDecl)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Find out if this is an interesting point and what is the kind.
|
|
|
|
const char *Msg = 0;
|
|
|
|
if (isAllocated(RS, RSPrev))
|
|
|
|
Msg = "Memory is allocated here";
|
|
|
|
else if (isReleased(RS, RSPrev))
|
|
|
|
Msg = "Memory is released here";
|
|
|
|
if (!Msg)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Generate the extra diagnostic.
|
|
|
|
PathDiagnosticLocation Pos(CE, BRC.getSourceManager(),
|
|
|
|
N->getLocationContext());
|
|
|
|
return new PathDiagnosticEventPiece(Pos, Msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-09 07:16:52 +08:00
|
|
|
#define REGISTER_CHECKER(name) \
|
|
|
|
void ento::register##name(CheckerManager &mgr) {\
|
|
|
|
mgr.registerChecker<MallocChecker>()->Filter.C##name = true;\
|
2011-02-28 09:26:35 +08:00
|
|
|
}
|
2012-02-09 07:16:52 +08:00
|
|
|
|
|
|
|
REGISTER_CHECKER(MallocPessimistic)
|
|
|
|
REGISTER_CHECKER(MallocOptimistic)
|