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"
|
2012-02-18 06:35:31 +08:00
|
|
|
#include "InterCheckerAPI.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.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"
|
2012-07-27 05:39:41 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.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"
|
2012-02-04 20:31:12 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-01 23:09:41 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-09-22 09:24:42 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2012-02-22 11:14:20 +08:00
|
|
|
#include <climits>
|
|
|
|
|
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 {
|
|
|
|
|
2013-03-29 01:05:19 +08:00
|
|
|
// Used to check correspondence between allocators and deallocators.
|
|
|
|
enum AllocationFamily {
|
|
|
|
AF_None,
|
|
|
|
AF_Malloc,
|
|
|
|
AF_CXXNew,
|
|
|
|
AF_CXXNewArray
|
|
|
|
};
|
|
|
|
|
2009-12-11 08:55:44 +08:00
|
|
|
class RefState {
|
2012-06-21 04:57:46 +08:00
|
|
|
enum Kind { // Reference to allocated memory.
|
|
|
|
Allocated,
|
|
|
|
// Reference to released/freed memory.
|
|
|
|
Released,
|
2013-12-02 11:50:25 +08:00
|
|
|
// The responsibility for freeing resources has transferred from
|
2012-06-21 04:57:46 +08:00
|
|
|
// this reference. A relinquished symbol should not be freed.
|
2013-04-09 08:30:28 +08:00
|
|
|
Relinquished,
|
|
|
|
// We are no longer guaranteed to have observed all manipulations
|
|
|
|
// of this pointer/memory. For example, it could have been
|
|
|
|
// passed as a parameter to an opaque function.
|
|
|
|
Escaped
|
|
|
|
};
|
2013-03-29 01:05:19 +08:00
|
|
|
|
2009-11-17 15:54:15 +08:00
|
|
|
const Stmt *S;
|
2013-03-29 01:05:19 +08:00
|
|
|
unsigned K : 2; // Kind enum, but stored as a bitfield.
|
|
|
|
unsigned Family : 30; // Rest of 32-bit word, currently just an allocation
|
|
|
|
// family.
|
2009-11-17 15:54:15 +08:00
|
|
|
|
2013-03-29 01:05:19 +08:00
|
|
|
RefState(Kind k, const Stmt *s, unsigned family)
|
2013-04-09 08:30:28 +08:00
|
|
|
: S(s), K(k), Family(family) {
|
|
|
|
assert(family != AF_None);
|
|
|
|
}
|
2009-12-11 08:55:44 +08:00
|
|
|
public:
|
2012-06-21 04:57:46 +08:00
|
|
|
bool isAllocated() const { return K == Allocated; }
|
2009-11-17 15:54:15 +08:00
|
|
|
bool isReleased() const { return K == Released; }
|
2012-06-21 04:57:46 +08:00
|
|
|
bool isRelinquished() const { return K == Relinquished; }
|
2013-04-09 08:30:28 +08:00
|
|
|
bool isEscaped() const { return K == Escaped; }
|
|
|
|
AllocationFamily getAllocationFamily() const {
|
2013-03-29 01:05:19 +08:00
|
|
|
return (AllocationFamily)Family;
|
|
|
|
}
|
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 {
|
2013-03-29 01:05:19 +08:00
|
|
|
return K == X.K && S == X.S && Family == X.Family;
|
2009-11-17 15:54:15 +08:00
|
|
|
}
|
|
|
|
|
2013-03-29 01:05:19 +08:00
|
|
|
static RefState getAllocated(unsigned family, const Stmt *s) {
|
|
|
|
return RefState(Allocated, s, family);
|
2009-12-31 14:13:07 +08:00
|
|
|
}
|
2013-03-29 01:05:19 +08:00
|
|
|
static RefState getReleased(unsigned family, const Stmt *s) {
|
|
|
|
return RefState(Released, s, family);
|
|
|
|
}
|
|
|
|
static RefState getRelinquished(unsigned family, const Stmt *s) {
|
|
|
|
return RefState(Relinquished, s, family);
|
2010-08-07 05:12:55 +08:00
|
|
|
}
|
2013-04-09 08:30:28 +08:00
|
|
|
static RefState getEscaped(const RefState *RS) {
|
|
|
|
return RefState(Escaped, RS->getStmt(), RS->getAllocationFamily());
|
|
|
|
}
|
2009-11-17 15:54:15 +08:00
|
|
|
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
ID.AddInteger(K);
|
|
|
|
ID.AddPointer(S);
|
2013-03-29 01:05:19 +08:00
|
|
|
ID.AddInteger(Family);
|
2009-11-17 15:54:15 +08:00
|
|
|
}
|
2013-01-03 09:30:12 +08:00
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
void dump(raw_ostream &OS) const {
|
2013-07-15 16:24:27 +08:00
|
|
|
static const char *const Table[] = {
|
2013-01-03 09:30:12 +08:00
|
|
|
"Allocated",
|
|
|
|
"Released",
|
|
|
|
"Relinquished"
|
|
|
|
};
|
|
|
|
OS << Table[(unsigned) K];
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_ATTRIBUTE_USED void dump() const {
|
|
|
|
dump(llvm::errs());
|
|
|
|
}
|
2009-11-12 16:38:56 +08:00
|
|
|
};
|
|
|
|
|
2012-09-13 06:57:34 +08:00
|
|
|
enum ReallocPairKind {
|
|
|
|
RPToBeFreedAfterFailure,
|
|
|
|
// The symbol has been freed when reallocation failed.
|
|
|
|
RPIsFreeOnFailure,
|
|
|
|
// The symbol does not need to be freed after reallocation fails.
|
|
|
|
RPDoNotTrackAfterFailure
|
|
|
|
};
|
|
|
|
|
2012-08-24 10:28:20 +08:00
|
|
|
/// \class ReallocPair
|
|
|
|
/// \brief Stores information about the symbol being reallocated by a call to
|
|
|
|
/// 'realloc' to allow modeling failed reallocation later in the path.
|
2012-02-15 08:11:25 +08:00
|
|
|
struct ReallocPair {
|
2012-08-24 10:28:20 +08:00
|
|
|
// \brief The symbol which realloc reallocated.
|
2012-02-15 08:11:25 +08:00
|
|
|
SymbolRef ReallocatedSym;
|
2012-09-13 06:57:34 +08:00
|
|
|
ReallocPairKind Kind;
|
2012-08-24 10:28:20 +08:00
|
|
|
|
2012-09-13 06:57:34 +08:00
|
|
|
ReallocPair(SymbolRef S, ReallocPairKind K) :
|
|
|
|
ReallocatedSym(S), Kind(K) {}
|
2012-02-15 08:11:25 +08:00
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const {
|
2012-09-13 06:57:34 +08:00
|
|
|
ID.AddInteger(Kind);
|
2012-02-15 08:11:25 +08:00
|
|
|
ID.AddPointer(ReallocatedSym);
|
|
|
|
}
|
|
|
|
bool operator==(const ReallocPair &X) const {
|
|
|
|
return ReallocatedSym == X.ReallocatedSym &&
|
2012-09-13 06:57:34 +08:00
|
|
|
Kind == X.Kind;
|
2012-02-15 08:11:25 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-08 08:25:29 +08:00
|
|
|
typedef std::pair<const ExplodedNode*, const MemRegion*> LeakInfo;
|
2012-03-22 03:45:08 +08:00
|
|
|
|
2012-02-09 04:13:28 +08:00
|
|
|
class MallocChecker : public Checker<check::DeadSymbols,
|
2012-12-20 08:38:25 +08:00
|
|
|
check::PointerEscape,
|
2013-03-29 07:15:29 +08:00
|
|
|
check::ConstPointerEscape,
|
2012-01-05 07:48:37 +08:00
|
|
|
check::PreStmt<ReturnStmt>,
|
2013-04-11 06:21:41 +08:00
|
|
|
check::PreCall,
|
2012-02-09 04:13:28 +08:00
|
|
|
check::PostStmt<CallExpr>,
|
2013-03-25 09:35:45 +08:00
|
|
|
check::PostStmt<CXXNewExpr>,
|
|
|
|
check::PreStmt<CXXDeleteExpr>,
|
2012-03-22 08:57:20 +08:00
|
|
|
check::PostStmt<BlockExpr>,
|
2012-11-13 11:18:01 +08:00
|
|
|
check::PostObjCMessage,
|
2012-01-05 07:48:37 +08:00
|
|
|
check::Location,
|
2012-12-20 08:38:25 +08:00
|
|
|
eval::Assume>
|
2012-01-05 07:48:37 +08:00
|
|
|
{
|
2012-02-17 06:26:12 +08:00
|
|
|
mutable OwningPtr<BugType> BT_DoubleFree;
|
|
|
|
mutable OwningPtr<BugType> BT_Leak;
|
|
|
|
mutable OwningPtr<BugType> BT_UseFree;
|
|
|
|
mutable OwningPtr<BugType> BT_BadFree;
|
2013-04-05 07:46:29 +08:00
|
|
|
mutable OwningPtr<BugType> BT_MismatchedDealloc;
|
2013-02-08 07:05:47 +08:00
|
|
|
mutable OwningPtr<BugType> BT_OffsetFree;
|
2012-02-15 08:11:22 +08:00
|
|
|
mutable IdentifierInfo *II_malloc, *II_free, *II_realloc, *II_calloc,
|
2012-02-22 11:14:20 +08:00
|
|
|
*II_valloc, *II_reallocf, *II_strndup, *II_strdup;
|
|
|
|
|
2009-11-12 16:38:56 +08:00
|
|
|
public:
|
2012-02-15 08:11:22 +08:00
|
|
|
MallocChecker() : II_malloc(0), II_free(0), II_realloc(0), II_calloc(0),
|
2012-02-22 11:14:20 +08:00
|
|
|
II_valloc(0), II_reallocf(0), II_strndup(0), II_strdup(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;
|
2013-03-25 09:35:45 +08:00
|
|
|
DefaultBool CNewDeleteChecker;
|
2013-04-06 01:55:00 +08:00
|
|
|
DefaultBool CNewDeleteLeaksChecker;
|
2013-03-29 01:05:19 +08:00
|
|
|
DefaultBool CMismatchedDeallocatorChecker;
|
2012-02-09 07:16:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ChecksFilter Filter;
|
|
|
|
|
2013-04-11 06:21:41 +08:00
|
|
|
void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
|
2012-02-09 04:13:28 +08:00
|
|
|
void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
|
2013-03-25 09:35:45 +08:00
|
|
|
void checkPostStmt(const CXXNewExpr *NE, CheckerContext &C) const;
|
|
|
|
void checkPreStmt(const CXXDeleteExpr *DE, CheckerContext &C) const;
|
2012-11-13 11:18:01 +08:00
|
|
|
void checkPostObjCMessage(const ObjCMethodCall &Call, CheckerContext &C) const;
|
2012-03-22 08:57:20 +08:00
|
|
|
void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
|
2011-02-28 09:26:35 +08:00
|
|
|
void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
|
|
|
|
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;
|
2012-12-20 08:38:25 +08:00
|
|
|
|
|
|
|
ProgramStateRef checkPointerEscape(ProgramStateRef State,
|
|
|
|
const InvalidatedSymbols &Escaped,
|
2013-02-08 07:05:43 +08:00
|
|
|
const CallEvent *Call,
|
|
|
|
PointerEscapeKind Kind) const;
|
2013-03-29 07:15:29 +08:00
|
|
|
ProgramStateRef checkConstPointerEscape(ProgramStateRef State,
|
|
|
|
const InvalidatedSymbols &Escaped,
|
|
|
|
const CallEvent *Call,
|
|
|
|
PointerEscapeKind Kind) const;
|
2009-12-31 14:13:07 +08:00
|
|
|
|
2012-05-02 08:05:20 +08:00
|
|
|
void printState(raw_ostream &Out, ProgramStateRef State,
|
|
|
|
const char *NL, const char *Sep) const;
|
|
|
|
|
2009-11-13 15:25:27 +08:00
|
|
|
private:
|
2012-02-15 05:55:24 +08:00
|
|
|
void initIdentifierInfo(ASTContext &C) const;
|
|
|
|
|
2013-03-29 01:05:19 +08:00
|
|
|
/// \brief Determine family of a deallocation expression.
|
2013-04-05 07:46:29 +08:00
|
|
|
AllocationFamily getAllocationFamily(CheckerContext &C, const Stmt *S) const;
|
2013-03-29 01:05:19 +08:00
|
|
|
|
|
|
|
/// \brief Print names of allocators and deallocators.
|
|
|
|
///
|
|
|
|
/// \returns true on success.
|
|
|
|
bool printAllocDeallocName(raw_ostream &os, CheckerContext &C,
|
|
|
|
const Expr *E) const;
|
|
|
|
|
|
|
|
/// \brief Print expected name of an allocator based on the deallocator's
|
|
|
|
/// family derived from the DeallocExpr.
|
|
|
|
void printExpectedAllocName(raw_ostream &os, CheckerContext &C,
|
|
|
|
const Expr *DeallocExpr) const;
|
|
|
|
/// \brief Print expected name of a deallocator based on the allocator's
|
|
|
|
/// family.
|
|
|
|
void printExpectedDeallocName(raw_ostream &os, AllocationFamily Family) const;
|
|
|
|
|
2013-03-09 08:59:10 +08:00
|
|
|
///@{
|
2012-02-15 05:55:24 +08:00
|
|
|
/// Check if this is one of the functions which can allocate/reallocate memory
|
|
|
|
/// pointed to by one of its arguments.
|
|
|
|
bool isMemFunction(const FunctionDecl *FD, ASTContext &C) const;
|
2012-05-18 09:16:10 +08:00
|
|
|
bool isFreeFunction(const FunctionDecl *FD, ASTContext &C) const;
|
|
|
|
bool isAllocationFunction(const FunctionDecl *FD, ASTContext &C) const;
|
2013-03-25 09:35:45 +08:00
|
|
|
bool isStandardNewDelete(const FunctionDecl *FD, ASTContext &C) const;
|
2013-03-09 08:59:10 +08:00
|
|
|
///@}
|
2013-11-27 09:46:48 +08:00
|
|
|
ProgramStateRef MallocMemReturnsAttr(CheckerContext &C,
|
|
|
|
const CallExpr *CE,
|
|
|
|
const OwnershipAttr* Att) const;
|
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,
|
2013-03-29 01:05:19 +08:00
|
|
|
ProgramStateRef State,
|
|
|
|
AllocationFamily Family = AF_Malloc) {
|
2012-01-07 06:09:28 +08:00
|
|
|
return MallocMemAux(C, CE,
|
2013-03-29 01:05:19 +08:00
|
|
|
State->getSVal(SizeEx, C.getLocationContext()),
|
|
|
|
Init, State, Family);
|
2010-06-01 11:01:33 +08:00
|
|
|
}
|
2012-02-23 03:24:52 +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,
|
2013-03-29 01:05:19 +08:00
|
|
|
ProgramStateRef State,
|
|
|
|
AllocationFamily Family = AF_Malloc);
|
2010-06-01 11:01:33 +08:00
|
|
|
|
2012-02-23 03:24:52 +08:00
|
|
|
/// Update the RefState to reflect the new memory allocation.
|
2013-03-29 01:05:19 +08:00
|
|
|
static ProgramStateRef
|
|
|
|
MallocUpdateRefState(CheckerContext &C, const Expr *E, ProgramStateRef State,
|
|
|
|
AllocationFamily Family = AF_Malloc);
|
2012-02-23 03:24:52 +08:00
|
|
|
|
|
|
|
ProgramStateRef FreeMemAttr(CheckerContext &C, const CallExpr *CE,
|
|
|
|
const OwnershipAttr* Att) const;
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef FreeMemAux(CheckerContext &C, const CallExpr *CE,
|
2012-06-22 10:04:31 +08:00
|
|
|
ProgramStateRef state, unsigned Num,
|
2012-08-24 10:28:20 +08:00
|
|
|
bool Hold,
|
2012-11-13 11:18:01 +08:00
|
|
|
bool &ReleasedAllocated,
|
|
|
|
bool ReturnsNullOnFailure = false) const;
|
2012-06-22 10:04:31 +08:00
|
|
|
ProgramStateRef FreeMemAux(CheckerContext &C, const Expr *Arg,
|
|
|
|
const Expr *ParentExpr,
|
2012-11-13 11:18:01 +08:00
|
|
|
ProgramStateRef State,
|
2012-08-24 10:28:20 +08:00
|
|
|
bool Hold,
|
2012-11-13 11:18:01 +08:00
|
|
|
bool &ReleasedAllocated,
|
|
|
|
bool ReturnsNullOnFailure = false) const;
|
2009-12-12 20:29:38 +08:00
|
|
|
|
2012-02-23 03:24:52 +08:00
|
|
|
ProgramStateRef ReallocMem(CheckerContext &C, const CallExpr *CE,
|
|
|
|
bool FreesMemOnFailure) const;
|
|
|
|
static ProgramStateRef CallocMem(CheckerContext &C, const CallExpr *CE);
|
2010-06-08 03:32:37 +08:00
|
|
|
|
2012-05-18 09:16:10 +08:00
|
|
|
///\brief Check if the memory associated with this symbol was released.
|
|
|
|
bool isReleased(SymbolRef Sym, CheckerContext &C) const;
|
|
|
|
|
2013-03-25 09:35:45 +08:00
|
|
|
bool checkUseAfterFree(SymbolRef Sym, CheckerContext &C, const Stmt *S) const;
|
2012-02-09 07:16:56 +08:00
|
|
|
|
2013-06-01 07:47:32 +08:00
|
|
|
/// Check if the function is known free memory, or if it is
|
2013-03-09 08:59:10 +08:00
|
|
|
/// "interesting" and should be modeled explicitly.
|
|
|
|
///
|
2013-06-08 08:29:29 +08:00
|
|
|
/// \param [out] EscapingSymbol A function might not free memory in general,
|
|
|
|
/// but could be known to free a particular symbol. In this case, false is
|
2013-06-01 07:47:32 +08:00
|
|
|
/// returned and the single escaping symbol is returned through the out
|
|
|
|
/// parameter.
|
|
|
|
///
|
2013-03-09 08:59:10 +08:00
|
|
|
/// We assume that pointers do not escape through calls to system functions
|
|
|
|
/// not handled by this checker.
|
2013-06-08 08:29:29 +08:00
|
|
|
bool mayFreeAnyEscapedMemoryOrIsModeledExplicitly(const CallEvent *Call,
|
2013-06-01 07:47:32 +08:00
|
|
|
ProgramStateRef State,
|
|
|
|
SymbolRef &EscapingSymbol) const;
|
2012-02-15 05:55:24 +08:00
|
|
|
|
2013-03-29 07:15:29 +08:00
|
|
|
// Implementation of the checkPointerEscape callabcks.
|
|
|
|
ProgramStateRef checkPointerEscapeAux(ProgramStateRef State,
|
|
|
|
const InvalidatedSymbols &Escaped,
|
|
|
|
const CallEvent *Call,
|
|
|
|
PointerEscapeKind Kind,
|
|
|
|
bool(*CheckRefState)(const RefState*)) const;
|
|
|
|
|
2013-04-11 08:05:20 +08:00
|
|
|
///@{
|
|
|
|
/// Tells if a given family/call/symbol is tracked by the current checker.
|
|
|
|
bool isTrackedByCurrentChecker(AllocationFamily Family) const;
|
|
|
|
bool isTrackedByCurrentChecker(CheckerContext &C,
|
|
|
|
const Stmt *AllocDeallocStmt) const;
|
|
|
|
bool isTrackedByCurrentChecker(CheckerContext &C, SymbolRef Sym) 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);
|
2013-03-29 01:05:19 +08:00
|
|
|
void ReportBadFree(CheckerContext &C, SVal ArgVal, SourceRange Range,
|
|
|
|
const Expr *DeallocExpr) const;
|
2013-04-05 07:46:29 +08:00
|
|
|
void ReportMismatchedDealloc(CheckerContext &C, SourceRange Range,
|
2013-04-05 19:25:10 +08:00
|
|
|
const Expr *DeallocExpr, const RefState *RS,
|
2013-09-17 01:51:25 +08:00
|
|
|
SymbolRef Sym, bool OwnershipTransferred) const;
|
2013-03-29 01:05:19 +08:00
|
|
|
void ReportOffsetFree(CheckerContext &C, SVal ArgVal, SourceRange Range,
|
|
|
|
const Expr *DeallocExpr,
|
|
|
|
const Expr *AllocExpr = 0) const;
|
2013-03-13 22:39:10 +08:00
|
|
|
void ReportUseAfterFree(CheckerContext &C, SourceRange Range,
|
|
|
|
SymbolRef Sym) const;
|
|
|
|
void ReportDoubleFree(CheckerContext &C, SourceRange Range, bool Released,
|
2013-03-14 01:07:32 +08:00
|
|
|
SymbolRef Sym, SymbolRef PrevSym) const;
|
2012-02-09 14:25:51 +08:00
|
|
|
|
2012-02-24 05:38:21 +08:00
|
|
|
/// Find the location of the allocation for Sym on the path leading to the
|
|
|
|
/// exploded node N.
|
2012-03-22 03:45:08 +08:00
|
|
|
LeakInfo getAllocationSite(const ExplodedNode *N, SymbolRef Sym,
|
|
|
|
CheckerContext &C) const;
|
2012-02-24 05:38:21 +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.
|
2012-03-24 10:45:35 +08:00
|
|
|
class MallocBugVisitor : public BugReporterVisitorImpl<MallocBugVisitor> {
|
2012-02-09 14:25:51 +08:00
|
|
|
protected:
|
2012-02-17 06:26:07 +08:00
|
|
|
enum NotificationMode {
|
|
|
|
Normal,
|
|
|
|
ReallocationFailed
|
|
|
|
};
|
|
|
|
|
2012-02-09 14:25:51 +08:00
|
|
|
// The allocated region symbol tracked by the main analysis.
|
|
|
|
SymbolRef Sym;
|
|
|
|
|
2012-05-10 09:37:40 +08:00
|
|
|
// The mode we are in, i.e. what kind of diagnostics will be emitted.
|
|
|
|
NotificationMode Mode;
|
2012-03-24 11:15:09 +08:00
|
|
|
|
2012-05-10 09:37:40 +08:00
|
|
|
// A symbol from when the primary region should have been reallocated.
|
|
|
|
SymbolRef FailedReallocSymbol;
|
2012-03-24 11:15:09 +08:00
|
|
|
|
2012-05-10 09:37:40 +08:00
|
|
|
bool IsLeak;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MallocBugVisitor(SymbolRef S, bool isLeak = false)
|
|
|
|
: Sym(S), Mode(Normal), FailedReallocSymbol(0), IsLeak(isLeak) {}
|
2012-03-24 11:15:09 +08:00
|
|
|
|
2012-02-09 14:25:51 +08:00
|
|
|
virtual ~MallocBugVisitor() {}
|
|
|
|
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
static int X = 0;
|
|
|
|
ID.AddPointer(&X);
|
|
|
|
ID.AddPointer(Sym);
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:26:07 +08:00
|
|
|
inline bool isAllocated(const RefState *S, const RefState *SPrev,
|
|
|
|
const Stmt *Stmt) {
|
2012-02-09 14:25:51 +08:00
|
|
|
// Did not track -> allocated. Other state (released) -> allocated.
|
2013-03-25 09:35:45 +08:00
|
|
|
return (Stmt && (isa<CallExpr>(Stmt) || isa<CXXNewExpr>(Stmt)) &&
|
2012-02-17 06:26:07 +08:00
|
|
|
(S && S->isAllocated()) && (!SPrev || !SPrev->isAllocated()));
|
2012-02-09 14:25:51 +08:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:26:07 +08:00
|
|
|
inline bool isReleased(const RefState *S, const RefState *SPrev,
|
|
|
|
const Stmt *Stmt) {
|
2012-02-09 14:25:51 +08:00
|
|
|
// Did not track -> released. Other state (allocated) -> released.
|
2013-03-25 09:35:45 +08:00
|
|
|
return (Stmt && (isa<CallExpr>(Stmt) || isa<CXXDeleteExpr>(Stmt)) &&
|
2012-02-17 06:26:07 +08:00
|
|
|
(S && S->isReleased()) && (!SPrev || !SPrev->isReleased()));
|
|
|
|
}
|
|
|
|
|
2012-06-22 10:04:31 +08:00
|
|
|
inline bool isRelinquished(const RefState *S, const RefState *SPrev,
|
|
|
|
const Stmt *Stmt) {
|
|
|
|
// Did not track -> relinquished. Other state (allocated) -> relinquished.
|
|
|
|
return (Stmt && (isa<CallExpr>(Stmt) || isa<ObjCMessageExpr>(Stmt) ||
|
|
|
|
isa<ObjCPropertyRefExpr>(Stmt)) &&
|
|
|
|
(S && S->isRelinquished()) &&
|
|
|
|
(!SPrev || !SPrev->isRelinquished()));
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:26:07 +08:00
|
|
|
inline bool isReallocFailedCheck(const RefState *S, const RefState *SPrev,
|
|
|
|
const Stmt *Stmt) {
|
|
|
|
// If the expression is not a call, and the state change is
|
|
|
|
// released -> allocated, it must be the realloc return value
|
|
|
|
// check. If we have to handle more cases here, it might be cleaner just
|
|
|
|
// to track this extra bit in the state itself.
|
|
|
|
return ((!Stmt || !isa<CallExpr>(Stmt)) &&
|
|
|
|
(S && S->isAllocated()) && (SPrev && !SPrev->isAllocated()));
|
2012-02-09 14:25:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
|
|
|
|
const ExplodedNode *PrevN,
|
|
|
|
BugReporterContext &BRC,
|
|
|
|
BugReport &BR);
|
2012-05-10 09:37:40 +08:00
|
|
|
|
|
|
|
PathDiagnosticPiece* getEndPath(BugReporterContext &BRC,
|
|
|
|
const ExplodedNode *EndPathNode,
|
|
|
|
BugReport &BR) {
|
|
|
|
if (!IsLeak)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
PathDiagnosticLocation L =
|
|
|
|
PathDiagnosticLocation::createEndOfPath(EndPathNode,
|
|
|
|
BRC.getSourceManager());
|
|
|
|
// Do not add the statement itself as a range in case of leak.
|
|
|
|
return new PathDiagnosticEventPiece(L, BR.getDescription(), false);
|
|
|
|
}
|
|
|
|
|
2012-03-17 07:24:20 +08:00
|
|
|
private:
|
|
|
|
class StackHintGeneratorForReallocationFailed
|
|
|
|
: public StackHintGeneratorForSymbol {
|
|
|
|
public:
|
|
|
|
StackHintGeneratorForReallocationFailed(SymbolRef S, StringRef M)
|
|
|
|
: StackHintGeneratorForSymbol(S, M) {}
|
|
|
|
|
|
|
|
virtual std::string getMessageForArg(const Expr *ArgE, unsigned ArgIndex) {
|
2012-09-22 09:24:42 +08:00
|
|
|
// Printed parameters start at 1, not 0.
|
|
|
|
++ArgIndex;
|
|
|
|
|
2012-03-17 07:24:20 +08:00
|
|
|
SmallString<200> buf;
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
|
2012-09-22 09:24:42 +08:00
|
|
|
os << "Reallocation of " << ArgIndex << llvm::getOrdinalSuffix(ArgIndex)
|
|
|
|
<< " parameter failed";
|
2012-03-17 07:24:20 +08:00
|
|
|
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual std::string getMessageForReturn(const CallExpr *CallExpr) {
|
2012-03-17 07:44:28 +08:00
|
|
|
return "Reallocation of returned value failed";
|
2012-03-17 07:24:20 +08:00
|
|
|
}
|
|
|
|
};
|
2012-02-09 14:25:51 +08:00
|
|
|
};
|
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
|
|
|
|
2012-11-02 09:54:06 +08:00
|
|
|
REGISTER_MAP_WITH_PROGRAMSTATE(RegionState, SymbolRef, RefState)
|
|
|
|
REGISTER_MAP_WITH_PROGRAMSTATE(ReallocPairs, SymbolRef, ReallocPair)
|
2009-11-12 16:38:56 +08:00
|
|
|
|
2012-11-13 11:18:01 +08:00
|
|
|
// A map from the freed symbol to the symbol representing the return value of
|
|
|
|
// the free function.
|
|
|
|
REGISTER_MAP_WITH_PROGRAMSTATE(FreeReturnValue, SymbolRef, SymbolRef)
|
|
|
|
|
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-15 05:55:24 +08:00
|
|
|
void MallocChecker::initIdentifierInfo(ASTContext &Ctx) const {
|
2012-05-19 06:47:40 +08:00
|
|
|
if (II_malloc)
|
|
|
|
return;
|
|
|
|
II_malloc = &Ctx.Idents.get("malloc");
|
|
|
|
II_free = &Ctx.Idents.get("free");
|
|
|
|
II_realloc = &Ctx.Idents.get("realloc");
|
|
|
|
II_reallocf = &Ctx.Idents.get("reallocf");
|
|
|
|
II_calloc = &Ctx.Idents.get("calloc");
|
|
|
|
II_valloc = &Ctx.Idents.get("valloc");
|
|
|
|
II_strdup = &Ctx.Idents.get("strdup");
|
|
|
|
II_strndup = &Ctx.Idents.get("strndup");
|
2012-02-09 04:13:28 +08:00
|
|
|
}
|
|
|
|
|
2012-02-15 05:55:24 +08:00
|
|
|
bool MallocChecker::isMemFunction(const FunctionDecl *FD, ASTContext &C) const {
|
2012-05-18 09:16:10 +08:00
|
|
|
if (isFreeFunction(FD, C))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (isAllocationFunction(FD, C))
|
|
|
|
return true;
|
|
|
|
|
2013-03-25 09:35:45 +08:00
|
|
|
if (isStandardNewDelete(FD, C))
|
|
|
|
return true;
|
|
|
|
|
2012-05-18 09:16:10 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MallocChecker::isAllocationFunction(const FunctionDecl *FD,
|
|
|
|
ASTContext &C) const {
|
2012-02-15 10:12:00 +08:00
|
|
|
if (!FD)
|
|
|
|
return false;
|
2012-05-18 09:16:10 +08:00
|
|
|
|
2012-07-11 07:13:01 +08:00
|
|
|
if (FD->getKind() == Decl::Function) {
|
|
|
|
IdentifierInfo *FunI = FD->getIdentifier();
|
|
|
|
initIdentifierInfo(C);
|
|
|
|
|
|
|
|
if (FunI == II_malloc || FunI == II_realloc ||
|
|
|
|
FunI == II_reallocf || FunI == II_calloc || FunI == II_valloc ||
|
|
|
|
FunI == II_strdup || FunI == II_strndup)
|
|
|
|
return true;
|
|
|
|
}
|
2012-02-15 05:55:24 +08:00
|
|
|
|
2012-05-18 09:16:10 +08:00
|
|
|
if (Filter.CMallocOptimistic && FD->hasAttrs())
|
|
|
|
for (specific_attr_iterator<OwnershipAttr>
|
|
|
|
i = FD->specific_attr_begin<OwnershipAttr>(),
|
|
|
|
e = FD->specific_attr_end<OwnershipAttr>();
|
|
|
|
i != e; ++i)
|
|
|
|
if ((*i)->getOwnKind() == OwnershipAttr::Returns)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MallocChecker::isFreeFunction(const FunctionDecl *FD, ASTContext &C) const {
|
|
|
|
if (!FD)
|
|
|
|
return false;
|
|
|
|
|
2012-07-11 07:13:01 +08:00
|
|
|
if (FD->getKind() == Decl::Function) {
|
|
|
|
IdentifierInfo *FunI = FD->getIdentifier();
|
|
|
|
initIdentifierInfo(C);
|
2012-02-15 05:55:24 +08:00
|
|
|
|
2012-07-11 07:13:01 +08:00
|
|
|
if (FunI == II_free || FunI == II_realloc || FunI == II_reallocf)
|
|
|
|
return true;
|
|
|
|
}
|
2012-02-15 05:55:24 +08:00
|
|
|
|
2012-05-18 09:16:10 +08:00
|
|
|
if (Filter.CMallocOptimistic && FD->hasAttrs())
|
|
|
|
for (specific_attr_iterator<OwnershipAttr>
|
|
|
|
i = FD->specific_attr_begin<OwnershipAttr>(),
|
|
|
|
e = FD->specific_attr_end<OwnershipAttr>();
|
|
|
|
i != e; ++i)
|
|
|
|
if ((*i)->getOwnKind() == OwnershipAttr::Takes ||
|
|
|
|
(*i)->getOwnKind() == OwnershipAttr::Holds)
|
|
|
|
return true;
|
2012-02-15 05:55:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-29 00:10:38 +08:00
|
|
|
// Tells if the callee is one of the following:
|
|
|
|
// 1) A global non-placement new/delete operator function.
|
|
|
|
// 2) A global placement operator function with the single placement argument
|
|
|
|
// of type std::nothrow_t.
|
2013-03-25 09:35:45 +08:00
|
|
|
bool MallocChecker::isStandardNewDelete(const FunctionDecl *FD,
|
|
|
|
ASTContext &C) const {
|
|
|
|
if (!FD)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
OverloadedOperatorKind Kind = FD->getOverloadedOperator();
|
|
|
|
if (Kind != OO_New && Kind != OO_Array_New &&
|
|
|
|
Kind != OO_Delete && Kind != OO_Array_Delete)
|
|
|
|
return false;
|
|
|
|
|
2013-03-29 00:10:38 +08:00
|
|
|
// Skip all operator new/delete methods.
|
|
|
|
if (isa<CXXMethodDecl>(FD))
|
2013-03-25 09:35:45 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Return true if tested operator is a standard placement nothrow operator.
|
|
|
|
if (FD->getNumParams() == 2) {
|
|
|
|
QualType T = FD->getParamDecl(1)->getType();
|
|
|
|
if (const IdentifierInfo *II = T.getBaseTypeIdentifier())
|
|
|
|
return II->getName().equals("nothrow_t");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip placement operators.
|
|
|
|
if (FD->getNumParams() != 1 || FD->isVariadic())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// One of the standard new/new[]/delete/delete[] non-placement operators.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-09 04:13:28 +08:00
|
|
|
void MallocChecker::checkPostStmt(const CallExpr *CE, CheckerContext &C) const {
|
2012-09-20 09:55:32 +08:00
|
|
|
if (C.wasInlined)
|
|
|
|
return;
|
|
|
|
|
2012-02-09 04:13:28 +08:00
|
|
|
const FunctionDecl *FD = C.getCalleeDecl(CE);
|
|
|
|
if (!FD)
|
|
|
|
return;
|
2012-02-15 08:11:22 +08:00
|
|
|
|
2012-02-23 03:24:52 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
2012-08-24 10:28:20 +08:00
|
|
|
bool ReleasedAllocatedMemory = false;
|
2012-07-11 07:13:01 +08:00
|
|
|
|
|
|
|
if (FD->getKind() == Decl::Function) {
|
|
|
|
initIdentifierInfo(C.getASTContext());
|
|
|
|
IdentifierInfo *FunI = FD->getIdentifier();
|
|
|
|
|
2013-04-05 07:46:29 +08:00
|
|
|
if (FunI == II_malloc || FunI == II_valloc) {
|
|
|
|
if (CE->getNumArgs() < 1)
|
|
|
|
return;
|
|
|
|
State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State);
|
|
|
|
} else if (FunI == II_realloc) {
|
|
|
|
State = ReallocMem(C, CE, false);
|
|
|
|
} else if (FunI == II_reallocf) {
|
|
|
|
State = ReallocMem(C, CE, true);
|
|
|
|
} else if (FunI == II_calloc) {
|
|
|
|
State = CallocMem(C, CE);
|
|
|
|
} else if (FunI == II_free) {
|
|
|
|
State = FreeMemAux(C, CE, State, 0, false, ReleasedAllocatedMemory);
|
|
|
|
} else if (FunI == II_strdup) {
|
|
|
|
State = MallocUpdateRefState(C, CE, State);
|
|
|
|
} else if (FunI == II_strndup) {
|
|
|
|
State = MallocUpdateRefState(C, CE, State);
|
2013-03-25 09:35:45 +08:00
|
|
|
}
|
2013-04-05 07:46:29 +08:00
|
|
|
else if (isStandardNewDelete(FD, C.getASTContext())) {
|
|
|
|
// Process direct calls to operator new/new[]/delete/delete[] functions
|
|
|
|
// as distinct from new/new[]/delete/delete[] expressions that are
|
|
|
|
// processed by the checkPostStmt callbacks for CXXNewExpr and
|
|
|
|
// CXXDeleteExpr.
|
|
|
|
OverloadedOperatorKind K = FD->getOverloadedOperator();
|
|
|
|
if (K == OO_New)
|
|
|
|
State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State,
|
|
|
|
AF_CXXNew);
|
|
|
|
else if (K == OO_Array_New)
|
|
|
|
State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State,
|
|
|
|
AF_CXXNewArray);
|
|
|
|
else if (K == OO_Delete || K == OO_Array_Delete)
|
|
|
|
State = FreeMemAux(C, CE, State, 0, false, ReleasedAllocatedMemory);
|
|
|
|
else
|
|
|
|
llvm_unreachable("not a new/delete operator");
|
2012-07-11 07:13:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-29 01:05:19 +08:00
|
|
|
if (Filter.CMallocOptimistic || Filter.CMismatchedDeallocatorChecker) {
|
2012-02-23 03:24:52 +08:00
|
|
|
// Check all the attributes, if there are any.
|
|
|
|
// There can be multiple of these attributes.
|
|
|
|
if (FD->hasAttrs())
|
|
|
|
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:
|
|
|
|
State = MallocMemReturnsAttr(C, CE, *i);
|
|
|
|
break;
|
|
|
|
case OwnershipAttr::Takes:
|
|
|
|
case OwnershipAttr::Holds:
|
|
|
|
State = FreeMemAttr(C, CE, *i);
|
|
|
|
break;
|
|
|
|
}
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
|
|
|
}
|
2012-02-22 11:14:20 +08:00
|
|
|
C.addTransition(State);
|
2009-12-12 20:29:38 +08:00
|
|
|
}
|
|
|
|
|
2013-03-25 09:35:45 +08:00
|
|
|
void MallocChecker::checkPostStmt(const CXXNewExpr *NE,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
|
|
|
|
if (NE->getNumPlacementArgs())
|
|
|
|
for (CXXNewExpr::const_arg_iterator I = NE->placement_arg_begin(),
|
|
|
|
E = NE->placement_arg_end(); I != E; ++I)
|
|
|
|
if (SymbolRef Sym = C.getSVal(*I).getAsSymbol())
|
|
|
|
checkUseAfterFree(Sym, C, *I);
|
|
|
|
|
|
|
|
if (!isStandardNewDelete(NE->getOperatorNew(), C.getASTContext()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
// The return value from operator new is bound to a specified initialization
|
|
|
|
// value (if any) and we don't want to loose this value. So we call
|
|
|
|
// MallocUpdateRefState() instead of MallocMemAux() which breakes the
|
|
|
|
// existing binding.
|
2013-03-29 01:05:19 +08:00
|
|
|
State = MallocUpdateRefState(C, NE, State, NE->isArray() ? AF_CXXNewArray
|
|
|
|
: AF_CXXNew);
|
2013-03-25 09:35:45 +08:00
|
|
|
C.addTransition(State);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MallocChecker::checkPreStmt(const CXXDeleteExpr *DE,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
|
2013-03-29 01:05:19 +08:00
|
|
|
if (!Filter.CNewDeleteChecker)
|
2013-03-25 09:35:45 +08:00
|
|
|
if (SymbolRef Sym = C.getSVal(DE->getArgument()).getAsSymbol())
|
|
|
|
checkUseAfterFree(Sym, C, DE->getArgument());
|
|
|
|
|
|
|
|
if (!isStandardNewDelete(DE->getOperatorDelete(), C.getASTContext()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
bool ReleasedAllocated;
|
|
|
|
State = FreeMemAux(C, DE->getArgument(), DE, State,
|
|
|
|
/*Hold*/false, ReleasedAllocated);
|
|
|
|
|
|
|
|
C.addTransition(State);
|
|
|
|
}
|
|
|
|
|
2013-03-09 08:59:10 +08:00
|
|
|
static bool isKnownDeallocObjCMethodName(const ObjCMethodCall &Call) {
|
|
|
|
// If the first selector piece is one of the names below, assume that the
|
|
|
|
// object takes ownership of the memory, promising to eventually deallocate it
|
|
|
|
// with free().
|
|
|
|
// Ex: [NSData dataWithBytesNoCopy:bytes length:10];
|
|
|
|
// (...unless a 'freeWhenDone' parameter is false, but that's checked later.)
|
|
|
|
StringRef FirstSlot = Call.getSelector().getNameForSlot(0);
|
|
|
|
if (FirstSlot == "dataWithBytesNoCopy" ||
|
|
|
|
FirstSlot == "initWithBytesNoCopy" ||
|
|
|
|
FirstSlot == "initWithCharactersNoCopy")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Optional<bool> getFreeWhenDoneArg(const ObjCMethodCall &Call) {
|
2012-07-03 03:27:56 +08:00
|
|
|
Selector S = Call.getSelector();
|
2013-03-09 08:59:10 +08:00
|
|
|
|
|
|
|
// FIXME: We should not rely on fully-constrained symbols being folded.
|
2012-06-23 06:08:09 +08:00
|
|
|
for (unsigned i = 1; i < S.getNumArgs(); ++i)
|
2012-06-22 10:04:31 +08:00
|
|
|
if (S.getNameForSlot(i).equals("freeWhenDone"))
|
2013-03-09 08:59:10 +08:00
|
|
|
return !Call.getArgSVal(i).isZeroConstant();
|
2012-06-22 10:04:31 +08:00
|
|
|
|
2013-03-09 08:59:10 +08:00
|
|
|
return None;
|
2012-06-22 10:04:31 +08:00
|
|
|
}
|
|
|
|
|
2012-11-13 11:18:01 +08:00
|
|
|
void MallocChecker::checkPostObjCMessage(const ObjCMethodCall &Call,
|
|
|
|
CheckerContext &C) const {
|
2012-12-11 08:17:53 +08:00
|
|
|
if (C.wasInlined)
|
|
|
|
return;
|
|
|
|
|
2013-03-09 08:59:10 +08:00
|
|
|
if (!isKnownDeallocObjCMethodName(Call))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Optional<bool> FreeWhenDone = getFreeWhenDoneArg(Call))
|
|
|
|
if (!*FreeWhenDone)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool ReleasedAllocatedMemory;
|
|
|
|
ProgramStateRef State = FreeMemAux(C, Call.getArgExpr(0),
|
|
|
|
Call.getOriginExpr(), C.getState(),
|
|
|
|
/*Hold=*/true, ReleasedAllocatedMemory,
|
|
|
|
/*RetNullOnFailure=*/true);
|
|
|
|
|
|
|
|
C.addTransition(State);
|
2012-06-22 10:04:31 +08:00
|
|
|
}
|
|
|
|
|
2013-11-27 09:46:48 +08:00
|
|
|
ProgramStateRef
|
|
|
|
MallocChecker::MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,
|
|
|
|
const OwnershipAttr *Att) const {
|
|
|
|
if (Att->getModule() != II_malloc)
|
2012-02-23 03:24:52 +08:00
|
|
|
return 0;
|
2010-07-31 09:52:11 +08:00
|
|
|
|
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-02-23 03:24:52 +08:00
|
|
|
return MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), C.getState());
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
2012-02-23 03:24:52 +08:00
|
|
|
return MallocMemAux(C, CE, UnknownVal(), UndefinedVal(), C.getState());
|
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,
|
2013-03-29 01:05:19 +08:00
|
|
|
ProgramStateRef State,
|
|
|
|
AllocationFamily Family) {
|
2012-06-07 11:57:32 +08:00
|
|
|
|
|
|
|
// Bind the return value to the symbolic value from the heap region.
|
|
|
|
// TODO: We could rewrite post visit to eval call; 'malloc' does not have
|
|
|
|
// side effects other than what we model here.
|
2012-08-22 14:26:15 +08:00
|
|
|
unsigned Count = C.blockCount();
|
2012-06-07 11:57:32 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
|
2013-02-20 13:52:05 +08:00
|
|
|
DefinedSVal RetVal = svalBuilder.getConjuredHeapSymbolVal(CE, LCtx, Count)
|
|
|
|
.castAs<DefinedSVal>();
|
2013-03-29 01:05:19 +08:00
|
|
|
State = State->BindExpr(CE, C.getLocationContext(), RetVal);
|
2009-12-11 11:09:01 +08:00
|
|
|
|
2012-02-15 08:11:22 +08:00
|
|
|
// We expect the malloc functions to return a pointer.
|
2013-02-20 13:52:05 +08:00
|
|
|
if (!RetVal.getAs<Loc>())
|
2012-02-15 08:11:22 +08:00
|
|
|
return 0;
|
|
|
|
|
2010-07-04 08:00:41 +08:00
|
|
|
// Fill the region with the initialization value.
|
2013-03-29 01:05:19 +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 =
|
2012-06-07 11:57:32 +08:00
|
|
|
dyn_cast_or_null<SymbolicRegion>(RetVal.getAsRegion());
|
2012-02-22 11:14:20 +08:00
|
|
|
if (!R)
|
2012-02-10 09:11:00 +08:00
|
|
|
return 0;
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<DefinedOrUnknownSVal> DefinedSize =
|
2013-02-20 13:52:05 +08:00
|
|
|
Size.getAs<DefinedOrUnknownSVal>()) {
|
2012-02-23 03:24:52 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
2012-02-22 11:14:20 +08:00
|
|
|
DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
|
|
|
|
DefinedOrUnknownSVal extentMatchesSize =
|
2013-03-29 01:05:19 +08:00
|
|
|
svalBuilder.evalEQ(State, Extent, *DefinedSize);
|
2012-02-22 11:14:20 +08:00
|
|
|
|
2013-03-29 01:05:19 +08:00
|
|
|
State = State->assume(extentMatchesSize, true);
|
|
|
|
assert(State);
|
2012-02-22 11:14:20 +08:00
|
|
|
}
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2013-03-29 01:05:19 +08:00
|
|
|
return MallocUpdateRefState(C, CE, State, Family);
|
2012-02-23 03:24:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ProgramStateRef MallocChecker::MallocUpdateRefState(CheckerContext &C,
|
2013-03-25 09:35:45 +08:00
|
|
|
const Expr *E,
|
2013-03-29 01:05:19 +08:00
|
|
|
ProgramStateRef State,
|
|
|
|
AllocationFamily Family) {
|
2012-02-23 03:24:52 +08:00
|
|
|
// Get the return value.
|
2013-03-29 01:05:19 +08:00
|
|
|
SVal retVal = State->getSVal(E, C.getLocationContext());
|
2012-02-23 03:24:52 +08:00
|
|
|
|
|
|
|
// We expect the malloc functions to return a pointer.
|
2013-02-20 13:52:05 +08:00
|
|
|
if (!retVal.getAs<Loc>())
|
2012-02-23 03:24:52 +08:00
|
|
|
return 0;
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
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.
|
2013-03-29 01:05:19 +08:00
|
|
|
return State->set<RegionState>(Sym, RefState::getAllocated(Family, E));
|
2009-12-12 20:29:38 +08:00
|
|
|
}
|
|
|
|
|
2012-02-23 03:24:52 +08:00
|
|
|
ProgramStateRef MallocChecker::FreeMemAttr(CheckerContext &C,
|
|
|
|
const CallExpr *CE,
|
2013-11-27 09:46:48 +08:00
|
|
|
const OwnershipAttr *Att) const {
|
|
|
|
if (Att->getModule() != II_malloc)
|
2012-02-23 03:24:52 +08:00
|
|
|
return 0;
|
2010-07-31 09:52:11 +08:00
|
|
|
|
2012-03-02 06:06:06 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
2012-08-24 10:28:20 +08:00
|
|
|
bool ReleasedAllocated = false;
|
2012-03-02 06:06:06 +08:00
|
|
|
|
2010-08-19 07:23:40 +08:00
|
|
|
for (OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
|
|
|
|
I != E; ++I) {
|
2012-03-02 06:06:06 +08:00
|
|
|
ProgramStateRef StateI = FreeMemAux(C, CE, State, *I,
|
2012-08-24 10:28:20 +08:00
|
|
|
Att->getOwnKind() == OwnershipAttr::Holds,
|
|
|
|
ReleasedAllocated);
|
2012-03-02 06:06:06 +08:00
|
|
|
if (StateI)
|
|
|
|
State = StateI;
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
2012-03-02 06:06:06 +08:00
|
|
|
return 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,
|
2012-08-24 10:28:20 +08:00
|
|
|
bool Hold,
|
2012-11-13 11:18:01 +08:00
|
|
|
bool &ReleasedAllocated,
|
|
|
|
bool ReturnsNullOnFailure) const {
|
2012-04-11 07:41:11 +08:00
|
|
|
if (CE->getNumArgs() < (Num + 1))
|
|
|
|
return 0;
|
|
|
|
|
2012-11-13 11:18:01 +08:00
|
|
|
return FreeMemAux(C, CE->getArg(Num), CE, state, Hold,
|
|
|
|
ReleasedAllocated, ReturnsNullOnFailure);
|
|
|
|
}
|
|
|
|
|
2012-11-14 03:47:40 +08:00
|
|
|
/// Checks if the previous call to free on the given symbol failed - if free
|
|
|
|
/// failed, returns true. Also, returns the corresponding return value symbol.
|
2012-11-22 23:02:44 +08:00
|
|
|
static bool didPreviousFreeFail(ProgramStateRef State,
|
|
|
|
SymbolRef Sym, SymbolRef &RetStatusSymbol) {
|
2012-11-14 03:47:40 +08:00
|
|
|
const SymbolRef *Ret = State->get<FreeReturnValue>(Sym);
|
2012-11-13 11:18:01 +08:00
|
|
|
if (Ret) {
|
|
|
|
assert(*Ret && "We should not store the null return symbol");
|
|
|
|
ConstraintManager &CMgr = State->getConstraintManager();
|
|
|
|
ConditionTruthVal FreeFailed = CMgr.isNull(State, *Ret);
|
2012-11-14 03:47:40 +08:00
|
|
|
RetStatusSymbol = *Ret;
|
|
|
|
return FreeFailed.isConstrainedTrue();
|
2012-11-13 11:18:01 +08:00
|
|
|
}
|
2012-11-14 03:47:40 +08:00
|
|
|
return false;
|
2012-06-22 10:04:31 +08:00
|
|
|
}
|
|
|
|
|
2013-03-29 01:05:19 +08:00
|
|
|
AllocationFamily MallocChecker::getAllocationFamily(CheckerContext &C,
|
2013-04-05 07:46:29 +08:00
|
|
|
const Stmt *S) const {
|
|
|
|
if (!S)
|
2013-03-29 01:05:19 +08:00
|
|
|
return AF_None;
|
|
|
|
|
2013-04-05 07:46:29 +08:00
|
|
|
if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
|
2013-03-29 01:05:19 +08:00
|
|
|
const FunctionDecl *FD = C.getCalleeDecl(CE);
|
2013-04-05 07:46:29 +08:00
|
|
|
|
|
|
|
if (!FD)
|
|
|
|
FD = dyn_cast<FunctionDecl>(CE->getCalleeDecl());
|
|
|
|
|
2013-03-29 01:05:19 +08:00
|
|
|
ASTContext &Ctx = C.getASTContext();
|
|
|
|
|
2013-04-05 07:46:29 +08:00
|
|
|
if (isAllocationFunction(FD, Ctx) || isFreeFunction(FD, Ctx))
|
2013-03-29 01:05:19 +08:00
|
|
|
return AF_Malloc;
|
|
|
|
|
|
|
|
if (isStandardNewDelete(FD, Ctx)) {
|
|
|
|
OverloadedOperatorKind Kind = FD->getOverloadedOperator();
|
2013-04-05 07:46:29 +08:00
|
|
|
if (Kind == OO_New || Kind == OO_Delete)
|
2013-03-29 01:05:19 +08:00
|
|
|
return AF_CXXNew;
|
2013-04-05 07:46:29 +08:00
|
|
|
else if (Kind == OO_Array_New || Kind == OO_Array_Delete)
|
2013-03-29 01:05:19 +08:00
|
|
|
return AF_CXXNewArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AF_None;
|
|
|
|
}
|
|
|
|
|
2013-04-05 07:46:29 +08:00
|
|
|
if (const CXXNewExpr *NE = dyn_cast<CXXNewExpr>(S))
|
|
|
|
return NE->isArray() ? AF_CXXNewArray : AF_CXXNew;
|
|
|
|
|
|
|
|
if (const CXXDeleteExpr *DE = dyn_cast<CXXDeleteExpr>(S))
|
2013-03-29 01:05:19 +08:00
|
|
|
return DE->isArrayForm() ? AF_CXXNewArray : AF_CXXNew;
|
|
|
|
|
2013-04-05 07:46:29 +08:00
|
|
|
if (isa<ObjCMessageExpr>(S))
|
2013-03-29 01:05:19 +08:00
|
|
|
return AF_Malloc;
|
|
|
|
|
|
|
|
return AF_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MallocChecker::printAllocDeallocName(raw_ostream &os, CheckerContext &C,
|
|
|
|
const Expr *E) const {
|
|
|
|
if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
|
|
|
|
// FIXME: This doesn't handle indirect calls.
|
|
|
|
const FunctionDecl *FD = CE->getDirectCallee();
|
|
|
|
if (!FD)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
os << *FD;
|
|
|
|
if (!FD->isOverloadedOperator())
|
|
|
|
os << "()";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) {
|
|
|
|
if (Msg->isInstanceMessage())
|
|
|
|
os << "-";
|
|
|
|
else
|
|
|
|
os << "+";
|
|
|
|
os << Msg->getSelector().getAsString();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const CXXNewExpr *NE = dyn_cast<CXXNewExpr>(E)) {
|
|
|
|
os << "'"
|
|
|
|
<< getOperatorSpelling(NE->getOperatorNew()->getOverloadedOperator())
|
|
|
|
<< "'";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const CXXDeleteExpr *DE = dyn_cast<CXXDeleteExpr>(E)) {
|
|
|
|
os << "'"
|
|
|
|
<< getOperatorSpelling(DE->getOperatorDelete()->getOverloadedOperator())
|
|
|
|
<< "'";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MallocChecker::printExpectedAllocName(raw_ostream &os, CheckerContext &C,
|
|
|
|
const Expr *E) const {
|
|
|
|
AllocationFamily Family = getAllocationFamily(C, E);
|
|
|
|
|
|
|
|
switch(Family) {
|
|
|
|
case AF_Malloc: os << "malloc()"; return;
|
|
|
|
case AF_CXXNew: os << "'new'"; return;
|
|
|
|
case AF_CXXNewArray: os << "'new[]'"; return;
|
|
|
|
case AF_None: llvm_unreachable("not a deallocation expression");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MallocChecker::printExpectedDeallocName(raw_ostream &os,
|
|
|
|
AllocationFamily Family) const {
|
|
|
|
switch(Family) {
|
|
|
|
case AF_Malloc: os << "free()"; return;
|
|
|
|
case AF_CXXNew: os << "'delete'"; return;
|
|
|
|
case AF_CXXNewArray: os << "'delete[]'"; return;
|
|
|
|
case AF_None: llvm_unreachable("suspicious AF_None argument");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-22 10:04:31 +08:00
|
|
|
ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C,
|
|
|
|
const Expr *ArgExpr,
|
|
|
|
const Expr *ParentExpr,
|
2012-11-13 11:18:01 +08:00
|
|
|
ProgramStateRef State,
|
2012-08-24 10:28:20 +08:00
|
|
|
bool Hold,
|
2012-11-13 11:18:01 +08:00
|
|
|
bool &ReleasedAllocated,
|
|
|
|
bool ReturnsNullOnFailure) const {
|
2012-06-22 10:04:31 +08:00
|
|
|
|
2012-11-13 11:18:01 +08:00
|
|
|
SVal ArgVal = State->getSVal(ArgExpr, C.getLocationContext());
|
2013-02-20 13:52:05 +08:00
|
|
|
if (!ArgVal.getAs<DefinedOrUnknownSVal>())
|
2012-02-10 09:11:00 +08:00
|
|
|
return 0;
|
2013-02-20 13:52:05 +08:00
|
|
|
DefinedOrUnknownSVal location = ArgVal.castAs<DefinedOrUnknownSVal>();
|
2010-07-31 09:52:11 +08:00
|
|
|
|
|
|
|
// Check for null dereferences.
|
2013-02-20 13:52:05 +08:00
|
|
|
if (!location.getAs<Loc>())
|
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;
|
2012-11-13 11:18:01 +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) {
|
2013-03-29 01:05:19 +08:00
|
|
|
ReportBadFree(C, ArgVal, ArgExpr->getSourceRange(), ParentExpr);
|
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)) {
|
2013-03-29 01:05:19 +08:00
|
|
|
ReportBadFree(C, ArgVal, ArgExpr->getSourceRange(), ParentExpr);
|
2012-02-09 04:13:28 +08:00
|
|
|
return 0;
|
2010-06-08 03:32:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const MemSpaceRegion *MS = R->getMemorySpace();
|
|
|
|
|
2013-03-13 22:39:10 +08:00
|
|
|
// Parameters, locals, statics, globals, and memory returned by alloca()
|
|
|
|
// shouldn't be freed.
|
2010-06-08 03:32:37 +08:00
|
|
|
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.
|
|
|
|
|
2013-03-29 01:05:19 +08:00
|
|
|
ReportBadFree(C, ArgVal, ArgExpr->getSourceRange(), ParentExpr);
|
2012-02-09 04:13:28 +08:00
|
|
|
return 0;
|
2010-06-08 03:32:37 +08:00
|
|
|
}
|
2013-02-08 07:05:47 +08:00
|
|
|
|
|
|
|
const SymbolicRegion *SrBase = dyn_cast<SymbolicRegion>(R->getBaseRegion());
|
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.
|
2013-02-08 07:05:47 +08:00
|
|
|
if (!SrBase)
|
2012-02-09 04:13:28 +08:00
|
|
|
return 0;
|
2009-11-12 16:38:56 +08:00
|
|
|
|
2013-02-08 07:05:47 +08:00
|
|
|
SymbolRef SymBase = SrBase->getSymbol();
|
|
|
|
const RefState *RsBase = State->get<RegionState>(SymBase);
|
2012-11-14 03:47:40 +08:00
|
|
|
SymbolRef PreviousRetStatusSymbol = 0;
|
2010-01-18 11:27:34 +08:00
|
|
|
|
2013-04-05 07:46:29 +08:00
|
|
|
if (RsBase) {
|
2009-11-12 16:38:56 +08:00
|
|
|
|
2013-04-09 08:30:28 +08:00
|
|
|
// Check for double free first.
|
|
|
|
if ((RsBase->isReleased() || RsBase->isRelinquished()) &&
|
2013-04-05 07:46:29 +08:00
|
|
|
!didPreviousFreeFail(State, SymBase, PreviousRetStatusSymbol)) {
|
|
|
|
ReportDoubleFree(C, ParentExpr->getSourceRange(), RsBase->isReleased(),
|
|
|
|
SymBase, PreviousRetStatusSymbol);
|
|
|
|
return 0;
|
|
|
|
|
2013-04-09 08:30:28 +08:00
|
|
|
// If the pointer is allocated or escaped, but we are now trying to free it,
|
|
|
|
// check that the call to free is proper.
|
|
|
|
} else if (RsBase->isAllocated() || RsBase->isEscaped()) {
|
|
|
|
|
|
|
|
// Check if an expected deallocation function matches the real one.
|
|
|
|
bool DeallocMatchesAlloc =
|
|
|
|
RsBase->getAllocationFamily() == getAllocationFamily(C, ParentExpr);
|
|
|
|
if (!DeallocMatchesAlloc) {
|
|
|
|
ReportMismatchedDealloc(C, ArgExpr->getSourceRange(),
|
2013-09-17 01:51:25 +08:00
|
|
|
ParentExpr, RsBase, SymBase, Hold);
|
2013-04-09 08:30:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the memory location being freed is the actual location
|
|
|
|
// allocated, or an offset.
|
|
|
|
RegionOffset Offset = R->getAsOffset();
|
|
|
|
if (Offset.isValid() &&
|
|
|
|
!Offset.hasSymbolicOffset() &&
|
|
|
|
Offset.getOffset() != 0) {
|
|
|
|
const Expr *AllocExpr = cast<Expr>(RsBase->getStmt());
|
|
|
|
ReportOffsetFree(C, ArgVal, ArgExpr->getSourceRange(), ParentExpr,
|
|
|
|
AllocExpr);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-04-05 07:46:29 +08:00
|
|
|
}
|
2013-02-08 07:05:47 +08:00
|
|
|
}
|
|
|
|
|
[analyzer] If realloc fails on an escaped region, that region doesn't leak.
When a region is realloc()ed, MallocChecker records whether it was known
to be allocated or not. If it is, and the reallocation fails, the original
region has to be freed. Previously, when an allocated region escaped,
MallocChecker completely stopped tracking it, so a failed reallocation
still (correctly) wouldn't require freeing the original region. Recently,
however, MallocChecker started tracking escaped symbols, so that if it were
freed we could check that the deallocator matched the allocator. This
broke the reallocation model for whether or not a symbol was allocated.
Now, MallocChecker will actually check if a symbol is owned, and only
require freeing after a failed reallocation if it was owned before.
PR16730
llvm-svn: 188468
2013-08-16 01:22:06 +08:00
|
|
|
ReleasedAllocated = (RsBase != 0) && RsBase->isAllocated();
|
2012-08-24 10:28:20 +08:00
|
|
|
|
2012-11-14 03:47:40 +08:00
|
|
|
// Clean out the info on previous call to free return info.
|
2013-02-08 07:05:47 +08:00
|
|
|
State = State->remove<FreeReturnValue>(SymBase);
|
2012-11-14 03:47:40 +08:00
|
|
|
|
2012-11-13 11:18:01 +08:00
|
|
|
// Keep track of the return value. If it is NULL, we will know that free
|
|
|
|
// failed.
|
|
|
|
if (ReturnsNullOnFailure) {
|
|
|
|
SVal RetVal = C.getSVal(ParentExpr);
|
|
|
|
SymbolRef RetStatusSymbol = RetVal.getAsSymbol();
|
|
|
|
if (RetStatusSymbol) {
|
2013-02-08 07:05:47 +08:00
|
|
|
C.getSymbolManager().addSymbolDependency(SymBase, RetStatusSymbol);
|
|
|
|
State = State->set<FreeReturnValue>(SymBase, RetStatusSymbol);
|
2012-11-13 11:18:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-06 03:08:04 +08:00
|
|
|
AllocationFamily Family = RsBase ? RsBase->getAllocationFamily()
|
|
|
|
: getAllocationFamily(C, ParentExpr);
|
2009-11-12 16:38:56 +08:00
|
|
|
// Normal free.
|
2013-03-29 01:05:19 +08:00
|
|
|
if (Hold)
|
2013-02-08 07:05:47 +08:00
|
|
|
return State->set<RegionState>(SymBase,
|
2013-03-29 01:05:19 +08:00
|
|
|
RefState::getRelinquished(Family,
|
|
|
|
ParentExpr));
|
|
|
|
|
|
|
|
return State->set<RegionState>(SymBase,
|
|
|
|
RefState::getReleased(Family, ParentExpr));
|
2009-12-12 20:29:38 +08:00
|
|
|
}
|
|
|
|
|
2013-04-11 08:05:20 +08:00
|
|
|
bool MallocChecker::isTrackedByCurrentChecker(AllocationFamily Family) const {
|
2013-04-05 08:31:02 +08:00
|
|
|
switch (Family) {
|
|
|
|
case AF_Malloc: {
|
|
|
|
if (!Filter.CMallocOptimistic && !Filter.CMallocPessimistic)
|
|
|
|
return false;
|
2013-04-05 10:12:04 +08:00
|
|
|
return true;
|
2013-04-05 08:31:02 +08:00
|
|
|
}
|
|
|
|
case AF_CXXNew:
|
|
|
|
case AF_CXXNewArray: {
|
2013-04-13 07:25:40 +08:00
|
|
|
if (!Filter.CNewDeleteChecker)
|
2013-04-05 08:31:02 +08:00
|
|
|
return false;
|
2013-04-05 10:12:04 +08:00
|
|
|
return true;
|
2013-04-05 08:31:02 +08:00
|
|
|
}
|
|
|
|
case AF_None: {
|
2013-04-06 03:08:04 +08:00
|
|
|
llvm_unreachable("no family");
|
2013-04-05 08:31:02 +08:00
|
|
|
}
|
|
|
|
}
|
2013-04-05 10:12:04 +08:00
|
|
|
llvm_unreachable("unhandled family");
|
2013-04-05 07:46:29 +08:00
|
|
|
}
|
|
|
|
|
2013-04-11 08:05:20 +08:00
|
|
|
bool
|
|
|
|
MallocChecker::isTrackedByCurrentChecker(CheckerContext &C,
|
|
|
|
const Stmt *AllocDeallocStmt) const {
|
|
|
|
return isTrackedByCurrentChecker(getAllocationFamily(C, AllocDeallocStmt));
|
2013-04-05 07:46:29 +08:00
|
|
|
}
|
|
|
|
|
2013-04-11 08:05:20 +08:00
|
|
|
bool MallocChecker::isTrackedByCurrentChecker(CheckerContext &C,
|
|
|
|
SymbolRef Sym) const {
|
2013-04-05 07:46:29 +08:00
|
|
|
|
2013-04-06 03:08:04 +08:00
|
|
|
const RefState *RS = C.getState()->get<RegionState>(Sym);
|
|
|
|
assert(RS);
|
2013-04-11 08:05:20 +08:00
|
|
|
return isTrackedByCurrentChecker(RS->getAllocationFamily());
|
2013-04-05 07:46:29 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
bool MallocChecker::SummarizeValue(raw_ostream &os, SVal V) {
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<nonloc::ConcreteInt> IntVal = V.getAs<nonloc::ConcreteInt>())
|
2010-06-08 03:32:37 +08:00
|
|
|
os << "an integer (" << IntVal->getValue() << ")";
|
2013-02-21 06:23:23 +08:00
|
|
|
else if (Optional<loc::ConcreteInt> ConstAddr = V.getAs<loc::ConcreteInt>())
|
2010-06-08 03:32:37 +08:00
|
|
|
os << "a constant address (" << ConstAddr->getValue() << ")";
|
2013-02-21 06:23:23 +08:00
|
|
|
else if (Optional<loc::GotoLabel> Label = V.getAs<loc::GotoLabel>())
|
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: {
|
2012-09-18 03:13:56 +08:00
|
|
|
const NamedDecl *FD = cast<FunctionTextRegion>(MR)->getDecl();
|
2010-06-08 03:32:37 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-29 01:05:19 +08:00
|
|
|
void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal,
|
|
|
|
SourceRange Range,
|
|
|
|
const Expr *DeallocExpr) const {
|
|
|
|
|
|
|
|
if (!Filter.CMallocOptimistic && !Filter.CMallocPessimistic &&
|
|
|
|
!Filter.CNewDeleteChecker)
|
|
|
|
return;
|
|
|
|
|
2013-04-11 08:05:20 +08:00
|
|
|
if (!isTrackedByCurrentChecker(C, DeallocExpr))
|
2013-04-05 07:46:29 +08:00
|
|
|
return;
|
|
|
|
|
2010-12-21 05:19:09 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
2010-06-08 03:32:37 +08:00
|
|
|
if (!BT_BadFree)
|
2012-02-17 06:26:12 +08:00
|
|
|
BT_BadFree.reset(new BugType("Bad free", "Memory Error"));
|
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);
|
2013-03-29 01:05:19 +08:00
|
|
|
|
2010-06-08 03:32:37 +08:00
|
|
|
const MemRegion *MR = ArgVal.getAsRegion();
|
2013-03-29 01:05:19 +08:00
|
|
|
while (const ElementRegion *ER = dyn_cast_or_null<ElementRegion>(MR))
|
|
|
|
MR = ER->getSuperRegion();
|
|
|
|
|
|
|
|
if (MR && isa<AllocaRegion>(MR))
|
|
|
|
os << "Memory allocated by alloca() should not be deallocated";
|
|
|
|
else {
|
|
|
|
os << "Argument to ";
|
|
|
|
if (!printAllocDeallocName(os, C, DeallocExpr))
|
|
|
|
os << "deallocator";
|
|
|
|
|
|
|
|
os << " is ";
|
|
|
|
bool Summarized = MR ? SummarizeRegion(os, MR)
|
|
|
|
: SummarizeValue(os, ArgVal);
|
|
|
|
if (Summarized)
|
|
|
|
os << ", which is not memory allocated by ";
|
2010-06-08 03:32:37 +08:00
|
|
|
else
|
2013-03-29 01:05:19 +08:00
|
|
|
os << "not memory allocated by ";
|
|
|
|
|
|
|
|
printExpectedAllocName(os, C, DeallocExpr);
|
2010-06-08 03:32:37 +08:00
|
|
|
}
|
2013-03-29 01:05:19 +08:00
|
|
|
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *R = new BugReport(*BT_BadFree, os.str(), N);
|
2012-03-09 09:13:14 +08:00
|
|
|
R->markInteresting(MR);
|
2013-03-13 22:39:10 +08:00
|
|
|
R->addRange(Range);
|
2012-11-02 09:53:40 +08:00
|
|
|
C.emitReport(R);
|
2010-06-08 03:32:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-05 07:46:29 +08:00
|
|
|
void MallocChecker::ReportMismatchedDealloc(CheckerContext &C,
|
|
|
|
SourceRange Range,
|
|
|
|
const Expr *DeallocExpr,
|
2013-04-05 19:25:10 +08:00
|
|
|
const RefState *RS,
|
2013-09-17 01:51:25 +08:00
|
|
|
SymbolRef Sym,
|
|
|
|
bool OwnershipTransferred) const {
|
2013-03-29 01:05:19 +08:00
|
|
|
|
|
|
|
if (!Filter.CMismatchedDeallocatorChecker)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
2013-04-05 07:46:29 +08:00
|
|
|
if (!BT_MismatchedDealloc)
|
|
|
|
BT_MismatchedDealloc.reset(new BugType("Bad deallocator",
|
|
|
|
"Memory Error"));
|
2013-03-29 01:05:19 +08:00
|
|
|
|
|
|
|
SmallString<100> buf;
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
|
|
|
|
const Expr *AllocExpr = cast<Expr>(RS->getStmt());
|
|
|
|
SmallString<20> AllocBuf;
|
|
|
|
llvm::raw_svector_ostream AllocOs(AllocBuf);
|
|
|
|
SmallString<20> DeallocBuf;
|
|
|
|
llvm::raw_svector_ostream DeallocOs(DeallocBuf);
|
|
|
|
|
2013-09-17 01:51:25 +08:00
|
|
|
if (OwnershipTransferred) {
|
|
|
|
if (printAllocDeallocName(DeallocOs, C, DeallocExpr))
|
|
|
|
os << DeallocOs.str() << " cannot";
|
|
|
|
else
|
|
|
|
os << "Cannot";
|
2013-03-29 01:05:19 +08:00
|
|
|
|
2013-09-17 01:51:25 +08:00
|
|
|
os << " take ownership of memory";
|
2013-03-29 01:05:19 +08:00
|
|
|
|
2013-09-17 01:51:25 +08:00
|
|
|
if (printAllocDeallocName(AllocOs, C, AllocExpr))
|
|
|
|
os << " allocated by " << AllocOs.str();
|
|
|
|
} else {
|
|
|
|
os << "Memory";
|
|
|
|
if (printAllocDeallocName(AllocOs, C, AllocExpr))
|
|
|
|
os << " allocated by " << AllocOs.str();
|
|
|
|
|
|
|
|
os << " should be deallocated by ";
|
|
|
|
printExpectedDeallocName(os, RS->getAllocationFamily());
|
|
|
|
|
|
|
|
if (printAllocDeallocName(DeallocOs, C, DeallocExpr))
|
|
|
|
os << ", not " << DeallocOs.str();
|
|
|
|
}
|
2013-03-29 01:05:19 +08:00
|
|
|
|
2013-04-05 07:46:29 +08:00
|
|
|
BugReport *R = new BugReport(*BT_MismatchedDealloc, os.str(), N);
|
2013-04-05 19:25:10 +08:00
|
|
|
R->markInteresting(Sym);
|
2013-03-29 01:05:19 +08:00
|
|
|
R->addRange(Range);
|
2013-04-05 19:25:10 +08:00
|
|
|
R->addVisitor(new MallocBugVisitor(Sym));
|
2013-03-29 01:05:19 +08:00
|
|
|
C.emitReport(R);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 07:05:47 +08:00
|
|
|
void MallocChecker::ReportOffsetFree(CheckerContext &C, SVal ArgVal,
|
2013-03-29 01:05:19 +08:00
|
|
|
SourceRange Range, const Expr *DeallocExpr,
|
|
|
|
const Expr *AllocExpr) const {
|
|
|
|
|
|
|
|
if (!Filter.CMallocOptimistic && !Filter.CMallocPessimistic &&
|
|
|
|
!Filter.CNewDeleteChecker)
|
|
|
|
return;
|
|
|
|
|
2013-04-11 08:05:20 +08:00
|
|
|
if (!isTrackedByCurrentChecker(C, AllocExpr))
|
2013-04-05 07:46:29 +08:00
|
|
|
return;
|
|
|
|
|
2013-02-08 07:05:47 +08:00
|
|
|
ExplodedNode *N = C.generateSink();
|
|
|
|
if (N == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!BT_OffsetFree)
|
|
|
|
BT_OffsetFree.reset(new BugType("Offset free", "Memory Error"));
|
|
|
|
|
|
|
|
SmallString<100> buf;
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
2013-03-29 01:05:19 +08:00
|
|
|
SmallString<20> AllocNameBuf;
|
|
|
|
llvm::raw_svector_ostream AllocNameOs(AllocNameBuf);
|
2013-02-08 07:05:47 +08:00
|
|
|
|
|
|
|
const MemRegion *MR = ArgVal.getAsRegion();
|
|
|
|
assert(MR && "Only MemRegion based symbols can have offset free errors");
|
|
|
|
|
|
|
|
RegionOffset Offset = MR->getAsOffset();
|
|
|
|
assert((Offset.isValid() &&
|
|
|
|
!Offset.hasSymbolicOffset() &&
|
|
|
|
Offset.getOffset() != 0) &&
|
|
|
|
"Only symbols with a valid offset can have offset free errors");
|
|
|
|
|
|
|
|
int offsetBytes = Offset.getOffset() / C.getASTContext().getCharWidth();
|
|
|
|
|
2013-03-29 01:05:19 +08:00
|
|
|
os << "Argument to ";
|
|
|
|
if (!printAllocDeallocName(os, C, DeallocExpr))
|
|
|
|
os << "deallocator";
|
|
|
|
os << " is offset by "
|
2013-02-08 07:05:47 +08:00
|
|
|
<< offsetBytes
|
|
|
|
<< " "
|
|
|
|
<< ((abs(offsetBytes) > 1) ? "bytes" : "byte")
|
2013-03-29 01:05:19 +08:00
|
|
|
<< " from the start of ";
|
|
|
|
if (AllocExpr && printAllocDeallocName(AllocNameOs, C, AllocExpr))
|
|
|
|
os << "memory allocated by " << AllocNameOs.str();
|
|
|
|
else
|
|
|
|
os << "allocated memory";
|
2013-02-08 07:05:47 +08:00
|
|
|
|
|
|
|
BugReport *R = new BugReport(*BT_OffsetFree, os.str(), N);
|
|
|
|
R->markInteresting(MR->getBaseRegion());
|
|
|
|
R->addRange(Range);
|
|
|
|
C.emitReport(R);
|
|
|
|
}
|
|
|
|
|
2013-03-13 22:39:10 +08:00
|
|
|
void MallocChecker::ReportUseAfterFree(CheckerContext &C, SourceRange Range,
|
|
|
|
SymbolRef Sym) const {
|
|
|
|
|
2013-03-29 01:05:19 +08:00
|
|
|
if (!Filter.CMallocOptimistic && !Filter.CMallocPessimistic &&
|
|
|
|
!Filter.CNewDeleteChecker)
|
|
|
|
return;
|
|
|
|
|
2013-04-11 08:05:20 +08:00
|
|
|
if (!isTrackedByCurrentChecker(C, Sym))
|
2013-04-05 07:46:29 +08:00
|
|
|
return;
|
|
|
|
|
2013-03-13 22:39:10 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
|
|
|
if (!BT_UseFree)
|
|
|
|
BT_UseFree.reset(new BugType("Use-after-free", "Memory Error"));
|
|
|
|
|
|
|
|
BugReport *R = new BugReport(*BT_UseFree,
|
|
|
|
"Use of memory after it is freed", N);
|
|
|
|
|
|
|
|
R->markInteresting(Sym);
|
|
|
|
R->addRange(Range);
|
|
|
|
R->addVisitor(new MallocBugVisitor(Sym));
|
|
|
|
C.emitReport(R);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MallocChecker::ReportDoubleFree(CheckerContext &C, SourceRange Range,
|
|
|
|
bool Released, SymbolRef Sym,
|
2013-03-14 01:07:32 +08:00
|
|
|
SymbolRef PrevSym) const {
|
2013-03-13 22:39:10 +08:00
|
|
|
|
2013-03-29 01:05:19 +08:00
|
|
|
if (!Filter.CMallocOptimistic && !Filter.CMallocPessimistic &&
|
|
|
|
!Filter.CNewDeleteChecker)
|
|
|
|
return;
|
|
|
|
|
2013-04-11 08:05:20 +08:00
|
|
|
if (!isTrackedByCurrentChecker(C, Sym))
|
2013-04-05 07:46:29 +08:00
|
|
|
return;
|
|
|
|
|
2013-03-13 22:39:10 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
|
|
|
if (!BT_DoubleFree)
|
|
|
|
BT_DoubleFree.reset(new BugType("Double free", "Memory Error"));
|
|
|
|
|
|
|
|
BugReport *R = new BugReport(*BT_DoubleFree,
|
|
|
|
(Released ? "Attempt to free released memory"
|
|
|
|
: "Attempt to free non-owned memory"),
|
|
|
|
N);
|
|
|
|
R->addRange(Range);
|
2013-03-14 01:07:32 +08:00
|
|
|
R->markInteresting(Sym);
|
|
|
|
if (PrevSym)
|
|
|
|
R->markInteresting(PrevSym);
|
2013-03-13 22:39:10 +08:00
|
|
|
R->addVisitor(new MallocBugVisitor(Sym));
|
|
|
|
C.emitReport(R);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-23 03:24:52 +08:00
|
|
|
ProgramStateRef MallocChecker::ReallocMem(CheckerContext &C,
|
|
|
|
const CallExpr *CE,
|
|
|
|
bool FreesOnFail) const {
|
2012-04-11 07:41:11 +08:00
|
|
|
if (CE->getNumArgs() < 2)
|
|
|
|
return 0;
|
|
|
|
|
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);
|
2013-02-20 13:52:05 +08:00
|
|
|
if (!Arg0Val.getAs<DefinedOrUnknownSVal>())
|
2012-02-23 03:24:52 +08:00
|
|
|
return 0;
|
2013-02-20 13:52:05 +08:00
|
|
|
DefinedOrUnknownSVal arg0Val = Arg0Val.castAs<DefinedOrUnknownSVal>();
|
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)
|
2012-02-23 03:24:52 +08:00
|
|
|
return 0;
|
2011-04-27 22:49:29 +08:00
|
|
|
|
|
|
|
// Get the value of the size argument.
|
2012-02-10 09:11:00 +08:00
|
|
|
SVal Arg1ValG = state->getSVal(Arg1, LCtx);
|
2013-02-20 13:52:05 +08:00
|
|
|
if (!Arg1ValG.getAs<DefinedOrUnknownSVal>())
|
2012-02-23 03:24:52 +08:00
|
|
|
return 0;
|
2013-02-20 13:52:05 +08:00
|
|
|
DefinedOrUnknownSVal Arg1Val = Arg1ValG.castAs<DefinedOrUnknownSVal>();
|
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-02-23 03:24:52 +08:00
|
|
|
ProgramStateRef stateMalloc = MallocMemAux(C, CE, CE->getArg(1),
|
2012-02-14 02:05:39 +08:00
|
|
|
UndefinedVal(), StatePtrIsNull);
|
2012-02-23 03:24:52 +08:00
|
|
|
return stateMalloc;
|
2009-12-12 20:29:38 +08:00
|
|
|
}
|
|
|
|
|
2012-02-14 02:05:39 +08:00
|
|
|
if (PrtIsNull && SizeIsZero)
|
2012-02-23 03:24:52 +08:00
|
|
|
return 0;
|
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)
|
2012-02-23 03:24:52 +08:00
|
|
|
return 0;
|
2012-02-14 02:05:39 +08:00
|
|
|
|
2012-08-24 10:28:20 +08:00
|
|
|
bool ReleasedAllocated = false;
|
|
|
|
|
2012-02-14 02:05:39 +08:00
|
|
|
// If the size is 0, free the memory.
|
|
|
|
if (SizeIsZero)
|
2012-08-24 10:28:20 +08:00
|
|
|
if (ProgramStateRef stateFree = FreeMemAux(C, CE, StateSizeIsZero, 0,
|
|
|
|
false, ReleasedAllocated)){
|
2012-02-14 02:05:39 +08:00
|
|
|
// The semantics of the return value are:
|
|
|
|
// If size was equal to 0, either NULL or a pointer suitable to be passed
|
2012-08-04 02:30:18 +08:00
|
|
|
// to free() is returned. We just free the input pointer and do not add
|
|
|
|
// any constrains on the output pointer.
|
2012-02-23 03:24:52 +08:00
|
|
|
return stateFree;
|
2012-02-14 02:05:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default behavior.
|
2012-08-24 10:28:20 +08:00
|
|
|
if (ProgramStateRef stateFree =
|
|
|
|
FreeMemAux(C, CE, state, 0, false, ReleasedAllocated)) {
|
|
|
|
|
2012-02-14 02:05:39 +08:00
|
|
|
ProgramStateRef stateRealloc = MallocMemAux(C, CE, CE->getArg(1),
|
|
|
|
UnknownVal(), stateFree);
|
2012-02-14 04:57:07 +08:00
|
|
|
if (!stateRealloc)
|
2012-02-23 03:24:52 +08:00
|
|
|
return 0;
|
2012-08-24 10:28:20 +08:00
|
|
|
|
2012-09-13 06:57:34 +08:00
|
|
|
ReallocPairKind Kind = RPToBeFreedAfterFailure;
|
|
|
|
if (FreesOnFail)
|
|
|
|
Kind = RPIsFreeOnFailure;
|
|
|
|
else if (!ReleasedAllocated)
|
|
|
|
Kind = RPDoNotTrackAfterFailure;
|
|
|
|
|
2012-08-24 10:28:20 +08:00
|
|
|
// Record the info about the reallocated symbol so that we could properly
|
|
|
|
// process failed reallocation.
|
2012-02-15 08:11:25 +08:00
|
|
|
stateRealloc = stateRealloc->set<ReallocPairs>(ToPtr,
|
2012-09-13 06:57:34 +08:00
|
|
|
ReallocPair(FromPtr, Kind));
|
2012-08-24 10:28:20 +08:00
|
|
|
// The reallocated symbol should stay alive for as long as the new symbol.
|
2012-02-14 08:26:13 +08:00
|
|
|
C.getSymbolManager().addSymbolDependency(ToPtr, FromPtr);
|
2012-02-23 03:24:52 +08:00
|
|
|
return stateRealloc;
|
2009-12-12 20:29:38 +08:00
|
|
|
}
|
2012-02-23 03:24:52 +08:00
|
|
|
return 0;
|
2009-11-12 16:38:56 +08:00
|
|
|
}
|
2009-11-13 15:25:27 +08:00
|
|
|
|
2012-02-23 03:24:52 +08:00
|
|
|
ProgramStateRef MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE){
|
2012-04-11 07:41:11 +08:00
|
|
|
if (CE->getNumArgs() < 2)
|
|
|
|
return 0;
|
|
|
|
|
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
|
|
|
|
2012-02-23 03:24:52 +08:00
|
|
|
return MallocMemAux(C, CE, TotalSize, zeroVal, state);
|
2010-06-01 11:01:33 +08:00
|
|
|
}
|
|
|
|
|
2012-03-22 03:45:08 +08:00
|
|
|
LeakInfo
|
2012-02-24 05:38:21 +08:00
|
|
|
MallocChecker::getAllocationSite(const ExplodedNode *N, SymbolRef Sym,
|
|
|
|
CheckerContext &C) const {
|
2012-02-28 07:40:55 +08:00
|
|
|
const LocationContext *LeakContext = N->getLocationContext();
|
2012-02-24 05:38:21 +08:00
|
|
|
// Walk the ExplodedGraph backwards and find the first node that referred to
|
|
|
|
// the tracked symbol.
|
|
|
|
const ExplodedNode *AllocNode = N;
|
2012-03-22 03:45:08 +08:00
|
|
|
const MemRegion *ReferenceRegion = 0;
|
2012-02-24 05:38:21 +08:00
|
|
|
|
|
|
|
while (N) {
|
2012-03-22 03:45:08 +08:00
|
|
|
ProgramStateRef State = N->getState();
|
|
|
|
if (!State->get<RegionState>(Sym))
|
2012-02-24 05:38:21 +08:00
|
|
|
break;
|
2012-03-22 03:45:08 +08:00
|
|
|
|
|
|
|
// Find the most recent expression bound to the symbol in the current
|
|
|
|
// context.
|
2013-04-11 05:42:02 +08:00
|
|
|
if (!ReferenceRegion) {
|
|
|
|
if (const MemRegion *MR = C.getLocationRegionIfPostStore(N)) {
|
|
|
|
SVal Val = State->getSVal(MR);
|
|
|
|
if (Val.getAsLocSymbol() == Sym) {
|
2013-04-11 06:56:33 +08:00
|
|
|
const VarRegion* VR = MR->getBaseRegion()->getAs<VarRegion>();
|
2013-04-11 05:42:02 +08:00
|
|
|
// Do not show local variables belonging to a function other than
|
|
|
|
// where the error is reported.
|
|
|
|
if (!VR ||
|
|
|
|
(VR->getStackFrame() == LeakContext->getCurrentStackFrame()))
|
|
|
|
ReferenceRegion = MR;
|
|
|
|
}
|
|
|
|
}
|
2012-03-22 05:03:48 +08:00
|
|
|
}
|
2012-03-22 03:45:08 +08:00
|
|
|
|
2012-02-28 07:40:55 +08:00
|
|
|
// Allocation node, is the last node in the current context in which the
|
|
|
|
// symbol was tracked.
|
|
|
|
if (N->getLocationContext() == LeakContext)
|
|
|
|
AllocNode = N;
|
2012-02-24 05:38:21 +08:00
|
|
|
N = N->pred_empty() ? NULL : *(N->pred_begin());
|
|
|
|
}
|
|
|
|
|
2013-01-08 08:25:29 +08:00
|
|
|
return LeakInfo(AllocNode, ReferenceRegion);
|
2012-02-24 05:38:21 +08:00
|
|
|
}
|
|
|
|
|
2012-02-12 05:02:40 +08:00
|
|
|
void MallocChecker::reportLeak(SymbolRef Sym, ExplodedNode *N,
|
|
|
|
CheckerContext &C) const {
|
2013-03-29 01:05:19 +08:00
|
|
|
|
|
|
|
if (!Filter.CMallocOptimistic && !Filter.CMallocPessimistic &&
|
2013-04-06 01:55:00 +08:00
|
|
|
!Filter.CNewDeleteLeaksChecker)
|
2013-03-29 01:05:19 +08:00
|
|
|
return;
|
|
|
|
|
2013-04-06 01:55:00 +08:00
|
|
|
const RefState *RS = C.getState()->get<RegionState>(Sym);
|
|
|
|
assert(RS && "cannot leak an untracked symbol");
|
|
|
|
AllocationFamily Family = RS->getAllocationFamily();
|
2013-04-11 08:05:20 +08:00
|
|
|
if (!isTrackedByCurrentChecker(Family))
|
2013-04-05 10:25:02 +08:00
|
|
|
return;
|
|
|
|
|
2013-04-06 01:55:00 +08:00
|
|
|
// Special case for new and new[]; these are controlled by a separate checker
|
|
|
|
// flag so that they can be selectively disabled.
|
|
|
|
if (Family == AF_CXXNew || Family == AF_CXXNewArray)
|
|
|
|
if (!Filter.CNewDeleteLeaksChecker)
|
|
|
|
return;
|
|
|
|
|
2012-02-12 05:02:40 +08:00
|
|
|
assert(N);
|
|
|
|
if (!BT_Leak) {
|
2012-02-17 06:26:12 +08:00
|
|
|
BT_Leak.reset(new BugType("Memory leak", "Memory Error"));
|
2012-02-12 05:02:40 +08:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2012-02-24 05:38:21 +08:00
|
|
|
// Most bug reports are cached at the location where they occurred.
|
|
|
|
// With leaks, we want to unique them by the location where they were
|
|
|
|
// allocated, and only report a single path.
|
2012-02-28 07:40:55 +08:00
|
|
|
PathDiagnosticLocation LocUsedForUniqueing;
|
2013-01-08 08:25:29 +08:00
|
|
|
const ExplodedNode *AllocNode = 0;
|
2012-03-22 03:45:08 +08:00
|
|
|
const MemRegion *Region = 0;
|
2013-01-08 08:25:29 +08:00
|
|
|
llvm::tie(AllocNode, Region) = getAllocationSite(N, Sym, C);
|
|
|
|
|
|
|
|
ProgramPoint P = AllocNode->getLocation();
|
|
|
|
const Stmt *AllocationStmt = 0;
|
2013-02-22 06:23:56 +08:00
|
|
|
if (Optional<CallExitEnd> Exit = P.getAs<CallExitEnd>())
|
2013-01-08 08:25:29 +08:00
|
|
|
AllocationStmt = Exit->getCalleeContext()->getCallSite();
|
2013-02-22 06:23:56 +08:00
|
|
|
else if (Optional<StmtPoint> SP = P.getAs<StmtPoint>())
|
2013-01-08 08:25:29 +08:00
|
|
|
AllocationStmt = SP->getStmt();
|
2013-04-05 10:25:02 +08:00
|
|
|
if (AllocationStmt)
|
2013-01-08 08:25:29 +08:00
|
|
|
LocUsedForUniqueing = PathDiagnosticLocation::createBegin(AllocationStmt,
|
|
|
|
C.getSourceManager(),
|
|
|
|
AllocNode->getLocationContext());
|
2012-02-24 05:38:21 +08:00
|
|
|
|
2012-03-22 03:45:08 +08:00
|
|
|
SmallString<200> buf;
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
2012-08-09 02:23:36 +08:00
|
|
|
if (Region && Region->canPrintPretty()) {
|
2013-04-13 02:40:21 +08:00
|
|
|
os << "Potential leak of memory pointed to by ";
|
2012-08-09 02:23:36 +08:00
|
|
|
Region->printPretty(os);
|
2013-04-06 08:41:36 +08:00
|
|
|
} else {
|
|
|
|
os << "Potential memory leak";
|
2012-03-22 03:45:08 +08:00
|
|
|
}
|
|
|
|
|
2013-01-08 08:25:29 +08:00
|
|
|
BugReport *R = new BugReport(*BT_Leak, os.str(), N,
|
|
|
|
LocUsedForUniqueing,
|
|
|
|
AllocNode->getLocationContext()->getDecl());
|
2012-03-09 09:13:14 +08:00
|
|
|
R->markInteresting(Sym);
|
2012-05-10 09:37:40 +08:00
|
|
|
R->addVisitor(new MallocBugVisitor(Sym, true));
|
2012-11-02 09:53:40 +08:00
|
|
|
C.emitReport(R);
|
2012-02-12 05:02:40 +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;
|
|
|
|
|
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
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
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-10-30 06:51:54 +08:00
|
|
|
if (I->second.isAllocated())
|
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.
|
2012-11-02 09:54:06 +08:00
|
|
|
ReallocPairsTy RP = state->get<ReallocPairs>();
|
|
|
|
for (ReallocPairsTy::iterator I = RP.begin(), E = RP.end(); I != E; ++I) {
|
2012-02-15 08:11:25 +08:00
|
|
|
if (SymReaper.isDead(I->first) ||
|
|
|
|
SymReaper.isDead(I->second.ReallocatedSym)) {
|
2012-02-14 02:05:39 +08:00
|
|
|
state = state->remove<ReallocPairs>(I->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-13 11:18:01 +08:00
|
|
|
// Cleanup the FreeReturnValue Map.
|
|
|
|
FreeReturnValueTy FR = state->get<FreeReturnValue>();
|
|
|
|
for (FreeReturnValueTy::iterator I = FR.begin(), E = FR.end(); I != E; ++I) {
|
|
|
|
if (SymReaper.isDead(I->first) ||
|
|
|
|
SymReaper.isDead(I->second)) {
|
|
|
|
state = state->remove<FreeReturnValue>(I->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-24 05:38:21 +08:00
|
|
|
// Generate leak node.
|
2012-10-30 06:51:54 +08:00
|
|
|
ExplodedNode *N = C.getPredecessor();
|
|
|
|
if (!Errors.empty()) {
|
|
|
|
static SimpleProgramPointTag Tag("MallocChecker : DeadSymbolsLeak");
|
|
|
|
N = C.addTransition(C.getState(), C.getPredecessor(), &Tag);
|
2013-07-04 11:08:24 +08:00
|
|
|
for (SmallVectorImpl<SymbolRef>::iterator
|
|
|
|
I = Errors.begin(), E = Errors.end(); I != E; ++I) {
|
2012-02-12 05:02:40 +08:00
|
|
|
reportLeak(*I, N, C);
|
2012-02-09 14:48:19 +08:00
|
|
|
}
|
2011-07-29 07:07:51 +08:00
|
|
|
}
|
2012-10-30 06:51:54 +08:00
|
|
|
|
2012-02-24 05:38:21 +08:00
|
|
|
C.addTransition(state->set<RegionState>(RS), N);
|
2009-11-13 15:25:27 +08:00
|
|
|
}
|
2009-11-17 15:54:15 +08:00
|
|
|
|
2013-04-11 06:21:41 +08:00
|
|
|
void MallocChecker::checkPreCall(const CallEvent &Call,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
|
2012-05-18 09:16:10 +08:00
|
|
|
// We will check for double free in the post visit.
|
2013-04-11 06:21:41 +08:00
|
|
|
if (const AnyFunctionCall *FC = dyn_cast<AnyFunctionCall>(&Call)) {
|
|
|
|
const FunctionDecl *FD = FC->getDecl();
|
|
|
|
if (!FD)
|
|
|
|
return;
|
2013-03-25 09:35:45 +08:00
|
|
|
|
2013-04-11 06:21:41 +08:00
|
|
|
if ((Filter.CMallocOptimistic || Filter.CMallocPessimistic) &&
|
|
|
|
isFreeFunction(FD, C.getASTContext()))
|
|
|
|
return;
|
2012-02-15 05:55:24 +08:00
|
|
|
|
2013-04-11 06:21:41 +08:00
|
|
|
if (Filter.CNewDeleteChecker &&
|
|
|
|
isStandardNewDelete(FD, C.getASTContext()))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the callee of a method is deleted.
|
|
|
|
if (const CXXInstanceCall *CC = dyn_cast<CXXInstanceCall>(&Call)) {
|
|
|
|
SymbolRef Sym = CC->getCXXThisVal().getAsSymbol();
|
|
|
|
if (!Sym || checkUseAfterFree(Sym, C, CC->getCXXThisExpr()))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check arguments for being used after free.
|
|
|
|
for (unsigned I = 0, E = Call.getNumArgs(); I != E; ++I) {
|
|
|
|
SVal ArgSVal = Call.getArgSVal(I);
|
|
|
|
if (ArgSVal.getAs<Loc>()) {
|
|
|
|
SymbolRef Sym = ArgSVal.getAsSymbol();
|
2012-02-15 05:55:24 +08:00
|
|
|
if (!Sym)
|
|
|
|
continue;
|
2013-04-11 06:21:41 +08:00
|
|
|
if (checkUseAfterFree(Sym, C, Call.getArgExpr(I)))
|
2012-02-15 05:55:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-09 02:23:31 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
SVal RetVal = State->getSVal(E, C.getLocationContext());
|
2012-02-22 10:36:01 +08:00
|
|
|
SymbolRef Sym = RetVal.getAsSymbol();
|
|
|
|
if (!Sym)
|
|
|
|
// If we are returning a field of the allocated struct or an array element,
|
|
|
|
// the callee could still free the memory.
|
|
|
|
// TODO: This logic should be a part of generic symbol escape callback.
|
|
|
|
if (const MemRegion *MR = RetVal.getAsRegion())
|
|
|
|
if (isa<FieldRegion>(MR) || isa<ElementRegion>(MR))
|
|
|
|
if (const SymbolicRegion *BMR =
|
|
|
|
dyn_cast<SymbolicRegion>(MR->getBaseRegion()))
|
|
|
|
Sym = BMR->getSymbol();
|
2012-02-09 07:16:56 +08:00
|
|
|
|
2012-02-12 05:44:39 +08:00
|
|
|
// Check if we are returning freed memory.
|
2012-08-09 02:23:31 +08:00
|
|
|
if (Sym)
|
2012-11-16 03:11:33 +08:00
|
|
|
checkUseAfterFree(Sym, C, E);
|
2009-11-17 16:58:18 +08:00
|
|
|
}
|
2009-12-31 14:13:07 +08:00
|
|
|
|
2012-03-22 08:57:20 +08:00
|
|
|
// TODO: Blocks should be either inlined or should call invalidate regions
|
|
|
|
// upon invocation. After that's in place, special casing here will not be
|
|
|
|
// needed.
|
|
|
|
void MallocChecker::checkPostStmt(const BlockExpr *BE,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
|
|
|
|
// Scan the BlockDecRefExprs for any object the retain count checker
|
|
|
|
// may be tracking.
|
|
|
|
if (!BE->getBlockDecl()->hasCaptures())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ProgramStateRef state = C.getState();
|
|
|
|
const BlockDataRegion *R =
|
|
|
|
cast<BlockDataRegion>(state->getSVal(BE,
|
|
|
|
C.getLocationContext()).getAsRegion());
|
|
|
|
|
|
|
|
BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
|
|
|
|
E = R->referenced_vars_end();
|
|
|
|
|
|
|
|
if (I == E)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SmallVector<const MemRegion*, 10> Regions;
|
|
|
|
const LocationContext *LC = C.getLocationContext();
|
|
|
|
MemRegionManager &MemMgr = C.getSValBuilder().getRegionManager();
|
|
|
|
|
|
|
|
for ( ; I != E; ++I) {
|
2012-12-06 15:17:20 +08:00
|
|
|
const VarRegion *VR = I.getCapturedRegion();
|
2012-03-22 08:57:20 +08:00
|
|
|
if (VR->getSuperRegion() == R) {
|
|
|
|
VR = MemMgr.getVarRegion(VR->getDecl(), LC);
|
|
|
|
}
|
|
|
|
Regions.push_back(VR);
|
|
|
|
}
|
|
|
|
|
|
|
|
state =
|
|
|
|
state->scanReachableSymbols<StopTrackingCallback>(Regions.data(),
|
|
|
|
Regions.data() + Regions.size()).getState();
|
|
|
|
C.addTransition(state);
|
|
|
|
}
|
|
|
|
|
2012-05-18 09:16:10 +08:00
|
|
|
bool MallocChecker::isReleased(SymbolRef Sym, CheckerContext &C) const {
|
2012-02-09 07:16:56 +08:00
|
|
|
assert(Sym);
|
|
|
|
const RefState *RS = C.getState()->get<RegionState>(Sym);
|
2012-05-18 09:16:10 +08:00
|
|
|
return (RS && RS->isReleased());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MallocChecker::checkUseAfterFree(SymbolRef Sym, CheckerContext &C,
|
|
|
|
const Stmt *S) const {
|
2013-03-13 22:39:10 +08:00
|
|
|
|
2013-09-26 00:06:17 +08:00
|
|
|
// FIXME: Handle destructor called from delete more precisely.
|
|
|
|
if (isReleased(Sym, C) && S) {
|
2013-03-13 22:39:10 +08:00
|
|
|
ReportUseAfterFree(C, S->getSourceRange(), Sym);
|
|
|
|
return true;
|
2012-02-09 07:16:56 +08:00
|
|
|
}
|
2013-03-13 22:39:10 +08:00
|
|
|
|
2012-02-09 07:16:56 +08:00
|
|
|
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)
|
2012-05-18 09:16:10 +08:00
|
|
|
checkUseAfterFree(Sym, C, S);
|
2010-03-10 12:58:55 +08:00
|
|
|
}
|
2010-07-31 09:52:11 +08:00
|
|
|
|
2012-02-12 05:02:35 +08:00
|
|
|
// 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) {
|
2012-09-08 06:31:01 +08:00
|
|
|
// If the symbol is assumed to be NULL, remove it from consideration.
|
2012-11-01 08:18:27 +08:00
|
|
|
ConstraintManager &CMgr = state->getConstraintManager();
|
|
|
|
ConditionTruthVal AllocFailed = CMgr.isNull(state, I.getKey());
|
|
|
|
if (AllocFailed.isConstrainedTrue())
|
2012-02-12 05:02:35 +08:00
|
|
|
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.
|
2012-11-02 09:54:06 +08:00
|
|
|
ReallocPairsTy RP = state->get<ReallocPairs>();
|
|
|
|
for (ReallocPairsTy::iterator I = RP.begin(), E = RP.end(); I != E; ++I) {
|
2012-09-08 06:31:01 +08:00
|
|
|
// If the symbol is assumed to be NULL, remove it from consideration.
|
2012-11-01 08:18:27 +08:00
|
|
|
ConstraintManager &CMgr = state->getConstraintManager();
|
|
|
|
ConditionTruthVal AllocFailed = CMgr.isNull(state, I.getKey());
|
2012-11-01 08:25:15 +08:00
|
|
|
if (!AllocFailed.isConstrainedTrue())
|
2012-09-13 06:57:34 +08:00
|
|
|
continue;
|
2012-11-01 08:18:27 +08:00
|
|
|
|
2012-09-13 06:57:34 +08:00
|
|
|
SymbolRef ReallocSym = I.getData().ReallocatedSym;
|
|
|
|
if (const RefState *RS = state->get<RegionState>(ReallocSym)) {
|
|
|
|
if (RS->isReleased()) {
|
|
|
|
if (I.getData().Kind == RPToBeFreedAfterFailure)
|
2012-02-15 08:11:25 +08:00
|
|
|
state = state->set<RegionState>(ReallocSym,
|
2013-03-29 01:05:19 +08:00
|
|
|
RefState::getAllocated(RS->getAllocationFamily(), RS->getStmt()));
|
2012-09-13 06:57:34 +08:00
|
|
|
else if (I.getData().Kind == RPDoNotTrackAfterFailure)
|
|
|
|
state = state->remove<RegionState>(ReallocSym);
|
|
|
|
else
|
|
|
|
assert(I.getData().Kind == RPIsFreeOnFailure);
|
2012-02-14 02:05:39 +08:00
|
|
|
}
|
|
|
|
}
|
2012-09-13 06:57:34 +08:00
|
|
|
state = state->remove<ReallocPairs>(I.getKey());
|
2012-02-14 02:05:39 +08:00
|
|
|
}
|
|
|
|
|
2012-02-12 05:02:35 +08:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2013-06-08 08:29:29 +08:00
|
|
|
bool MallocChecker::mayFreeAnyEscapedMemoryOrIsModeledExplicitly(
|
2013-06-01 07:47:32 +08:00
|
|
|
const CallEvent *Call,
|
|
|
|
ProgramStateRef State,
|
|
|
|
SymbolRef &EscapingSymbol) const {
|
2012-07-03 03:27:51 +08:00
|
|
|
assert(Call);
|
2013-06-08 08:29:29 +08:00
|
|
|
EscapingSymbol = 0;
|
|
|
|
|
2012-02-25 07:56:53 +08:00
|
|
|
// For now, assume that any C++ call can free memory.
|
|
|
|
// TODO: If we want to be more optimistic here, we'll need to make sure that
|
|
|
|
// regions escape to C++ containers. They seem to do that even now, but for
|
|
|
|
// mysterious reasons.
|
2012-07-03 03:27:56 +08:00
|
|
|
if (!(isa<FunctionCall>(Call) || isa<ObjCMethodCall>(Call)))
|
2013-06-01 07:47:32 +08:00
|
|
|
return true;
|
2012-02-15 05:55:24 +08:00
|
|
|
|
2012-07-03 03:27:35 +08:00
|
|
|
// Check Objective-C messages by selector name.
|
2012-07-03 03:27:56 +08:00
|
|
|
if (const ObjCMethodCall *Msg = dyn_cast<ObjCMethodCall>(Call)) {
|
2012-07-03 03:27:51 +08:00
|
|
|
// If it's not a framework call, or if it takes a callback, assume it
|
|
|
|
// can free memory.
|
|
|
|
if (!Call->isInSystemHeader() || Call->hasNonZeroCallbackArg())
|
2013-06-01 07:47:32 +08:00
|
|
|
return true;
|
2012-03-30 13:48:16 +08:00
|
|
|
|
2013-03-09 08:59:10 +08:00
|
|
|
// If it's a method we know about, handle it explicitly post-call.
|
|
|
|
// This should happen before the "freeWhenDone" check below.
|
|
|
|
if (isKnownDeallocObjCMethodName(*Msg))
|
2013-06-01 07:47:32 +08:00
|
|
|
return false;
|
2012-02-25 07:56:53 +08:00
|
|
|
|
2013-03-09 08:59:10 +08:00
|
|
|
// If there's a "freeWhenDone" parameter, but the method isn't one we know
|
|
|
|
// about, we can't be sure that the object will use free() to deallocate the
|
|
|
|
// memory, so we can't model it explicitly. The best we can do is use it to
|
|
|
|
// decide whether the pointer escapes.
|
|
|
|
if (Optional<bool> FreeWhenDone = getFreeWhenDoneArg(*Msg))
|
2013-06-01 07:47:32 +08:00
|
|
|
return *FreeWhenDone;
|
2013-03-09 08:59:10 +08:00
|
|
|
|
|
|
|
// If the first selector piece ends with "NoCopy", and there is no
|
|
|
|
// "freeWhenDone" parameter set to zero, we know ownership is being
|
|
|
|
// transferred. Again, though, we can't be sure that the object will use
|
|
|
|
// free() to deallocate the memory, so we can't model it explicitly.
|
|
|
|
StringRef FirstSlot = Msg->getSelector().getNameForSlot(0);
|
2012-07-03 03:27:35 +08:00
|
|
|
if (FirstSlot.endswith("NoCopy"))
|
2013-06-01 07:47:32 +08:00
|
|
|
return true;
|
2012-03-06 01:42:10 +08:00
|
|
|
|
2012-06-19 13:10:32 +08:00
|
|
|
// If the first selector starts with addPointer, insertPointer,
|
|
|
|
// or replacePointer, assume we are dealing with NSPointerArray or similar.
|
|
|
|
// This is similar to C++ containers (vector); we still might want to check
|
2012-07-03 03:27:35 +08:00
|
|
|
// that the pointers get freed by following the container itself.
|
|
|
|
if (FirstSlot.startswith("addPointer") ||
|
|
|
|
FirstSlot.startswith("insertPointer") ||
|
|
|
|
FirstSlot.startswith("replacePointer")) {
|
2013-06-01 07:47:32 +08:00
|
|
|
return true;
|
2012-06-19 13:10:32 +08:00
|
|
|
}
|
|
|
|
|
2013-06-01 07:47:32 +08:00
|
|
|
// We should escape receiver on call to 'init'. This is especially relevant
|
|
|
|
// to the receiver, as the corresponding symbol is usually not referenced
|
|
|
|
// after the call.
|
|
|
|
if (Msg->getMethodFamily() == OMF_init) {
|
|
|
|
EscapingSymbol = Msg->getReceiverSVal().getAsSymbol();
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-01 06:39:13 +08:00
|
|
|
|
2012-07-03 03:27:35 +08:00
|
|
|
// Otherwise, assume that the method does not free memory.
|
|
|
|
// Most framework methods do not free memory.
|
2013-06-01 07:47:32 +08:00
|
|
|
return false;
|
2012-07-03 03:27:35 +08:00
|
|
|
}
|
2012-05-04 07:50:28 +08:00
|
|
|
|
2012-07-03 03:27:35 +08:00
|
|
|
// At this point the only thing left to handle is straight function calls.
|
|
|
|
const FunctionDecl *FD = cast<FunctionCall>(Call)->getDecl();
|
|
|
|
if (!FD)
|
2013-06-01 07:47:32 +08:00
|
|
|
return true;
|
2012-07-03 03:27:35 +08:00
|
|
|
|
|
|
|
ASTContext &ASTC = State->getStateManager().getContext();
|
|
|
|
|
|
|
|
// If it's one of the allocation functions we can reason about, we model
|
|
|
|
// its behavior explicitly.
|
|
|
|
if (isMemFunction(FD, ASTC))
|
2013-06-01 07:47:32 +08:00
|
|
|
return false;
|
2012-07-03 03:27:35 +08:00
|
|
|
|
|
|
|
// If it's not a system call, assume it frees memory.
|
|
|
|
if (!Call->isInSystemHeader())
|
2013-06-01 07:47:32 +08:00
|
|
|
return true;
|
2012-07-03 03:27:35 +08:00
|
|
|
|
|
|
|
// White list the system functions whose arguments escape.
|
|
|
|
const IdentifierInfo *II = FD->getIdentifier();
|
|
|
|
if (!II)
|
2013-06-01 07:47:32 +08:00
|
|
|
return true;
|
2012-07-03 03:27:35 +08:00
|
|
|
StringRef FName = II->getName();
|
|
|
|
|
|
|
|
// White list the 'XXXNoCopy' CoreFoundation functions.
|
2012-07-03 03:27:51 +08:00
|
|
|
// We specifically check these before
|
2012-07-03 03:27:35 +08:00
|
|
|
if (FName.endswith("NoCopy")) {
|
|
|
|
// Look for the deallocator argument. We know that the memory ownership
|
|
|
|
// is not transferred only if the deallocator argument is
|
|
|
|
// 'kCFAllocatorNull'.
|
|
|
|
for (unsigned i = 1; i < Call->getNumArgs(); ++i) {
|
|
|
|
const Expr *ArgE = Call->getArgExpr(i)->IgnoreParenCasts();
|
|
|
|
if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(ArgE)) {
|
|
|
|
StringRef DeallocatorName = DE->getFoundDecl()->getName();
|
|
|
|
if (DeallocatorName == "kCFAllocatorNull")
|
2013-06-01 07:47:32 +08:00
|
|
|
return false;
|
2012-07-03 03:27:35 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-01 07:47:32 +08:00
|
|
|
return true;
|
2012-02-15 05:55:24 +08:00
|
|
|
}
|
|
|
|
|
2012-07-03 03:27:35 +08:00
|
|
|
// Associating streams with malloced buffers. The pointer can escape if
|
2012-07-03 03:27:51 +08:00
|
|
|
// 'closefn' is specified (and if that function does free memory),
|
|
|
|
// but it will not if closefn is not specified.
|
2012-07-03 03:27:35 +08:00
|
|
|
// Currently, we do not inspect the 'closefn' function (PR12101).
|
|
|
|
if (FName == "funopen")
|
2012-07-03 03:27:51 +08:00
|
|
|
if (Call->getNumArgs() >= 4 && Call->getArgSVal(4).isConstant(0))
|
2013-06-01 07:47:32 +08:00
|
|
|
return false;
|
2012-02-25 07:56:53 +08:00
|
|
|
|
2012-07-03 03:27:35 +08:00
|
|
|
// Do not warn on pointers passed to 'setbuf' when used with std streams,
|
|
|
|
// these leaks might be intentional when setting the buffer for stdio.
|
|
|
|
// http://stackoverflow.com/questions/2671151/who-frees-setvbuf-buffer
|
|
|
|
if (FName == "setbuf" || FName =="setbuffer" ||
|
|
|
|
FName == "setlinebuf" || FName == "setvbuf") {
|
|
|
|
if (Call->getNumArgs() >= 1) {
|
|
|
|
const Expr *ArgE = Call->getArgExpr(0)->IgnoreParenCasts();
|
|
|
|
if (const DeclRefExpr *ArgDRE = dyn_cast<DeclRefExpr>(ArgE))
|
|
|
|
if (const VarDecl *D = dyn_cast<VarDecl>(ArgDRE->getDecl()))
|
|
|
|
if (D->getCanonicalDecl()->getName().find("std") != StringRef::npos)
|
2013-06-01 07:47:32 +08:00
|
|
|
return true;
|
2012-07-03 03:27:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A bunch of other functions which either take ownership of a pointer or
|
|
|
|
// wrap the result up in a struct or object, meaning it can be freed later.
|
|
|
|
// (See RetainCountChecker.) Not all the parameters here are invalidated,
|
|
|
|
// but the Malloc checker cannot differentiate between them. The right way
|
|
|
|
// of doing this would be to implement a pointer escapes callback.
|
|
|
|
if (FName == "CGBitmapContextCreate" ||
|
|
|
|
FName == "CGBitmapContextCreateWithData" ||
|
|
|
|
FName == "CVPixelBufferCreateWithBytes" ||
|
|
|
|
FName == "CVPixelBufferCreateWithPlanarBytes" ||
|
|
|
|
FName == "OSAtomicEnqueue") {
|
2013-06-01 07:47:32 +08:00
|
|
|
return true;
|
2012-07-03 03:27:35 +08:00
|
|
|
}
|
|
|
|
|
2012-07-03 03:27:51 +08:00
|
|
|
// Handle cases where we know a buffer's /address/ can escape.
|
|
|
|
// Note that the above checks handle some special cases where we know that
|
|
|
|
// even though the address escapes, it's still our responsibility to free the
|
|
|
|
// buffer.
|
|
|
|
if (Call->argumentsMayEscape())
|
2013-06-01 07:47:32 +08:00
|
|
|
return true;
|
2012-07-03 03:27:35 +08:00
|
|
|
|
|
|
|
// Otherwise, assume that the function does not free memory.
|
|
|
|
// Most system calls do not free the memory.
|
2013-06-01 07:47:32 +08:00
|
|
|
return false;
|
2012-02-15 05:55:24 +08:00
|
|
|
}
|
|
|
|
|
2013-03-29 07:15:29 +08:00
|
|
|
static bool retTrue(const RefState *RS) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool checkIfNewOrNewArrayFamily(const RefState *RS) {
|
|
|
|
return (RS->getAllocationFamily() == AF_CXXNewArray ||
|
|
|
|
RS->getAllocationFamily() == AF_CXXNew);
|
|
|
|
}
|
|
|
|
|
2012-12-20 08:38:25 +08:00
|
|
|
ProgramStateRef MallocChecker::checkPointerEscape(ProgramStateRef State,
|
|
|
|
const InvalidatedSymbols &Escaped,
|
2013-02-08 07:05:43 +08:00
|
|
|
const CallEvent *Call,
|
|
|
|
PointerEscapeKind Kind) const {
|
2013-03-29 07:15:29 +08:00
|
|
|
return checkPointerEscapeAux(State, Escaped, Call, Kind, &retTrue);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramStateRef MallocChecker::checkConstPointerEscape(ProgramStateRef State,
|
|
|
|
const InvalidatedSymbols &Escaped,
|
|
|
|
const CallEvent *Call,
|
|
|
|
PointerEscapeKind Kind) const {
|
|
|
|
return checkPointerEscapeAux(State, Escaped, Call, Kind,
|
|
|
|
&checkIfNewOrNewArrayFamily);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramStateRef MallocChecker::checkPointerEscapeAux(ProgramStateRef State,
|
|
|
|
const InvalidatedSymbols &Escaped,
|
|
|
|
const CallEvent *Call,
|
|
|
|
PointerEscapeKind Kind,
|
|
|
|
bool(*CheckRefState)(const RefState*)) const {
|
2013-03-09 08:59:10 +08:00
|
|
|
// If we know that the call does not free memory, or we want to process the
|
|
|
|
// call later, keep tracking the top level arguments.
|
2013-06-01 07:47:32 +08:00
|
|
|
SymbolRef EscapingSymbol = 0;
|
[analyzer] Indirect invalidation counts as an escape for leak checkers.
Consider this example:
char *p = malloc(sizeof(char));
systemFunction(&p);
free(p);
In this case, when we call systemFunction, we know (because it's a system
function) that it won't free 'p'. However, we /don't/ know whether or not
it will /change/ 'p', so the analyzer is forced to invalidate 'p', wiping
out any bindings it contains. But now the malloc'd region looks like a
leak, since there are no more bindings pointing to it, and we'll get a
spurious leak warning.
The fix for this is to notice when something is becoming inaccessible due
to invalidation (i.e. an imperfect model, as opposed to being explicitly
overwritten) and stop tracking it at that point. Currently, the best way
to determine this for a call is the "indirect escape" pointer-escape kind.
In practice, all the patch does is take the "system functions don't free
memory" special case and limit it to direct parameters, i.e. just the
arguments to a call and not other regions accessible to them. This is a
conservative change that should only cause us to escape regions more
eagerly, which means fewer leak warnings.
This isn't perfect for several reasons, the main one being that this
example is treated the same as the one above:
char **p = malloc(sizeof(char *));
systemFunction(p + 1);
// leak
Currently, "addresses accessible by offsets of the starting region" and
"addresses accessible through bindings of the starting region" are both
considered "indirect" regions, hence this uniform treatment.
Another issue is our longstanding problem of not distinguishing const and
non-const bindings; if in the first example systemFunction's parameter were
a char * const *, we should know that the function will not overwrite 'p',
and thus we can safely report the leak.
<rdar://problem/13758386>
llvm-svn: 181607
2013-05-11 01:07:16 +08:00
|
|
|
if (Kind == PSK_DirectEscapeOnCall &&
|
2013-06-08 08:29:29 +08:00
|
|
|
!mayFreeAnyEscapedMemoryOrIsModeledExplicitly(Call, State,
|
|
|
|
EscapingSymbol) &&
|
2013-06-01 07:47:32 +08:00
|
|
|
!EscapingSymbol) {
|
2012-02-15 05:55:24 +08:00
|
|
|
return State;
|
2013-02-08 07:05:43 +08:00
|
|
|
}
|
2012-02-25 07:56:53 +08:00
|
|
|
|
2012-12-20 08:38:25 +08:00
|
|
|
for (InvalidatedSymbols::const_iterator I = Escaped.begin(),
|
2013-03-29 07:15:29 +08:00
|
|
|
E = Escaped.end();
|
|
|
|
I != E; ++I) {
|
2012-02-12 05:02:35 +08:00
|
|
|
SymbolRef sym = *I;
|
2012-12-20 08:38:25 +08:00
|
|
|
|
2013-06-01 07:47:32 +08:00
|
|
|
if (EscapingSymbol && EscapingSymbol != sym)
|
|
|
|
continue;
|
|
|
|
|
2012-06-22 10:04:31 +08:00
|
|
|
if (const RefState *RS = State->get<RegionState>(sym)) {
|
2013-04-09 08:30:28 +08:00
|
|
|
if (RS->isAllocated() && CheckRefState(RS)) {
|
2012-08-09 08:42:24 +08:00
|
|
|
State = State->remove<RegionState>(sym);
|
2013-04-09 08:30:28 +08:00
|
|
|
State = State->set<RegionState>(sym, RefState::getEscaped(RS));
|
|
|
|
}
|
2012-06-22 10:04:31 +08:00
|
|
|
}
|
2012-02-12 05:02:35 +08:00
|
|
|
}
|
2012-02-15 05:55:24 +08:00
|
|
|
return State;
|
2010-07-31 09:52:11 +08:00
|
|
|
}
|
2011-02-28 09:26:35 +08:00
|
|
|
|
2012-03-18 15:43:35 +08:00
|
|
|
static SymbolRef findFailedReallocSymbol(ProgramStateRef currState,
|
|
|
|
ProgramStateRef prevState) {
|
2012-11-02 09:54:06 +08:00
|
|
|
ReallocPairsTy currMap = currState->get<ReallocPairs>();
|
|
|
|
ReallocPairsTy prevMap = prevState->get<ReallocPairs>();
|
2012-03-18 15:43:35 +08:00
|
|
|
|
2012-11-02 09:54:06 +08:00
|
|
|
for (ReallocPairsTy::iterator I = prevMap.begin(), E = prevMap.end();
|
2012-03-18 15:43:35 +08:00
|
|
|
I != E; ++I) {
|
|
|
|
SymbolRef sym = I.getKey();
|
|
|
|
if (!currMap.lookup(sym))
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-09 14:25:51 +08:00
|
|
|
PathDiagnosticPiece *
|
|
|
|
MallocChecker::MallocBugVisitor::VisitNode(const ExplodedNode *N,
|
|
|
|
const ExplodedNode *PrevN,
|
|
|
|
BugReporterContext &BRC,
|
|
|
|
BugReport &BR) {
|
2012-03-18 15:43:35 +08:00
|
|
|
ProgramStateRef state = N->getState();
|
|
|
|
ProgramStateRef statePrev = PrevN->getState();
|
|
|
|
|
|
|
|
const RefState *RS = state->get<RegionState>(Sym);
|
|
|
|
const RefState *RSPrev = statePrev->get<RegionState>(Sym);
|
2012-08-04 02:30:18 +08:00
|
|
|
if (!RS)
|
2012-02-09 14:25:51 +08:00
|
|
|
return 0;
|
|
|
|
|
2012-02-17 06:26:07 +08:00
|
|
|
const Stmt *S = 0;
|
|
|
|
const char *Msg = 0;
|
2012-03-17 07:24:20 +08:00
|
|
|
StackHintGeneratorForSymbol *StackHint = 0;
|
2012-02-17 06:26:07 +08:00
|
|
|
|
|
|
|
// Retrieve the associated statement.
|
|
|
|
ProgramPoint ProgLoc = N->getLocation();
|
2013-02-22 06:23:56 +08:00
|
|
|
if (Optional<StmtPoint> SP = ProgLoc.getAs<StmtPoint>()) {
|
2012-07-11 06:07:52 +08:00
|
|
|
S = SP->getStmt();
|
2013-02-22 06:23:56 +08:00
|
|
|
} else if (Optional<CallExitEnd> Exit = ProgLoc.getAs<CallExitEnd>()) {
|
2012-07-11 06:07:52 +08:00
|
|
|
S = Exit->getCalleeContext()->getCallSite();
|
2013-02-22 06:23:56 +08:00
|
|
|
} else if (Optional<BlockEdge> Edge = ProgLoc.getAs<BlockEdge>()) {
|
2013-01-05 03:04:36 +08:00
|
|
|
// If an assumption was made on a branch, it should be caught
|
|
|
|
// here by looking at the state transition.
|
|
|
|
S = Edge->getSrc()->getTerminator();
|
2012-02-17 06:26:07 +08:00
|
|
|
}
|
2013-01-05 03:04:36 +08:00
|
|
|
|
2012-02-17 06:26:07 +08:00
|
|
|
if (!S)
|
2012-02-09 14:25:51 +08:00
|
|
|
return 0;
|
|
|
|
|
2012-07-11 06:07:42 +08:00
|
|
|
// FIXME: We will eventually need to handle non-statement-based events
|
|
|
|
// (__attribute__((cleanup))).
|
|
|
|
|
2012-02-09 14:25:51 +08:00
|
|
|
// Find out if this is an interesting point and what is the kind.
|
2012-02-17 06:26:07 +08:00
|
|
|
if (Mode == Normal) {
|
2012-03-16 05:13:02 +08:00
|
|
|
if (isAllocated(RS, RSPrev, S)) {
|
2012-02-17 06:26:07 +08:00
|
|
|
Msg = "Memory is allocated";
|
2012-03-17 07:44:28 +08:00
|
|
|
StackHint = new StackHintGeneratorForSymbol(Sym,
|
|
|
|
"Returned allocated memory");
|
2012-03-16 05:13:02 +08:00
|
|
|
} else if (isReleased(RS, RSPrev, S)) {
|
2012-02-17 06:26:07 +08:00
|
|
|
Msg = "Memory is released";
|
2012-03-17 07:44:28 +08:00
|
|
|
StackHint = new StackHintGeneratorForSymbol(Sym,
|
2013-04-16 08:22:55 +08:00
|
|
|
"Returning; memory was released");
|
2012-06-22 10:04:31 +08:00
|
|
|
} else if (isRelinquished(RS, RSPrev, S)) {
|
2013-12-02 11:50:25 +08:00
|
|
|
Msg = "Memory ownership is transferred";
|
2012-06-22 10:04:31 +08:00
|
|
|
StackHint = new StackHintGeneratorForSymbol(Sym, "");
|
2012-03-16 05:13:02 +08:00
|
|
|
} else if (isReallocFailedCheck(RS, RSPrev, S)) {
|
2012-02-17 06:26:07 +08:00
|
|
|
Mode = ReallocationFailed;
|
|
|
|
Msg = "Reallocation failed";
|
2012-03-17 07:24:20 +08:00
|
|
|
StackHint = new StackHintGeneratorForReallocationFailed(Sym,
|
2012-03-17 07:44:28 +08:00
|
|
|
"Reallocation failed");
|
2012-03-18 15:43:35 +08:00
|
|
|
|
2012-03-24 11:15:09 +08:00
|
|
|
if (SymbolRef sym = findFailedReallocSymbol(state, statePrev)) {
|
|
|
|
// Is it possible to fail two reallocs WITHOUT testing in between?
|
|
|
|
assert((!FailedReallocSymbol || FailedReallocSymbol == sym) &&
|
|
|
|
"We only support one failed realloc at a time.");
|
2012-03-18 15:43:35 +08:00
|
|
|
BR.markInteresting(sym);
|
2012-03-24 11:15:09 +08:00
|
|
|
FailedReallocSymbol = sym;
|
|
|
|
}
|
2012-02-17 06:26:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We are in a special mode if a reallocation failed later in the path.
|
|
|
|
} else if (Mode == ReallocationFailed) {
|
2012-03-24 11:15:09 +08:00
|
|
|
assert(FailedReallocSymbol && "No symbol to look for.");
|
2012-02-17 06:26:07 +08:00
|
|
|
|
2012-03-24 11:15:09 +08:00
|
|
|
// Is this is the first appearance of the reallocated symbol?
|
|
|
|
if (!statePrev->get<RegionState>(FailedReallocSymbol)) {
|
|
|
|
// We're at the reallocation point.
|
|
|
|
Msg = "Attempt to reallocate memory";
|
|
|
|
StackHint = new StackHintGeneratorForSymbol(Sym,
|
|
|
|
"Returned reallocated memory");
|
|
|
|
FailedReallocSymbol = NULL;
|
|
|
|
Mode = Normal;
|
|
|
|
}
|
2012-02-17 06:26:07 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 14:25:51 +08:00
|
|
|
if (!Msg)
|
|
|
|
return 0;
|
2012-03-17 07:24:20 +08:00
|
|
|
assert(StackHint);
|
2012-02-09 14:25:51 +08:00
|
|
|
|
|
|
|
// Generate the extra diagnostic.
|
2012-02-17 06:26:07 +08:00
|
|
|
PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
|
2012-02-09 14:25:51 +08:00
|
|
|
N->getLocationContext());
|
2012-03-17 07:24:20 +08:00
|
|
|
return new PathDiagnosticEventPiece(Pos, Msg, true, StackHint);
|
2012-02-09 14:25:51 +08:00
|
|
|
}
|
|
|
|
|
2012-05-02 08:05:20 +08:00
|
|
|
void MallocChecker::printState(raw_ostream &Out, ProgramStateRef State,
|
|
|
|
const char *NL, const char *Sep) const {
|
|
|
|
|
|
|
|
RegionStateTy RS = State->get<RegionState>();
|
|
|
|
|
2013-01-03 09:30:12 +08:00
|
|
|
if (!RS.isEmpty()) {
|
|
|
|
Out << Sep << "MallocChecker:" << NL;
|
|
|
|
for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
|
|
|
|
I.getKey()->dumpToStream(Out);
|
|
|
|
Out << " : ";
|
|
|
|
I.getData().dump(Out);
|
|
|
|
Out << NL;
|
|
|
|
}
|
|
|
|
}
|
2012-05-02 08:05:20 +08:00
|
|
|
}
|
2012-02-09 14:25:51 +08:00
|
|
|
|
2013-04-16 08:22:55 +08:00
|
|
|
void ento::registerNewDeleteLeaksChecker(CheckerManager &mgr) {
|
|
|
|
registerCStringCheckerBasic(mgr);
|
|
|
|
mgr.registerChecker<MallocChecker>()->Filter.CNewDeleteLeaksChecker = true;
|
|
|
|
// We currently treat NewDeleteLeaks checker as a subchecker of NewDelete
|
|
|
|
// checker.
|
|
|
|
mgr.registerChecker<MallocChecker>()->Filter.CNewDeleteChecker = true;
|
|
|
|
}
|
2013-04-13 07:25:40 +08:00
|
|
|
|
2012-02-09 07:16:52 +08:00
|
|
|
#define REGISTER_CHECKER(name) \
|
|
|
|
void ento::register##name(CheckerManager &mgr) {\
|
2012-02-18 06:35:31 +08:00
|
|
|
registerCStringCheckerBasic(mgr); \
|
2012-02-09 07:16:52 +08:00
|
|
|
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)
|
2013-03-25 09:35:45 +08:00
|
|
|
REGISTER_CHECKER(NewDeleteChecker)
|
2013-03-29 01:05:19 +08:00
|
|
|
REGISTER_CHECKER(MismatchedDeallocatorChecker)
|