2008-03-16 02:59:48 +03:00
|
|
|
// CFRefCount.cpp - Transfer functions for tracking simple values -*- C++ -*--//
|
2008-03-06 03:08:09 +03:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-03-06 13:40:09 +03:00
|
|
|
// This file defines the methods for CFRefCount, which implements
|
2008-03-06 03:08:09 +03:00
|
|
|
// a reference count checker for Core Foundation (Mac OS X).
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
#include "GRSimpleVals.h"
|
2008-05-01 03:47:44 +04:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2008-03-06 03:08:09 +03:00
|
|
|
#include "clang/Analysis/PathSensitive/ValueState.h"
|
2008-03-31 22:26:32 +04:00
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
2008-03-06 03:08:09 +03:00
|
|
|
#include "clang/Analysis/LocalCheckers.h"
|
2008-04-09 05:10:13 +04:00
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/BugReporter.h"
|
2008-03-11 09:39:11 +03:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
|
|
|
#include "llvm/ADT/ImmutableMap.h"
|
2008-04-09 05:10:13 +04:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-03-11 22:44:10 +03:00
|
|
|
#include <ostream>
|
2008-04-18 08:55:01 +04:00
|
|
|
#include <sstream>
|
2008-03-06 03:08:09 +03:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) {
|
|
|
|
IdentifierInfo* II = &Ctx.Idents.get(name);
|
|
|
|
return Ctx.Selectors.getSelector(0, &II);
|
|
|
|
}
|
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Symbolic Evaluation of Reference Counting Logic
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
namespace {
|
|
|
|
enum ArgEffect { IncRef, DecRef, DoNothing };
|
2008-04-24 21:22:33 +04:00
|
|
|
typedef std::vector<std::pair<unsigned,ArgEffect> > ArgEffects;
|
2008-03-11 09:39:11 +03:00
|
|
|
}
|
2008-03-06 03:08:09 +03:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
namespace llvm {
|
|
|
|
template <> struct FoldingSetTrait<ArgEffects> {
|
2008-04-11 02:58:08 +04:00
|
|
|
static void Profile(const ArgEffects& X, FoldingSetNodeID& ID) {
|
2008-04-24 21:22:33 +04:00
|
|
|
for (ArgEffects::const_iterator I = X.begin(), E = X.end(); I!= E; ++I) {
|
|
|
|
ID.AddInteger(I->first);
|
|
|
|
ID.AddInteger((unsigned) I->second);
|
|
|
|
}
|
2008-04-11 02:58:08 +04:00
|
|
|
}
|
2008-03-11 09:39:11 +03:00
|
|
|
};
|
|
|
|
} // end llvm namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class RetEffect {
|
|
|
|
public:
|
2008-04-11 03:44:06 +04:00
|
|
|
enum Kind { NoRet = 0x0, Alias = 0x1, OwnedSymbol = 0x2,
|
|
|
|
NotOwnedSymbol = 0x3 };
|
2008-03-11 09:39:11 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned Data;
|
2008-04-11 02:58:08 +04:00
|
|
|
RetEffect(Kind k, unsigned D) { Data = (D << 2) | (unsigned) k; }
|
2008-03-11 09:39:11 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Kind getKind() const { return (Kind) (Data & 0x3); }
|
|
|
|
|
|
|
|
unsigned getValue() const {
|
|
|
|
assert(getKind() == Alias);
|
2008-04-11 02:58:08 +04:00
|
|
|
return Data >> 2;
|
2008-03-11 09:39:11 +03:00
|
|
|
}
|
2008-04-12 02:25:11 +04:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
static RetEffect MakeAlias(unsigned Idx) { return RetEffect(Alias, Idx); }
|
|
|
|
|
|
|
|
static RetEffect MakeOwned() { return RetEffect(OwnedSymbol, 0); }
|
|
|
|
|
|
|
|
static RetEffect MakeNotOwned() { return RetEffect(NotOwnedSymbol, 0); }
|
|
|
|
|
2008-04-11 03:44:06 +04:00
|
|
|
static RetEffect MakeNoRet() { return RetEffect(NoRet, 0); }
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
operator Kind() const { return getKind(); }
|
|
|
|
|
|
|
|
void Profile(llvm::FoldingSetNodeID& ID) const { ID.AddInteger(Data); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class CFRefSummary : public llvm::FoldingSetNode {
|
|
|
|
ArgEffects* Args;
|
|
|
|
RetEffect Ret;
|
|
|
|
public:
|
|
|
|
|
|
|
|
CFRefSummary(ArgEffects* A, RetEffect R) : Args(A), Ret(R) {}
|
|
|
|
|
|
|
|
unsigned getNumArgs() const { return Args->size(); }
|
|
|
|
|
2008-03-11 20:48:22 +03:00
|
|
|
ArgEffect getArg(unsigned idx) const {
|
2008-04-24 21:22:33 +04:00
|
|
|
if (!Args)
|
|
|
|
return DoNothing;
|
|
|
|
|
|
|
|
// If Args is present, it is likely to contain only 1 element.
|
|
|
|
// Just do a linear search. Do it from the back because functions with
|
|
|
|
// large numbers of arguments will be tail heavy with respect to which
|
|
|
|
// argument they actually modify with respect to the reference count.
|
|
|
|
|
|
|
|
for (ArgEffects::reverse_iterator I=Args->rbegin(), E=Args->rend();
|
|
|
|
I!=E; ++I) {
|
|
|
|
|
|
|
|
if (idx > I->first)
|
|
|
|
return DoNothing;
|
|
|
|
|
|
|
|
if (idx == I->first)
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DoNothing;
|
2008-03-11 20:48:22 +03:00
|
|
|
}
|
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
RetEffect getRet() const {
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
typedef ArgEffects::const_iterator arg_iterator;
|
|
|
|
|
|
|
|
arg_iterator begin_args() const { return Args->begin(); }
|
|
|
|
arg_iterator end_args() const { return Args->end(); }
|
2008-03-06 03:08:09 +03:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects* A, RetEffect R) {
|
|
|
|
ID.AddPointer(A);
|
|
|
|
ID.Add(R);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
Profile(ID, Args, Ret);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-03-06 03:08:09 +03:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
class CFRefSummaryManager {
|
|
|
|
typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<ArgEffects> > AESetTy;
|
|
|
|
typedef llvm::FoldingSet<CFRefSummary> SummarySetTy;
|
|
|
|
typedef llvm::DenseMap<FunctionDecl*, CFRefSummary*> SummaryMapTy;
|
|
|
|
|
2008-04-29 09:33:51 +04:00
|
|
|
ASTContext& Ctx;
|
|
|
|
const bool GCEnabled;
|
|
|
|
|
2008-04-11 02:58:08 +04:00
|
|
|
SummarySetTy SummarySet;
|
|
|
|
SummaryMapTy SummaryMap;
|
|
|
|
AESetTy AESet;
|
|
|
|
llvm::BumpPtrAllocator BPAlloc;
|
2008-04-24 21:22:33 +04:00
|
|
|
ArgEffects ScratchArgs;
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
ArgEffects* getArgEffects();
|
|
|
|
|
2008-04-29 09:33:51 +04:00
|
|
|
enum CFUnaryFunc { cfretain, cfrelease, cfmakecollectable };
|
|
|
|
CFRefSummary* getUnaryCFSummary(FunctionTypeProto* FT, CFUnaryFunc func);
|
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
CFRefSummary* getCFSummary(FunctionDecl* FD, const char* FName);
|
|
|
|
|
|
|
|
CFRefSummary* getCFSummaryCreateRule(FunctionTypeProto* FT);
|
|
|
|
CFRefSummary* getCFSummaryGetRule(FunctionTypeProto* FT);
|
|
|
|
|
|
|
|
CFRefSummary* getPersistentSummary(ArgEffects* AE, RetEffect RE);
|
2008-04-24 21:22:33 +04:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
public:
|
2008-04-29 09:33:51 +04:00
|
|
|
CFRefSummaryManager(ASTContext& ctx, bool gcenabled)
|
|
|
|
: Ctx(ctx), GCEnabled(gcenabled) {}
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
~CFRefSummaryManager();
|
2008-03-06 03:08:09 +03:00
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
CFRefSummary* getSummary(FunctionDecl* FD, ASTContext& Ctx);
|
2008-03-11 09:39:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Implementation of checker data structures.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
CFRefSummaryManager::~CFRefSummaryManager() {
|
2008-03-06 03:08:09 +03:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
// FIXME: The ArgEffects could eventually be allocated from BPAlloc,
|
|
|
|
// mitigating the need to do explicit cleanup of the
|
|
|
|
// Argument-Effect summaries.
|
2008-03-06 03:08:09 +03:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
for (AESetTy::iterator I = AESet.begin(), E = AESet.end(); I!=E; ++I)
|
|
|
|
I->getValue().~ArgEffects();
|
2008-03-06 03:08:09 +03:00
|
|
|
}
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
ArgEffects* CFRefSummaryManager::getArgEffects() {
|
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
if (ScratchArgs.empty())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Compute a profile for a non-empty ScratchArgs.
|
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
llvm::FoldingSetNodeID profile;
|
2008-04-24 21:22:33 +04:00
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
profile.Add(ScratchArgs);
|
|
|
|
void* InsertPos;
|
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
// Look up the uniqued copy, or create a new one.
|
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
llvm::FoldingSetNodeWrapper<ArgEffects>* E =
|
|
|
|
AESet.FindNodeOrInsertPos(profile, InsertPos);
|
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
if (E) {
|
2008-03-12 04:21:45 +03:00
|
|
|
ScratchArgs.clear();
|
|
|
|
return &E->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
E = (llvm::FoldingSetNodeWrapper<ArgEffects>*)
|
|
|
|
BPAlloc.Allocate<llvm::FoldingSetNodeWrapper<ArgEffects> >();
|
|
|
|
|
|
|
|
new (E) llvm::FoldingSetNodeWrapper<ArgEffects>(ScratchArgs);
|
|
|
|
AESet.InsertNode(E, InsertPos);
|
|
|
|
|
|
|
|
ScratchArgs.clear();
|
|
|
|
return &E->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
CFRefSummary* CFRefSummaryManager::getPersistentSummary(ArgEffects* AE,
|
|
|
|
RetEffect RE) {
|
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
// Generate a profile for the summary.
|
2008-03-12 04:21:45 +03:00
|
|
|
llvm::FoldingSetNodeID profile;
|
|
|
|
CFRefSummary::Profile(profile, AE, RE);
|
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
// Look up the uniqued summary, or create one if it doesn't exist.
|
|
|
|
void* InsertPos;
|
2008-03-12 04:21:45 +03:00
|
|
|
CFRefSummary* Summ = SummarySet.FindNodeOrInsertPos(profile, InsertPos);
|
|
|
|
|
|
|
|
if (Summ)
|
|
|
|
return Summ;
|
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
// Create the summary and return it.
|
2008-03-12 04:21:45 +03:00
|
|
|
Summ = (CFRefSummary*) BPAlloc.Allocate<CFRefSummary>();
|
|
|
|
new (Summ) CFRefSummary(AE, RE);
|
|
|
|
SummarySet.InsertNode(Summ, InsertPos);
|
|
|
|
|
|
|
|
return Summ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CFRefSummary* CFRefSummaryManager::getSummary(FunctionDecl* FD,
|
|
|
|
ASTContext& Ctx) {
|
|
|
|
|
|
|
|
SourceLocation Loc = FD->getLocation();
|
|
|
|
|
|
|
|
if (!Loc.isFileID())
|
|
|
|
return NULL;
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
|
|
|
|
// Look up a summary in our cache of FunctionDecls -> Summaries.
|
|
|
|
SummaryMapTy::iterator I = SummaryMap.find(FD);
|
|
|
|
|
|
|
|
if (I != SummaryMap.end())
|
|
|
|
return I->second;
|
|
|
|
|
|
|
|
// No summary. Generate one.
|
2008-03-12 04:21:45 +03:00
|
|
|
const char* FName = FD->getIdentifier()->getName();
|
|
|
|
|
|
|
|
if (FName[0] == 'C' && FName[1] == 'F') {
|
|
|
|
CFRefSummary* S = getCFSummary(FD, FName);
|
|
|
|
SummaryMap[FD] = S;
|
|
|
|
return S;
|
|
|
|
}
|
2008-04-24 21:22:33 +04:00
|
|
|
|
|
|
|
// Function has no ref-count effects. Return the NULL summary.
|
2008-03-11 09:39:11 +03:00
|
|
|
return NULL;
|
2008-03-06 03:08:09 +03:00
|
|
|
}
|
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
CFRefSummary* CFRefSummaryManager::getCFSummary(FunctionDecl* FD,
|
|
|
|
const char* FName) {
|
|
|
|
|
|
|
|
// For now, only generate summaries for functions that have a prototype.
|
|
|
|
|
|
|
|
FunctionTypeProto* FT =
|
|
|
|
dyn_cast<FunctionTypeProto>(FD->getType().getTypePtr());
|
|
|
|
|
|
|
|
if (!FT)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
FName += 2;
|
|
|
|
|
|
|
|
if (strcmp(FName, "Retain") == 0)
|
2008-04-29 09:33:51 +04:00
|
|
|
return getUnaryCFSummary(FT, cfretain);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
if (strcmp(FName, "Release") == 0)
|
2008-04-29 09:33:51 +04:00
|
|
|
return getUnaryCFSummary(FT, cfrelease);
|
|
|
|
|
|
|
|
if (strcmp(FName, "MakeCollectable") == 0)
|
|
|
|
return getUnaryCFSummary(FT, cfmakecollectable);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
assert (ScratchArgs.empty());
|
|
|
|
bool usesCreateRule = false;
|
|
|
|
|
|
|
|
if (strstr(FName, "Create"))
|
|
|
|
usesCreateRule = true;
|
|
|
|
|
|
|
|
if (!usesCreateRule && strstr(FName, "Copy"))
|
|
|
|
usesCreateRule = true;
|
|
|
|
|
|
|
|
if (usesCreateRule)
|
|
|
|
return getCFSummaryCreateRule(FT);
|
|
|
|
|
|
|
|
if (strstr(FName, "Get"))
|
|
|
|
return getCFSummaryGetRule(FT);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-04-29 09:33:51 +04:00
|
|
|
CFRefSummary*
|
|
|
|
CFRefSummaryManager::getUnaryCFSummary(FunctionTypeProto* FT, CFUnaryFunc func) {
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
if (FT->getNumArgs() != 1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
TypedefType* ArgT = dyn_cast<TypedefType>(FT->getArgType(0).getTypePtr());
|
|
|
|
|
|
|
|
if (!ArgT)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// For CFRetain/CFRelease, the first (and only) argument is of type
|
|
|
|
// "CFTypeRef".
|
|
|
|
|
|
|
|
const char* TDName = ArgT->getDecl()->getIdentifier()->getName();
|
|
|
|
assert (TDName);
|
|
|
|
|
2008-04-11 02:58:08 +04:00
|
|
|
if (strcmp("CFTypeRef", TDName) != 0)
|
2008-03-12 04:21:45 +03:00
|
|
|
return NULL;
|
|
|
|
|
2008-05-01 00:17:27 +04:00
|
|
|
if (!ArgT->isPointerType())
|
|
|
|
return NULL;
|
2008-04-11 02:58:08 +04:00
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
QualType RetTy = FT->getResultType();
|
|
|
|
|
2008-04-29 09:33:51 +04:00
|
|
|
switch (func) {
|
|
|
|
case cfretain: {
|
|
|
|
|
|
|
|
// CFRetain: the return type should also be "CFTypeRef".
|
|
|
|
if (RetTy.getTypePtr() != ArgT)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// The function's interface checks out. Generate a canned summary.
|
|
|
|
assert (ScratchArgs.empty());
|
|
|
|
ScratchArgs.push_back(std::make_pair(0, IncRef));
|
|
|
|
return getPersistentSummary(getArgEffects(), RetEffect::MakeAlias(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
case cfrelease: {
|
|
|
|
|
|
|
|
// CFRelease: the return type should be void.
|
|
|
|
|
|
|
|
if (RetTy != Ctx.VoidTy)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
assert (ScratchArgs.empty());
|
|
|
|
ScratchArgs.push_back(std::make_pair(0, DecRef));
|
|
|
|
return getPersistentSummary(getArgEffects(), RetEffect::MakeNoRet());
|
|
|
|
}
|
|
|
|
|
|
|
|
case cfmakecollectable: {
|
|
|
|
|
|
|
|
// CFRetain: the return type should also be "CFTypeRef".
|
|
|
|
if (RetTy.getTypePtr() != ArgT)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// The function's interface checks out. Generate a canned summary.
|
|
|
|
assert (ScratchArgs.empty());
|
|
|
|
|
|
|
|
if (GCEnabled)
|
|
|
|
ScratchArgs.push_back(std::make_pair(0, DecRef));
|
|
|
|
|
|
|
|
return getPersistentSummary(getArgEffects(), RetEffect::MakeAlias(0));
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert (false && "Not a support unary function.");
|
2008-04-11 03:44:06 +04:00
|
|
|
}
|
2008-03-12 04:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isCFRefType(QualType T) {
|
|
|
|
|
|
|
|
if (!T->isPointerType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check the typedef for the name "CF" and the substring "Ref".
|
|
|
|
|
|
|
|
TypedefType* TD = dyn_cast<TypedefType>(T.getTypePtr());
|
|
|
|
|
|
|
|
if (!TD)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const char* TDName = TD->getDecl()->getIdentifier()->getName();
|
|
|
|
assert (TDName);
|
|
|
|
|
|
|
|
if (TDName[0] != 'C' || TDName[1] != 'F')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (strstr(TDName, "Ref") == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFRefSummary*
|
|
|
|
CFRefSummaryManager::getCFSummaryCreateRule(FunctionTypeProto* FT) {
|
|
|
|
|
|
|
|
if (!isCFRefType(FT->getResultType()))
|
2008-04-12 00:11:19 +04:00
|
|
|
return NULL;
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
// FIXME: Add special-cases for functions that retain/release. For now
|
|
|
|
// just handle the default case.
|
2008-04-24 21:22:33 +04:00
|
|
|
|
|
|
|
assert (ScratchArgs.empty());
|
2008-03-12 04:21:45 +03:00
|
|
|
return getPersistentSummary(getArgEffects(), RetEffect::MakeOwned());
|
|
|
|
}
|
|
|
|
|
|
|
|
CFRefSummary*
|
|
|
|
CFRefSummaryManager::getCFSummaryGetRule(FunctionTypeProto* FT) {
|
|
|
|
|
2008-04-12 00:11:19 +04:00
|
|
|
QualType RetTy = FT->getResultType();
|
|
|
|
|
|
|
|
// FIXME: For now we assume that all pointer types returned are referenced
|
|
|
|
// counted. Since this is the "Get" rule, we assume non-ownership, which
|
|
|
|
// works fine for things that are not reference counted. We do this because
|
|
|
|
// some generic data structures return "void*". We need something better
|
|
|
|
// in the future.
|
|
|
|
|
|
|
|
if (!isCFRefType(RetTy) && !RetTy->isPointerType())
|
|
|
|
return NULL;
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
// FIXME: Add special-cases for functions that retain/release. For now
|
|
|
|
// just handle the default case.
|
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
assert (ScratchArgs.empty());
|
2008-03-12 04:21:45 +03:00
|
|
|
return getPersistentSummary(getArgEffects(), RetEffect::MakeNotOwned());
|
|
|
|
}
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-17 00:40:59 +04:00
|
|
|
// Reference-counting logic (typestate + counts).
|
2008-03-11 09:39:11 +03:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
class VISIBILITY_HIDDEN RefVal {
|
2008-04-17 22:12:53 +04:00
|
|
|
public:
|
2008-03-11 20:48:22 +03:00
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
enum Kind {
|
|
|
|
Owned = 0, // Owning reference.
|
|
|
|
NotOwned, // Reference is not owned by still valid (not freed).
|
|
|
|
Released, // Object has been released.
|
|
|
|
ReturnedOwned, // Returned object passes ownership to caller.
|
|
|
|
ReturnedNotOwned, // Return object does not pass ownership to caller.
|
|
|
|
ErrorUseAfterRelease, // Object used after released.
|
|
|
|
ErrorReleaseNotOwned, // Release of an object that was not owned.
|
|
|
|
ErrorLeak // A memory leak due to excessive reference counts.
|
|
|
|
};
|
2008-03-11 20:48:22 +03:00
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
private:
|
|
|
|
|
|
|
|
Kind kind;
|
|
|
|
unsigned Cnt;
|
|
|
|
|
|
|
|
RefVal(Kind k, unsigned cnt) : kind(k), Cnt(cnt) {}
|
|
|
|
|
|
|
|
RefVal(Kind k) : kind(k), Cnt(0) {}
|
2008-03-11 20:48:22 +03:00
|
|
|
|
|
|
|
public:
|
2008-04-17 02:32:20 +04:00
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
Kind getKind() const { return kind; }
|
2008-03-11 20:48:22 +03:00
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
unsigned getCount() const { return Cnt; }
|
|
|
|
|
|
|
|
// Useful predicates.
|
2008-03-11 20:48:22 +03:00
|
|
|
|
2008-03-11 21:14:09 +03:00
|
|
|
static bool isError(Kind k) { return k >= ErrorUseAfterRelease; }
|
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
static bool isLeak(Kind k) { return k == ErrorLeak; }
|
|
|
|
|
2008-04-12 02:25:11 +04:00
|
|
|
bool isOwned() const {
|
|
|
|
return getKind() == Owned;
|
|
|
|
}
|
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
bool isNotOwned() const {
|
|
|
|
return getKind() == NotOwned;
|
|
|
|
}
|
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
bool isReturnedOwned() const {
|
|
|
|
return getKind() == ReturnedOwned;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isReturnedNotOwned() const {
|
|
|
|
return getKind() == ReturnedNotOwned;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isNonLeakError() const {
|
|
|
|
Kind k = getKind();
|
|
|
|
return isError(k) && !isLeak(k);
|
|
|
|
}
|
|
|
|
|
|
|
|
// State creation: normal state.
|
|
|
|
|
2008-04-11 03:09:18 +04:00
|
|
|
static RefVal makeOwned(unsigned Count = 0) {
|
|
|
|
return RefVal(Owned, Count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static RefVal makeNotOwned(unsigned Count = 0) {
|
|
|
|
return RefVal(NotOwned, Count);
|
|
|
|
}
|
2008-04-17 22:12:53 +04:00
|
|
|
|
|
|
|
static RefVal makeReturnedOwned(unsigned Count) {
|
|
|
|
return RefVal(ReturnedOwned, Count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static RefVal makeReturnedNotOwned() {
|
|
|
|
return RefVal(ReturnedNotOwned);
|
|
|
|
}
|
|
|
|
|
|
|
|
// State creation: errors.
|
2008-04-11 03:09:18 +04:00
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
static RefVal makeLeak() { return RefVal(ErrorLeak); }
|
2008-03-11 20:48:22 +03:00
|
|
|
static RefVal makeReleased() { return RefVal(Released); }
|
|
|
|
static RefVal makeUseAfterRelease() { return RefVal(ErrorUseAfterRelease); }
|
|
|
|
static RefVal makeReleaseNotOwned() { return RefVal(ErrorReleaseNotOwned); }
|
2008-04-17 22:12:53 +04:00
|
|
|
|
|
|
|
// Comparison, profiling, and pretty-printing.
|
2008-03-11 20:48:22 +03:00
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
bool operator==(const RefVal& X) const {
|
|
|
|
return kind == X.kind && Cnt == X.Cnt;
|
|
|
|
}
|
2008-03-11 22:44:10 +03:00
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
void Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
ID.AddInteger((unsigned) kind);
|
|
|
|
ID.AddInteger(Cnt);
|
|
|
|
}
|
|
|
|
|
2008-03-11 22:44:10 +03:00
|
|
|
void print(std::ostream& Out) const;
|
2008-03-11 20:48:22 +03:00
|
|
|
};
|
2008-03-11 22:44:10 +03:00
|
|
|
|
|
|
|
void RefVal::print(std::ostream& Out) const {
|
|
|
|
switch (getKind()) {
|
|
|
|
default: assert(false);
|
2008-04-11 03:09:18 +04:00
|
|
|
case Owned: {
|
|
|
|
Out << "Owned";
|
|
|
|
unsigned cnt = getCount();
|
|
|
|
if (cnt) Out << " (+ " << cnt << ")";
|
2008-03-11 22:44:10 +03:00
|
|
|
break;
|
2008-04-11 03:09:18 +04:00
|
|
|
}
|
2008-03-11 22:44:10 +03:00
|
|
|
|
2008-04-11 03:09:18 +04:00
|
|
|
case NotOwned: {
|
2008-04-17 22:12:53 +04:00
|
|
|
Out << "NotOwned";
|
|
|
|
unsigned cnt = getCount();
|
|
|
|
if (cnt) Out << " (+ " << cnt << ")";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ReturnedOwned: {
|
|
|
|
Out << "ReturnedOwned";
|
2008-04-11 03:09:18 +04:00
|
|
|
unsigned cnt = getCount();
|
|
|
|
if (cnt) Out << " (+ " << cnt << ")";
|
2008-03-11 22:44:10 +03:00
|
|
|
break;
|
2008-04-11 03:09:18 +04:00
|
|
|
}
|
2008-03-11 22:44:10 +03:00
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
case ReturnedNotOwned: {
|
|
|
|
Out << "ReturnedNotOwned";
|
|
|
|
unsigned cnt = getCount();
|
|
|
|
if (cnt) Out << " (+ " << cnt << ")";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-03-11 22:44:10 +03:00
|
|
|
case Released:
|
|
|
|
Out << "Released";
|
|
|
|
break;
|
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
case ErrorLeak:
|
|
|
|
Out << "Leaked";
|
|
|
|
break;
|
|
|
|
|
2008-03-11 22:44:10 +03:00
|
|
|
case ErrorUseAfterRelease:
|
|
|
|
Out << "Use-After-Release [ERROR]";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ErrorReleaseNotOwned:
|
|
|
|
Out << "Release of Not-Owned [ERROR]";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-03-11 20:48:22 +03:00
|
|
|
|
2008-04-17 00:40:59 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals {
|
2008-04-18 07:39:05 +04:00
|
|
|
public:
|
2008-03-11 22:44:10 +03:00
|
|
|
// Type definitions.
|
|
|
|
|
2008-03-11 20:48:22 +03:00
|
|
|
typedef llvm::ImmutableMap<SymbolID, RefVal> RefBindings;
|
2008-03-11 09:39:11 +03:00
|
|
|
typedef RefBindings::Factory RefBFactoryTy;
|
2008-03-11 21:14:09 +03:00
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
typedef llvm::DenseMap<GRExprEngine::NodeTy*,std::pair<Expr*, SymbolID> >
|
|
|
|
ReleasesNotOwnedTy;
|
2008-03-11 21:14:09 +03:00
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
typedef ReleasesNotOwnedTy UseAfterReleasesTy;
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<GRExprEngine::NodeTy*, std::vector<SymbolID>*>
|
|
|
|
LeaksTy;
|
|
|
|
|
2008-03-11 22:44:10 +03:00
|
|
|
class BindingsPrinter : public ValueState::CheckerStatePrinter {
|
|
|
|
public:
|
|
|
|
virtual void PrintCheckerState(std::ostream& Out, void* State,
|
|
|
|
const char* nl, const char* sep);
|
|
|
|
};
|
2008-04-18 07:39:05 +04:00
|
|
|
|
|
|
|
private:
|
2008-03-11 22:44:10 +03:00
|
|
|
// Instance variables.
|
|
|
|
|
2008-04-29 09:13:59 +04:00
|
|
|
CFRefSummaryManager Summaries;
|
2008-05-01 03:47:44 +04:00
|
|
|
const bool GCEnabled;
|
|
|
|
const LangOptions& LOpts;
|
2008-03-12 04:21:45 +03:00
|
|
|
RefBFactoryTy RefBFactory;
|
|
|
|
|
2008-03-11 21:14:09 +03:00
|
|
|
UseAfterReleasesTy UseAfterReleases;
|
|
|
|
ReleasesNotOwnedTy ReleasesNotOwned;
|
2008-04-17 02:32:20 +04:00
|
|
|
LeaksTy Leaks;
|
2008-03-11 21:14:09 +03:00
|
|
|
|
2008-03-11 22:44:10 +03:00
|
|
|
BindingsPrinter Printer;
|
|
|
|
|
2008-04-16 08:28:53 +04:00
|
|
|
Selector RetainSelector;
|
|
|
|
Selector ReleaseSelector;
|
2008-05-01 06:18:37 +04:00
|
|
|
Selector AutoreleaseSelector;
|
2008-04-16 08:28:53 +04:00
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
public:
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
static RefBindings GetRefBindings(ValueState& StImpl) {
|
|
|
|
return RefBindings((RefBindings::TreeTy*) StImpl.CheckerState);
|
|
|
|
}
|
2008-04-16 08:28:53 +04:00
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
private:
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
static void SetRefBindings(ValueState& StImpl, RefBindings B) {
|
|
|
|
StImpl.CheckerState = B.getRoot();
|
|
|
|
}
|
2008-04-16 08:28:53 +04:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
RefBindings Remove(RefBindings B, SymbolID sym) {
|
|
|
|
return RefBFactory.Remove(B, sym);
|
|
|
|
}
|
|
|
|
|
2008-03-11 20:48:22 +03:00
|
|
|
RefBindings Update(RefBindings B, SymbolID sym, RefVal V, ArgEffect E,
|
2008-04-16 08:28:53 +04:00
|
|
|
RefVal::Kind& hasErr);
|
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
void ProcessNonLeakError(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
Expr* NodeExpr, Expr* ErrorExpr,
|
|
|
|
ExplodedNode<ValueState>* Pred,
|
|
|
|
ValueState* St,
|
2008-04-18 07:39:05 +04:00
|
|
|
RefVal::Kind hasErr, SymbolID Sym);
|
2008-04-17 02:32:20 +04:00
|
|
|
|
|
|
|
ValueState* HandleSymbolDeath(ValueStateManager& VMgr, ValueState* St,
|
|
|
|
SymbolID sid, RefVal V, bool& hasLeak);
|
|
|
|
|
|
|
|
ValueState* NukeBinding(ValueStateManager& VMgr, ValueState* St,
|
|
|
|
SymbolID sid);
|
2008-03-11 09:39:11 +03:00
|
|
|
|
|
|
|
public:
|
2008-04-17 00:40:59 +04:00
|
|
|
|
2008-05-01 03:47:44 +04:00
|
|
|
CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts)
|
2008-04-29 09:33:51 +04:00
|
|
|
: Summaries(Ctx, gcenabled),
|
2008-04-29 09:13:59 +04:00
|
|
|
GCEnabled(gcenabled),
|
2008-05-01 03:47:44 +04:00
|
|
|
LOpts(lopts),
|
2008-04-16 08:28:53 +04:00
|
|
|
RetainSelector(GetUnarySelector("retain", Ctx)),
|
2008-05-01 06:18:37 +04:00
|
|
|
ReleaseSelector(GetUnarySelector("release", Ctx)),
|
|
|
|
AutoreleaseSelector(GetUnarySelector("autorelease", Ctx)) {}
|
2008-04-16 08:28:53 +04:00
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
virtual ~CFRefCount() {
|
|
|
|
for (LeaksTy::iterator I = Leaks.begin(), E = Leaks.end(); I!=E; ++I)
|
|
|
|
delete I->second;
|
|
|
|
}
|
2008-04-10 03:49:11 +04:00
|
|
|
|
|
|
|
virtual void RegisterChecks(GRExprEngine& Eng);
|
2008-03-11 22:44:10 +03:00
|
|
|
|
|
|
|
virtual ValueState::CheckerStatePrinter* getCheckerStatePrinter() {
|
|
|
|
return &Printer;
|
|
|
|
}
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-05-01 03:47:44 +04:00
|
|
|
bool isGCEnabled() const { return GCEnabled; }
|
|
|
|
const LangOptions& getLangOptions() const { return LOpts; }
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
// Calls.
|
|
|
|
|
|
|
|
virtual void EvalCall(ExplodedNodeSet<ValueState>& Dst,
|
2008-03-13 00:06:49 +03:00
|
|
|
GRExprEngine& Eng,
|
2008-03-11 09:39:11 +03:00
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
2008-04-24 00:12:28 +04:00
|
|
|
CallExpr* CE, RVal L,
|
2008-03-11 09:39:11 +03:00
|
|
|
ExplodedNode<ValueState>* Pred);
|
2008-04-09 05:10:13 +04:00
|
|
|
|
2008-04-16 03:44:31 +04:00
|
|
|
virtual void EvalObjCMessageExpr(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Engine,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
ObjCMessageExpr* ME,
|
|
|
|
ExplodedNode<ValueState>* Pred);
|
|
|
|
|
|
|
|
bool EvalObjCMessageExprAux(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Engine,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
ObjCMessageExpr* ME,
|
|
|
|
ExplodedNode<ValueState>* Pred);
|
|
|
|
|
2008-04-17 00:40:59 +04:00
|
|
|
// Stores.
|
|
|
|
|
|
|
|
virtual void EvalStore(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Engine,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
Expr* E, ExplodedNode<ValueState>* Pred,
|
|
|
|
ValueState* St, RVal TargetLV, RVal Val);
|
2008-04-12 02:25:11 +04:00
|
|
|
// End-of-path.
|
|
|
|
|
|
|
|
virtual void EvalEndPath(GRExprEngine& Engine,
|
|
|
|
GREndPathNodeBuilder<ValueState>& Builder);
|
|
|
|
|
2008-04-25 03:57:27 +04:00
|
|
|
virtual void EvalDeadSymbols(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Engine,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
2008-04-25 05:25:15 +04:00
|
|
|
ExplodedNode<ValueState>* Pred,
|
|
|
|
Stmt* S,
|
2008-04-25 03:57:27 +04:00
|
|
|
ValueState* St,
|
|
|
|
const ValueStateManager::DeadSymbolsTy& Dead);
|
2008-04-17 22:12:53 +04:00
|
|
|
// Return statements.
|
|
|
|
|
|
|
|
virtual void EvalReturn(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Engine,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
ReturnStmt* S,
|
|
|
|
ExplodedNode<ValueState>* Pred);
|
2008-04-18 23:23:43 +04:00
|
|
|
|
|
|
|
// Assumptions.
|
|
|
|
|
|
|
|
virtual ValueState* EvalAssume(GRExprEngine& Engine, ValueState* St,
|
|
|
|
RVal Cond, bool Assumption, bool& isFeasible);
|
|
|
|
|
2008-04-09 05:10:13 +04:00
|
|
|
// Error iterators.
|
|
|
|
|
|
|
|
typedef UseAfterReleasesTy::iterator use_after_iterator;
|
|
|
|
typedef ReleasesNotOwnedTy::iterator bad_release_iterator;
|
2008-04-18 03:43:50 +04:00
|
|
|
typedef LeaksTy::iterator leaks_iterator;
|
2008-04-09 05:10:13 +04:00
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
use_after_iterator use_after_begin() { return UseAfterReleases.begin(); }
|
|
|
|
use_after_iterator use_after_end() { return UseAfterReleases.end(); }
|
2008-04-09 05:10:13 +04:00
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
bad_release_iterator bad_release_begin() { return ReleasesNotOwned.begin(); }
|
|
|
|
bad_release_iterator bad_release_end() { return ReleasesNotOwned.end(); }
|
2008-04-18 03:43:50 +04:00
|
|
|
|
|
|
|
leaks_iterator leaks_begin() { return Leaks.begin(); }
|
|
|
|
leaks_iterator leaks_end() { return Leaks.end(); }
|
2008-03-11 09:39:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
|
|
|
|
|
2008-03-11 22:44:10 +03:00
|
|
|
void CFRefCount::BindingsPrinter::PrintCheckerState(std::ostream& Out,
|
|
|
|
void* State, const char* nl,
|
|
|
|
const char* sep) {
|
|
|
|
RefBindings B((RefBindings::TreeTy*) State);
|
|
|
|
|
|
|
|
if (State)
|
|
|
|
Out << sep << nl;
|
|
|
|
|
|
|
|
for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
|
|
|
|
Out << (*I).first << " : ";
|
|
|
|
(*I).second.print(Out);
|
|
|
|
Out << nl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-12 00:23:24 +04:00
|
|
|
static inline ArgEffect GetArgE(CFRefSummary* Summ, unsigned idx) {
|
|
|
|
return Summ ? Summ->getArg(idx) : DoNothing;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline RetEffect GetRetE(CFRefSummary* Summ) {
|
|
|
|
return Summ ? Summ->getRet() : RetEffect::MakeNoRet();
|
|
|
|
}
|
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
void CFRefCount::ProcessNonLeakError(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
Expr* NodeExpr, Expr* ErrorExpr,
|
|
|
|
ExplodedNode<ValueState>* Pred,
|
|
|
|
ValueState* St,
|
2008-04-18 07:39:05 +04:00
|
|
|
RefVal::Kind hasErr, SymbolID Sym) {
|
2008-04-16 08:28:53 +04:00
|
|
|
Builder.BuildSinks = true;
|
|
|
|
GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, NodeExpr, Pred, St);
|
|
|
|
|
|
|
|
if (!N) return;
|
|
|
|
|
|
|
|
switch (hasErr) {
|
|
|
|
default: assert(false);
|
|
|
|
case RefVal::ErrorUseAfterRelease:
|
2008-04-18 07:39:05 +04:00
|
|
|
UseAfterReleases[N] = std::make_pair(ErrorExpr, Sym);
|
2008-04-16 08:28:53 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RefVal::ErrorReleaseNotOwned:
|
2008-04-18 07:39:05 +04:00
|
|
|
ReleasesNotOwned[N] = std::make_pair(ErrorExpr, Sym);
|
2008-04-16 08:28:53 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-06 03:08:09 +03:00
|
|
|
void CFRefCount::EvalCall(ExplodedNodeSet<ValueState>& Dst,
|
2008-03-13 00:06:49 +03:00
|
|
|
GRExprEngine& Eng,
|
2008-03-12 04:21:45 +03:00
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
2008-04-24 00:12:28 +04:00
|
|
|
CallExpr* CE, RVal L,
|
2008-03-12 04:21:45 +03:00
|
|
|
ExplodedNode<ValueState>* Pred) {
|
|
|
|
|
2008-03-13 00:06:49 +03:00
|
|
|
ValueStateManager& StateMgr = Eng.getStateManager();
|
2008-03-06 03:08:09 +03:00
|
|
|
|
2008-04-14 21:45:13 +04:00
|
|
|
CFRefSummary* Summ = NULL;
|
2008-03-06 03:08:09 +03:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
// Get the summary.
|
2008-03-06 03:08:09 +03:00
|
|
|
|
2008-04-14 21:45:13 +04:00
|
|
|
if (isa<lval::FuncVal>(L)) {
|
|
|
|
lval::FuncVal FV = cast<lval::FuncVal>(L);
|
|
|
|
FunctionDecl* FD = FV.getDecl();
|
|
|
|
Summ = Summaries.getSummary(FD, Eng.getContext());
|
|
|
|
}
|
2008-03-06 03:08:09 +03:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
// Get the state.
|
|
|
|
|
|
|
|
ValueState* St = Builder.GetState(Pred);
|
|
|
|
|
|
|
|
// Evaluate the effects of the call.
|
|
|
|
|
|
|
|
ValueState StVals = *St;
|
2008-04-16 08:28:53 +04:00
|
|
|
RefVal::Kind hasErr = (RefVal::Kind) 0;
|
2008-04-12 00:23:24 +04:00
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
// This function has a summary. Evaluate the effect of the arguments.
|
|
|
|
|
|
|
|
unsigned idx = 0;
|
|
|
|
|
2008-04-11 22:40:51 +04:00
|
|
|
Expr* ErrorExpr = NULL;
|
2008-04-18 07:39:05 +04:00
|
|
|
SymbolID ErrorSym = 0;
|
2008-04-11 22:40:51 +04:00
|
|
|
|
|
|
|
for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
|
|
|
|
I != E; ++I, ++idx) {
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
RVal V = StateMgr.GetRVal(St, *I);
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
if (isa<lval::SymbolVal>(V)) {
|
|
|
|
SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
|
2008-04-12 00:23:24 +04:00
|
|
|
RefBindings B = GetRefBindings(StVals);
|
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
if (RefBindings::TreeTy* T = B.SlimFind(Sym)) {
|
2008-04-16 08:28:53 +04:00
|
|
|
B = Update(B, Sym, T->getValue().second, GetArgE(Summ, idx), hasErr);
|
2008-03-12 04:21:45 +03:00
|
|
|
SetRefBindings(StVals, B);
|
2008-04-11 22:40:51 +04:00
|
|
|
|
2008-04-16 08:28:53 +04:00
|
|
|
if (hasErr) {
|
2008-04-11 22:40:51 +04:00
|
|
|
ErrorExpr = *I;
|
2008-04-18 07:39:05 +04:00
|
|
|
ErrorSym = T->getValue().first;
|
2008-04-11 22:40:51 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-03-11 09:39:11 +03:00
|
|
|
}
|
2008-04-12 00:51:02 +04:00
|
|
|
}
|
|
|
|
else if (isa<LVal>(V)) { // Nuke all arguments passed by reference.
|
|
|
|
|
|
|
|
// FIXME: This is basically copy-and-paste from GRSimpleVals. We
|
|
|
|
// should compose behavior, not copy it.
|
2008-04-12 00:23:24 +04:00
|
|
|
StateMgr.Unbind(StVals, cast<LVal>(V));
|
2008-04-12 00:51:02 +04:00
|
|
|
}
|
2008-04-23 01:39:21 +04:00
|
|
|
else if (isa<nonlval::LValAsInteger>(V))
|
|
|
|
StateMgr.Unbind(StVals, cast<nonlval::LValAsInteger>(V).getLVal());
|
2008-03-12 04:21:45 +03:00
|
|
|
}
|
2008-04-16 08:28:53 +04:00
|
|
|
|
|
|
|
St = StateMgr.getPersistentState(StVals);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-04-16 08:28:53 +04:00
|
|
|
if (hasErr) {
|
2008-04-18 07:39:05 +04:00
|
|
|
ProcessNonLeakError(Dst, Builder, CE, ErrorExpr, Pred, St,
|
|
|
|
hasErr, ErrorSym);
|
2008-03-12 04:21:45 +03:00
|
|
|
return;
|
2008-03-11 20:48:22 +03:00
|
|
|
}
|
2008-04-16 08:28:53 +04:00
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
// Finally, consult the summary for the return value.
|
|
|
|
|
2008-04-12 00:23:24 +04:00
|
|
|
RetEffect RE = GetRetE(Summ);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
switch (RE.getKind()) {
|
|
|
|
default:
|
|
|
|
assert (false && "Unhandled RetEffect."); break;
|
|
|
|
|
2008-04-11 03:44:06 +04:00
|
|
|
case RetEffect::NoRet:
|
2008-04-12 00:23:24 +04:00
|
|
|
|
|
|
|
// Make up a symbol for the return value (not reference counted).
|
2008-04-12 00:51:02 +04:00
|
|
|
// FIXME: This is basically copy-and-paste from GRSimpleVals. We
|
|
|
|
// should compose behavior, not copy it.
|
2008-04-12 00:23:24 +04:00
|
|
|
|
|
|
|
if (CE->getType() != Eng.getContext().VoidTy) {
|
|
|
|
unsigned Count = Builder.getCurrentBlockCount();
|
|
|
|
SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count);
|
|
|
|
|
|
|
|
RVal X = CE->getType()->isPointerType()
|
|
|
|
? cast<RVal>(lval::SymbolVal(Sym))
|
|
|
|
: cast<RVal>(nonlval::SymbolVal(Sym));
|
|
|
|
|
|
|
|
St = StateMgr.SetRVal(St, CE, X, Eng.getCFG().isBlkExpr(CE), false);
|
|
|
|
}
|
|
|
|
|
2008-04-11 03:44:06 +04:00
|
|
|
break;
|
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
case RetEffect::Alias: {
|
|
|
|
unsigned idx = RE.getValue();
|
|
|
|
assert (idx < CE->getNumArgs());
|
|
|
|
RVal V = StateMgr.GetRVal(St, CE->getArg(idx));
|
2008-03-13 00:06:49 +03:00
|
|
|
St = StateMgr.SetRVal(St, CE, V, Eng.getCFG().isBlkExpr(CE), false);
|
2008-03-12 04:21:45 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RetEffect::OwnedSymbol: {
|
|
|
|
unsigned Count = Builder.getCurrentBlockCount();
|
2008-03-13 00:45:47 +03:00
|
|
|
SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
ValueState StImpl = *St;
|
|
|
|
RefBindings B = GetRefBindings(StImpl);
|
2008-04-11 03:09:18 +04:00
|
|
|
SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeOwned()));
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
St = StateMgr.SetRVal(StateMgr.getPersistentState(StImpl),
|
|
|
|
CE, lval::SymbolVal(Sym),
|
2008-03-13 00:06:49 +03:00
|
|
|
Eng.getCFG().isBlkExpr(CE), false);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RetEffect::NotOwnedSymbol: {
|
|
|
|
unsigned Count = Builder.getCurrentBlockCount();
|
2008-03-13 00:45:47 +03:00
|
|
|
SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
ValueState StImpl = *St;
|
|
|
|
RefBindings B = GetRefBindings(StImpl);
|
|
|
|
SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeNotOwned()));
|
|
|
|
|
|
|
|
St = StateMgr.SetRVal(StateMgr.getPersistentState(StImpl),
|
|
|
|
CE, lval::SymbolVal(Sym),
|
2008-03-13 00:06:49 +03:00
|
|
|
Eng.getCFG().isBlkExpr(CE), false);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-22 00:30:14 +03:00
|
|
|
Builder.MakeNode(Dst, CE, Pred, St);
|
2008-03-06 03:08:09 +03:00
|
|
|
}
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-04-16 03:44:31 +04:00
|
|
|
|
|
|
|
void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Eng,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
ObjCMessageExpr* ME,
|
|
|
|
ExplodedNode<ValueState>* Pred) {
|
|
|
|
|
|
|
|
if (EvalObjCMessageExprAux(Dst, Eng, Builder, ME, Pred))
|
|
|
|
GRSimpleVals::EvalObjCMessageExpr(Dst, Eng, Builder, ME, Pred);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFRefCount::EvalObjCMessageExprAux(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Eng,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
ObjCMessageExpr* ME,
|
|
|
|
ExplodedNode<ValueState>* Pred) {
|
2008-04-29 09:33:51 +04:00
|
|
|
|
|
|
|
if (GCEnabled)
|
|
|
|
return true;
|
|
|
|
|
2008-04-16 08:28:53 +04:00
|
|
|
// Handle "toll-free bridging" of calls to "Release" and "Retain".
|
|
|
|
|
|
|
|
// FIXME: track the underlying object type associated so that we can
|
|
|
|
// flag illegal uses of toll-free bridging (or at least handle it
|
|
|
|
// at casts).
|
2008-04-16 03:44:31 +04:00
|
|
|
|
|
|
|
Selector S = ME->getSelector();
|
|
|
|
|
|
|
|
if (!S.isUnarySelector())
|
|
|
|
return true;
|
|
|
|
|
2008-04-16 08:28:53 +04:00
|
|
|
Expr* Receiver = ME->getReceiver();
|
|
|
|
|
|
|
|
if (!Receiver)
|
|
|
|
return true;
|
|
|
|
|
2008-05-01 06:18:37 +04:00
|
|
|
// Check if we are calling "autorelease".
|
|
|
|
|
|
|
|
enum { IsRelease, IsRetain, IsAutorelease, IsNone } mode = IsNone;
|
2008-04-16 08:28:53 +04:00
|
|
|
|
2008-05-01 06:18:37 +04:00
|
|
|
if (S == AutoreleaseSelector)
|
|
|
|
mode = IsAutorelease;
|
|
|
|
else if (S == RetainSelector)
|
|
|
|
mode = IsRetain;
|
|
|
|
else if (S == ReleaseSelector)
|
|
|
|
mode = IsRelease;
|
2008-04-16 08:28:53 +04:00
|
|
|
|
2008-05-01 06:18:37 +04:00
|
|
|
if (mode == IsNone)
|
2008-04-16 08:28:53 +04:00
|
|
|
return true;
|
|
|
|
|
2008-05-01 06:18:37 +04:00
|
|
|
// We have "retain", "release", or "autorelease".
|
2008-04-16 08:28:53 +04:00
|
|
|
ValueStateManager& StateMgr = Eng.getStateManager();
|
|
|
|
ValueState* St = Builder.GetState(Pred);
|
|
|
|
RVal V = StateMgr.GetRVal(St, Receiver);
|
|
|
|
|
2008-05-01 06:18:37 +04:00
|
|
|
// Was the argument something we are not tracking?
|
2008-04-16 08:28:53 +04:00
|
|
|
if (!isa<lval::SymbolVal>(V))
|
|
|
|
return true;
|
|
|
|
|
2008-05-01 06:18:37 +04:00
|
|
|
// Get the bindings.
|
2008-04-16 08:28:53 +04:00
|
|
|
SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
|
|
|
|
RefBindings B = GetRefBindings(*St);
|
|
|
|
|
2008-05-01 06:18:37 +04:00
|
|
|
// Find the tracked value.
|
2008-04-16 08:28:53 +04:00
|
|
|
RefBindings::TreeTy* T = B.SlimFind(Sym);
|
2008-05-01 06:18:37 +04:00
|
|
|
|
2008-04-16 08:28:53 +04:00
|
|
|
if (!T)
|
|
|
|
return true;
|
|
|
|
|
2008-05-01 06:18:37 +04:00
|
|
|
RefVal::Kind hasErr = (RefVal::Kind) 0;
|
2008-04-16 08:28:53 +04:00
|
|
|
|
2008-05-01 06:18:37 +04:00
|
|
|
// Update the bindings.
|
|
|
|
switch (mode) {
|
|
|
|
case IsNone:
|
|
|
|
assert(false);
|
|
|
|
|
|
|
|
case IsRelease:
|
|
|
|
B = Update(B, Sym, T->getValue().second, DecRef, hasErr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IsRetain:
|
|
|
|
B = Update(B, Sym, T->getValue().second, IncRef, hasErr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IsAutorelease:
|
|
|
|
// For now we just stop tracking a value if we see
|
|
|
|
// it sent "autorelease." In the future we can potentially
|
|
|
|
// track the associated pool.
|
|
|
|
B = Remove(B, Sym);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new state with the updated bindings.
|
2008-04-16 08:28:53 +04:00
|
|
|
ValueState StVals = *St;
|
|
|
|
SetRefBindings(StVals, B);
|
2008-05-01 08:02:04 +04:00
|
|
|
St = Eng.SetRVal(StateMgr.getPersistentState(StVals), ME, V);
|
2008-04-16 08:28:53 +04:00
|
|
|
|
2008-05-01 06:18:37 +04:00
|
|
|
// Create an error node if it exists.
|
2008-04-16 08:28:53 +04:00
|
|
|
if (hasErr)
|
2008-04-18 07:39:05 +04:00
|
|
|
ProcessNonLeakError(Dst, Builder, ME, Receiver, Pred, St, hasErr, Sym);
|
2008-04-16 08:28:53 +04:00
|
|
|
else
|
|
|
|
Builder.MakeNode(Dst, ME, Pred, St);
|
|
|
|
|
|
|
|
return false;
|
2008-04-16 03:44:31 +04:00
|
|
|
}
|
|
|
|
|
2008-04-17 00:40:59 +04:00
|
|
|
// Stores.
|
|
|
|
|
|
|
|
void CFRefCount::EvalStore(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Eng,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
Expr* E, ExplodedNode<ValueState>* Pred,
|
|
|
|
ValueState* St, RVal TargetLV, RVal Val) {
|
|
|
|
|
|
|
|
// Check if we have a binding for "Val" and if we are storing it to something
|
|
|
|
// we don't understand or otherwise the value "escapes" the function.
|
|
|
|
|
|
|
|
if (!isa<lval::SymbolVal>(Val))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Are we storing to something that causes the value to "escape"?
|
|
|
|
|
|
|
|
bool escapes = false;
|
|
|
|
|
|
|
|
if (!isa<lval::DeclVal>(TargetLV))
|
|
|
|
escapes = true;
|
|
|
|
else
|
|
|
|
escapes = cast<lval::DeclVal>(TargetLV).getDecl()->hasGlobalStorage();
|
|
|
|
|
|
|
|
if (!escapes)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SymbolID Sym = cast<lval::SymbolVal>(Val).getSymbol();
|
|
|
|
RefBindings B = GetRefBindings(*St);
|
|
|
|
RefBindings::TreeTy* T = B.SlimFind(Sym);
|
|
|
|
|
|
|
|
if (!T)
|
|
|
|
return;
|
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
// Nuke the binding.
|
|
|
|
St = NukeBinding(Eng.getStateManager(), St, Sym);
|
2008-04-17 00:40:59 +04:00
|
|
|
|
|
|
|
// Hand of the remaining logic to the parent implementation.
|
|
|
|
GRSimpleVals::EvalStore(Dst, Eng, Builder, E, Pred, St, TargetLV, Val);
|
|
|
|
}
|
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
|
|
|
|
ValueState* CFRefCount::NukeBinding(ValueStateManager& VMgr, ValueState* St,
|
|
|
|
SymbolID sid) {
|
|
|
|
ValueState StImpl = *St;
|
|
|
|
RefBindings B = GetRefBindings(StImpl);
|
|
|
|
StImpl.CheckerState = RefBFactory.Remove(B, sid).getRoot();
|
|
|
|
return VMgr.getPersistentState(StImpl);
|
|
|
|
}
|
|
|
|
|
2008-04-12 02:25:11 +04:00
|
|
|
// End-of-path.
|
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
ValueState* CFRefCount::HandleSymbolDeath(ValueStateManager& VMgr,
|
|
|
|
ValueState* St, SymbolID sid,
|
|
|
|
RefVal V, bool& hasLeak) {
|
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
hasLeak = V.isOwned() ||
|
|
|
|
((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0);
|
2008-04-17 02:32:20 +04:00
|
|
|
|
|
|
|
if (!hasLeak)
|
|
|
|
return NukeBinding(VMgr, St, sid);
|
|
|
|
|
|
|
|
RefBindings B = GetRefBindings(*St);
|
|
|
|
ValueState StImpl = *St;
|
|
|
|
StImpl.CheckerState = RefBFactory.Add(B, sid, RefVal::makeLeak()).getRoot();
|
|
|
|
return VMgr.getPersistentState(StImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFRefCount::EvalEndPath(GRExprEngine& Eng,
|
2008-04-12 02:25:11 +04:00
|
|
|
GREndPathNodeBuilder<ValueState>& Builder) {
|
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
ValueState* St = Builder.getState();
|
|
|
|
RefBindings B = GetRefBindings(*St);
|
2008-04-12 02:25:11 +04:00
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
llvm::SmallVector<SymbolID, 10> Leaked;
|
2008-04-12 02:25:11 +04:00
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
|
|
|
|
bool hasLeak = false;
|
2008-04-12 02:25:11 +04:00
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
St = HandleSymbolDeath(Eng.getStateManager(), St,
|
|
|
|
(*I).first, (*I).second, hasLeak);
|
|
|
|
|
|
|
|
if (hasLeak) Leaked.push_back((*I).first);
|
|
|
|
}
|
2008-04-25 03:57:27 +04:00
|
|
|
|
|
|
|
if (Leaked.empty())
|
|
|
|
return;
|
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
ExplodedNode<ValueState>* N = Builder.MakeNode(St);
|
2008-04-18 20:30:14 +04:00
|
|
|
|
2008-04-25 03:57:27 +04:00
|
|
|
if (!N)
|
2008-04-18 20:30:14 +04:00
|
|
|
return;
|
2008-04-18 23:23:43 +04:00
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
std::vector<SymbolID>*& LeaksAtNode = Leaks[N];
|
|
|
|
assert (!LeaksAtNode);
|
|
|
|
LeaksAtNode = new std::vector<SymbolID>();
|
2008-04-17 02:32:20 +04:00
|
|
|
|
|
|
|
for (llvm::SmallVector<SymbolID, 10>::iterator I=Leaked.begin(),
|
|
|
|
E = Leaked.end(); I != E; ++I)
|
2008-04-18 07:39:05 +04:00
|
|
|
(*LeaksAtNode).push_back(*I);
|
2008-04-12 02:25:11 +04:00
|
|
|
}
|
|
|
|
|
2008-04-25 03:57:27 +04:00
|
|
|
// Dead symbols.
|
|
|
|
|
|
|
|
void CFRefCount::EvalDeadSymbols(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Eng,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
2008-04-25 05:25:15 +04:00
|
|
|
ExplodedNode<ValueState>* Pred,
|
|
|
|
Stmt* S,
|
2008-04-25 03:57:27 +04:00
|
|
|
ValueState* St,
|
|
|
|
const ValueStateManager::DeadSymbolsTy& Dead) {
|
2008-04-25 05:25:15 +04:00
|
|
|
|
2008-04-25 03:57:27 +04:00
|
|
|
// FIXME: a lot of copy-and-paste from EvalEndPath. Refactor.
|
|
|
|
|
|
|
|
RefBindings B = GetRefBindings(*St);
|
|
|
|
llvm::SmallVector<SymbolID, 10> Leaked;
|
|
|
|
|
|
|
|
for (ValueStateManager::DeadSymbolsTy::const_iterator
|
|
|
|
I=Dead.begin(), E=Dead.end(); I!=E; ++I) {
|
|
|
|
|
|
|
|
RefBindings::TreeTy* T = B.SlimFind(*I);
|
|
|
|
|
|
|
|
if (!T)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool hasLeak = false;
|
|
|
|
|
|
|
|
St = HandleSymbolDeath(Eng.getStateManager(), St,
|
|
|
|
*I, T->getValue().second, hasLeak);
|
|
|
|
|
|
|
|
if (hasLeak) Leaked.push_back(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Leaked.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ExplodedNode<ValueState>* N = Builder.MakeNode(Dst, S, Pred, St);
|
|
|
|
|
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::vector<SymbolID>*& LeaksAtNode = Leaks[N];
|
|
|
|
assert (!LeaksAtNode);
|
|
|
|
LeaksAtNode = new std::vector<SymbolID>();
|
|
|
|
|
|
|
|
for (llvm::SmallVector<SymbolID, 10>::iterator I=Leaked.begin(),
|
|
|
|
E = Leaked.end(); I != E; ++I)
|
|
|
|
(*LeaksAtNode).push_back(*I);
|
|
|
|
}
|
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
// Return statements.
|
|
|
|
|
|
|
|
void CFRefCount::EvalReturn(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Eng,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
ReturnStmt* S,
|
|
|
|
ExplodedNode<ValueState>* Pred) {
|
|
|
|
|
|
|
|
Expr* RetE = S->getRetValue();
|
|
|
|
if (!RetE) return;
|
|
|
|
|
|
|
|
ValueStateManager& StateMgr = Eng.getStateManager();
|
|
|
|
ValueState* St = Builder.GetState(Pred);
|
|
|
|
RVal V = StateMgr.GetRVal(St, RetE);
|
|
|
|
|
|
|
|
if (!isa<lval::SymbolVal>(V))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the reference count binding (if any).
|
|
|
|
SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
|
|
|
|
RefBindings B = GetRefBindings(*St);
|
|
|
|
RefBindings::TreeTy* T = B.SlimFind(Sym);
|
|
|
|
|
|
|
|
if (!T)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Change the reference count.
|
|
|
|
|
|
|
|
RefVal X = T->getValue().second;
|
|
|
|
|
|
|
|
switch (X.getKind()) {
|
|
|
|
|
|
|
|
case RefVal::Owned: {
|
|
|
|
unsigned cnt = X.getCount();
|
|
|
|
X = RefVal::makeReturnedOwned(cnt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RefVal::NotOwned: {
|
|
|
|
unsigned cnt = X.getCount();
|
|
|
|
X = cnt ? RefVal::makeReturnedOwned(cnt - 1)
|
|
|
|
: RefVal::makeReturnedNotOwned();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// None of the error states should be possible at this point.
|
|
|
|
// A symbol could not have been leaked (yet) if we are returning it
|
|
|
|
// (and thus it is still live), and the other errors are hard errors.
|
|
|
|
assert(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the binding.
|
|
|
|
|
|
|
|
ValueState StImpl = *St;
|
|
|
|
StImpl.CheckerState = RefBFactory.Add(B, Sym, X).getRoot();
|
|
|
|
Builder.MakeNode(Dst, S, Pred, StateMgr.getPersistentState(StImpl));
|
|
|
|
}
|
|
|
|
|
2008-04-18 23:23:43 +04:00
|
|
|
// Assumptions.
|
|
|
|
|
|
|
|
ValueState* CFRefCount::EvalAssume(GRExprEngine& Eng, ValueState* St,
|
|
|
|
RVal Cond, bool Assumption,
|
|
|
|
bool& isFeasible) {
|
|
|
|
|
|
|
|
// FIXME: We may add to the interface of EvalAssume the list of symbols
|
|
|
|
// whose assumptions have changed. For now we just iterate through the
|
|
|
|
// bindings and check if any of the tracked symbols are NULL. This isn't
|
|
|
|
// too bad since the number of symbols we will track in practice are
|
|
|
|
// probably small and EvalAssume is only called at branches and a few
|
|
|
|
// other places.
|
|
|
|
|
|
|
|
RefBindings B = GetRefBindings(*St);
|
|
|
|
|
|
|
|
if (B.isEmpty())
|
|
|
|
return St;
|
|
|
|
|
|
|
|
bool changed = false;
|
|
|
|
|
|
|
|
for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
|
|
|
|
|
|
|
|
// Check if the symbol is null (or equal to any constant).
|
|
|
|
// If this is the case, stop tracking the symbol.
|
|
|
|
|
|
|
|
if (St->getSymVal(I.getKey())) {
|
|
|
|
changed = true;
|
|
|
|
B = RefBFactory.Remove(B, I.getKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!changed)
|
|
|
|
return St;
|
|
|
|
|
|
|
|
ValueState StImpl = *St;
|
|
|
|
StImpl.CheckerState = B.getRoot();
|
|
|
|
return Eng.getStateManager().getPersistentState(StImpl);
|
|
|
|
}
|
2008-03-11 09:39:11 +03:00
|
|
|
|
|
|
|
CFRefCount::RefBindings CFRefCount::Update(RefBindings B, SymbolID sym,
|
2008-03-11 20:48:22 +03:00
|
|
|
RefVal V, ArgEffect E,
|
2008-04-16 08:28:53 +04:00
|
|
|
RefVal::Kind& hasErr) {
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-03-11 20:48:22 +03:00
|
|
|
// FIXME: This dispatch can potentially be sped up by unifiying it into
|
|
|
|
// a single switch statement. Opt for simplicity for now.
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-03-11 20:48:22 +03:00
|
|
|
switch (E) {
|
|
|
|
default:
|
|
|
|
assert (false && "Unhandled CFRef transition.");
|
|
|
|
|
|
|
|
case DoNothing:
|
2008-04-29 09:44:10 +04:00
|
|
|
if (!GCEnabled && V.getKind() == RefVal::Released) {
|
2008-03-12 04:21:45 +03:00
|
|
|
V = RefVal::makeUseAfterRelease();
|
2008-04-16 08:28:53 +04:00
|
|
|
hasErr = V.getKind();
|
2008-03-12 04:21:45 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-03-11 20:48:22 +03:00
|
|
|
return B;
|
|
|
|
|
|
|
|
case IncRef:
|
|
|
|
switch (V.getKind()) {
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
|
|
|
|
case RefVal::Owned:
|
2008-04-11 03:44:06 +04:00
|
|
|
V = RefVal::makeOwned(V.getCount()+1);
|
|
|
|
break;
|
2008-04-11 03:09:18 +04:00
|
|
|
|
2008-03-11 20:48:22 +03:00
|
|
|
case RefVal::NotOwned:
|
2008-04-11 03:09:18 +04:00
|
|
|
V = RefVal::makeNotOwned(V.getCount()+1);
|
2008-03-11 20:48:22 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RefVal::Released:
|
2008-04-29 09:44:10 +04:00
|
|
|
if (GCEnabled)
|
|
|
|
V = RefVal::makeOwned();
|
|
|
|
else {
|
|
|
|
V = RefVal::makeUseAfterRelease();
|
|
|
|
hasErr = V.getKind();
|
|
|
|
}
|
|
|
|
|
2008-03-11 20:48:22 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-04-11 03:44:06 +04:00
|
|
|
break;
|
|
|
|
|
2008-03-11 20:48:22 +03:00
|
|
|
case DecRef:
|
|
|
|
switch (V.getKind()) {
|
|
|
|
default:
|
|
|
|
assert (false);
|
|
|
|
|
|
|
|
case RefVal::Owned: {
|
2008-04-17 22:12:53 +04:00
|
|
|
unsigned Count = V.getCount();
|
|
|
|
V = Count > 0 ? RefVal::makeOwned(Count - 1) : RefVal::makeReleased();
|
2008-03-11 20:48:22 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-04-11 03:09:18 +04:00
|
|
|
case RefVal::NotOwned: {
|
2008-04-17 22:12:53 +04:00
|
|
|
unsigned Count = V.getCount();
|
2008-04-11 03:09:18 +04:00
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
if (Count > 0)
|
|
|
|
V = RefVal::makeNotOwned(Count - 1);
|
2008-04-11 03:09:18 +04:00
|
|
|
else {
|
|
|
|
V = RefVal::makeReleaseNotOwned();
|
2008-04-16 08:28:53 +04:00
|
|
|
hasErr = V.getKind();
|
2008-04-11 03:09:18 +04:00
|
|
|
}
|
2008-03-11 20:48:22 +03:00
|
|
|
|
|
|
|
break;
|
2008-04-11 03:09:18 +04:00
|
|
|
}
|
2008-03-11 20:48:22 +03:00
|
|
|
|
|
|
|
case RefVal::Released:
|
|
|
|
V = RefVal::makeUseAfterRelease();
|
2008-04-16 08:28:53 +04:00
|
|
|
hasErr = V.getKind();
|
2008-03-11 20:48:22 +03:00
|
|
|
break;
|
|
|
|
}
|
2008-04-11 03:44:06 +04:00
|
|
|
|
|
|
|
break;
|
2008-03-11 20:48:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return RefBFactory.Add(B, sym, V);
|
2008-03-11 09:39:11 +03:00
|
|
|
}
|
|
|
|
|
2008-04-09 05:10:13 +04:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-10 03:49:11 +04:00
|
|
|
// Error reporting.
|
2008-04-09 05:10:13 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
//===-------------===//
|
|
|
|
// Bug Descriptions. //
|
|
|
|
//===-------------===//
|
|
|
|
|
2008-04-19 00:54:29 +04:00
|
|
|
class VISIBILITY_HIDDEN CFRefBug : public BugTypeCacheLocation {
|
2008-04-18 07:39:05 +04:00
|
|
|
protected:
|
|
|
|
CFRefCount& TF;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CFRefBug(CFRefCount& tf) : TF(tf) {}
|
2008-05-01 03:47:44 +04:00
|
|
|
|
|
|
|
CFRefCount& getTF() { return TF; }
|
2008-04-18 07:39:05 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug {
|
|
|
|
public:
|
|
|
|
UseAfterRelease(CFRefCount& tf) : CFRefBug(tf) {}
|
|
|
|
|
|
|
|
virtual const char* getName() const {
|
2008-04-18 08:55:01 +04:00
|
|
|
return "Core Foundation: Use-After-Release";
|
2008-04-18 07:39:05 +04:00
|
|
|
}
|
|
|
|
virtual const char* getDescription() const {
|
2008-04-18 08:55:01 +04:00
|
|
|
return "Reference-counted object is used"
|
|
|
|
" after it is released.";
|
2008-04-18 07:39:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void EmitWarnings(BugReporter& BR);
|
|
|
|
};
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN BadRelease : public CFRefBug {
|
|
|
|
public:
|
|
|
|
BadRelease(CFRefCount& tf) : CFRefBug(tf) {}
|
|
|
|
|
|
|
|
virtual const char* getName() const {
|
2008-04-18 08:55:01 +04:00
|
|
|
return "Core Foundation: Release of non-owned object";
|
2008-04-18 07:39:05 +04:00
|
|
|
}
|
|
|
|
virtual const char* getDescription() const {
|
|
|
|
return "Incorrect decrement of the reference count of a "
|
2008-04-18 08:55:01 +04:00
|
|
|
"CoreFoundation object: "
|
2008-04-18 07:39:05 +04:00
|
|
|
"The object is not owned at this point by the caller.";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void EmitWarnings(BugReporter& BR);
|
|
|
|
};
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN Leak : public CFRefBug {
|
|
|
|
public:
|
|
|
|
Leak(CFRefCount& tf) : CFRefBug(tf) {}
|
|
|
|
|
|
|
|
virtual const char* getName() const {
|
2008-04-18 08:55:01 +04:00
|
|
|
return "Core Foundation: Memory Leak";
|
2008-04-18 07:39:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char* getDescription() const {
|
2008-04-18 08:55:01 +04:00
|
|
|
return "Object leaked.";
|
2008-04-18 07:39:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void EmitWarnings(BugReporter& BR);
|
2008-04-18 23:23:43 +04:00
|
|
|
virtual void GetErrorNodes(std::vector<ExplodedNode<ValueState>*>& Nodes);
|
2008-04-18 07:39:05 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
//===---------===//
|
|
|
|
// Bug Reports. //
|
|
|
|
//===---------===//
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport {
|
|
|
|
SymbolID Sym;
|
|
|
|
public:
|
2008-05-01 03:47:44 +04:00
|
|
|
CFRefReport(CFRefBug& D, ExplodedNode<ValueState> *n, SymbolID sym)
|
2008-04-18 07:39:05 +04:00
|
|
|
: RangedBugReport(D, n), Sym(sym) {}
|
|
|
|
|
|
|
|
virtual ~CFRefReport() {}
|
|
|
|
|
2008-05-01 03:47:44 +04:00
|
|
|
virtual std::pair<const char**,const char**> getExtraDescriptiveText();
|
2008-04-18 07:39:05 +04:00
|
|
|
|
|
|
|
virtual PathDiagnosticPiece* VisitNode(ExplodedNode<ValueState>* N,
|
|
|
|
ExplodedNode<ValueState>* PrevN,
|
|
|
|
ExplodedGraph<ValueState>& G,
|
|
|
|
BugReporter& BR);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void CFRefCount::RegisterChecks(GRExprEngine& Eng) {
|
|
|
|
GRSimpleVals::RegisterChecks(Eng);
|
|
|
|
Eng.Register(new UseAfterRelease(*this));
|
|
|
|
Eng.Register(new BadRelease(*this));
|
|
|
|
Eng.Register(new Leak(*this));
|
|
|
|
}
|
|
|
|
|
2008-05-01 03:47:44 +04:00
|
|
|
|
|
|
|
static const char* Msgs[] = {
|
|
|
|
"Code is compiled in garbage collection only mode" // GC only
|
|
|
|
" (the bug occurs with garbage collection enabled).",
|
|
|
|
|
|
|
|
"Code is compiled without garbage collection.", // No GC.
|
|
|
|
|
|
|
|
"Code is compiled for use with and without garbage collection (GC)."
|
|
|
|
" The bug occurs with GC enabled.", // Hybrid, with GC.
|
|
|
|
|
|
|
|
"Code is compiled for use with and without garbage collection (GC)."
|
|
|
|
" The bug occurs in non-GC mode." // Hyrbird, without GC/
|
|
|
|
};
|
|
|
|
|
|
|
|
std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
|
|
|
|
CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
|
|
|
|
|
|
|
|
switch (TF.getLangOptions().getGCMode()) {
|
|
|
|
default:
|
|
|
|
assert(false);
|
2008-05-01 08:02:04 +04:00
|
|
|
|
|
|
|
case LangOptions::GCOnly:
|
|
|
|
assert (TF.isGCEnabled());
|
|
|
|
return std::make_pair(&Msgs[0], &Msgs[0]+1);
|
2008-05-01 03:47:44 +04:00
|
|
|
|
|
|
|
case LangOptions::NonGC:
|
|
|
|
assert (!TF.isGCEnabled());
|
|
|
|
return std::make_pair(&Msgs[1], &Msgs[1]+1);
|
|
|
|
|
|
|
|
case LangOptions::HybridGC:
|
|
|
|
if (TF.isGCEnabled())
|
|
|
|
return std::make_pair(&Msgs[2], &Msgs[2]+1);
|
|
|
|
else
|
|
|
|
return std::make_pair(&Msgs[3], &Msgs[3]+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
PathDiagnosticPiece* CFRefReport::VisitNode(ExplodedNode<ValueState>* N,
|
|
|
|
ExplodedNode<ValueState>* PrevN,
|
|
|
|
ExplodedGraph<ValueState>& G,
|
|
|
|
BugReporter& BR) {
|
|
|
|
|
|
|
|
// Check if the type state has changed.
|
|
|
|
|
|
|
|
ValueState* PrevSt = PrevN->getState();
|
|
|
|
ValueState* CurrSt = N->getState();
|
|
|
|
|
|
|
|
CFRefCount::RefBindings PrevB = CFRefCount::GetRefBindings(*PrevSt);
|
|
|
|
CFRefCount::RefBindings CurrB = CFRefCount::GetRefBindings(*CurrSt);
|
|
|
|
|
2008-04-18 08:55:01 +04:00
|
|
|
CFRefCount::RefBindings::TreeTy* PrevT = PrevB.SlimFind(Sym);
|
|
|
|
CFRefCount::RefBindings::TreeTy* CurrT = CurrB.SlimFind(Sym);
|
2008-04-18 07:39:05 +04:00
|
|
|
|
2008-04-18 08:55:01 +04:00
|
|
|
if (!CurrT)
|
|
|
|
return NULL;
|
2008-04-18 07:39:05 +04:00
|
|
|
|
2008-04-18 08:55:01 +04:00
|
|
|
const char* Msg = NULL;
|
|
|
|
RefVal CurrV = CurrB.SlimFind(Sym)->getValue().second;
|
2008-04-18 07:39:05 +04:00
|
|
|
|
2008-04-18 08:55:01 +04:00
|
|
|
if (!PrevT) {
|
|
|
|
|
|
|
|
// Check for the point where we start tracking the value.
|
|
|
|
|
|
|
|
if (CurrV.isOwned())
|
|
|
|
Msg = "Function call returns 'Owned' Core Foundation object.";
|
|
|
|
else {
|
|
|
|
assert (CurrV.isNotOwned());
|
|
|
|
Msg = "Function call returns 'Non-Owned' Core Foundation object.";
|
|
|
|
}
|
|
|
|
|
|
|
|
Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
|
|
|
|
FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
|
|
|
|
PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg);
|
|
|
|
|
|
|
|
if (Expr* Exp = dyn_cast<Expr>(S))
|
|
|
|
P->addRange(Exp->getSourceRange());
|
|
|
|
|
|
|
|
return P;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if the typestate has changed.
|
|
|
|
|
|
|
|
RefVal PrevV = PrevB.SlimFind(Sym)->getValue().second;
|
|
|
|
|
|
|
|
if (PrevV == CurrV)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// The typestate has changed.
|
|
|
|
|
|
|
|
std::ostringstream os;
|
|
|
|
|
|
|
|
switch (CurrV.getKind()) {
|
|
|
|
case RefVal::Owned:
|
|
|
|
case RefVal::NotOwned:
|
|
|
|
assert (PrevV.getKind() == CurrV.getKind());
|
|
|
|
|
|
|
|
if (PrevV.getCount() > CurrV.getCount())
|
|
|
|
os << "Reference count decremented.";
|
|
|
|
else
|
|
|
|
os << "Reference count incremented.";
|
|
|
|
|
2008-04-18 09:32:44 +04:00
|
|
|
if (CurrV.getCount()) {
|
|
|
|
os << " Object has +" << CurrV.getCount();
|
|
|
|
|
|
|
|
if (CurrV.getCount() > 1)
|
|
|
|
os << " reference counts.";
|
|
|
|
else
|
|
|
|
os << " reference count.";
|
|
|
|
}
|
2008-04-18 08:55:01 +04:00
|
|
|
|
|
|
|
Msg = os.str().c_str();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RefVal::Released:
|
|
|
|
Msg = "Object released.";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RefVal::ReturnedOwned:
|
|
|
|
Msg = "Object returned to caller. "
|
|
|
|
"Caller gets ownership of object.";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RefVal::ReturnedNotOwned:
|
|
|
|
Msg = "Object returned to caller. "
|
|
|
|
"Caller does not get ownership of object.";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
|
|
|
|
FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
|
|
|
|
PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg);
|
|
|
|
|
|
|
|
// Add the range by scanning the children of the statement for any bindings
|
|
|
|
// to Sym.
|
|
|
|
|
|
|
|
ValueStateManager& VSM = BR.getEngine().getStateManager();
|
|
|
|
|
|
|
|
for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
|
|
|
|
if (Expr* Exp = dyn_cast_or_null<Expr>(*I)) {
|
|
|
|
RVal X = VSM.GetRVal(CurrSt, Exp);
|
|
|
|
|
|
|
|
if (lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&X))
|
|
|
|
if (SV->getSymbol() == Sym) {
|
|
|
|
P->addRange(Exp->getSourceRange()); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return P;
|
2008-04-18 07:39:05 +04:00
|
|
|
}
|
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
void UseAfterRelease::EmitWarnings(BugReporter& BR) {
|
2008-04-09 05:10:13 +04:00
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
for (CFRefCount::use_after_iterator I = TF.use_after_begin(),
|
|
|
|
E = TF.use_after_end(); I != E; ++I) {
|
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
CFRefReport report(*this, I->first, I->second.second);
|
|
|
|
report.addRange(I->second.first->getSourceRange());
|
2008-04-18 05:56:37 +04:00
|
|
|
BR.EmitWarning(report);
|
2008-04-09 05:10:13 +04:00
|
|
|
}
|
2008-04-10 03:49:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void BadRelease::EmitWarnings(BugReporter& BR) {
|
2008-04-09 05:10:13 +04:00
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
for (CFRefCount::bad_release_iterator I = TF.bad_release_begin(),
|
|
|
|
E = TF.bad_release_end(); I != E; ++I) {
|
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
CFRefReport report(*this, I->first, I->second.second);
|
|
|
|
report.addRange(I->second.first->getSourceRange());
|
|
|
|
BR.EmitWarning(report);
|
2008-04-10 03:49:11 +04:00
|
|
|
}
|
|
|
|
}
|
2008-04-09 05:10:13 +04:00
|
|
|
|
2008-04-18 03:43:50 +04:00
|
|
|
void Leak::EmitWarnings(BugReporter& BR) {
|
|
|
|
|
|
|
|
for (CFRefCount::leaks_iterator I = TF.leaks_begin(),
|
|
|
|
E = TF.leaks_end(); I != E; ++I) {
|
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
std::vector<SymbolID>& SymV = *(I->second);
|
|
|
|
unsigned n = SymV.size();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < n; ++i) {
|
|
|
|
CFRefReport report(*this, I->first, SymV[i]);
|
|
|
|
BR.EmitWarning(report);
|
|
|
|
}
|
2008-04-18 03:43:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-18 23:23:43 +04:00
|
|
|
void Leak::GetErrorNodes(std::vector<ExplodedNode<ValueState>*>& Nodes) {
|
|
|
|
for (CFRefCount::leaks_iterator I=TF.leaks_begin(), E=TF.leaks_end();
|
|
|
|
I!=E; ++I)
|
|
|
|
Nodes.push_back(I->first);
|
|
|
|
}
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-11 02:16:52 +04:00
|
|
|
// Transfer function creation for external clients.
|
2008-03-11 09:39:11 +03:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-01 03:47:44 +04:00
|
|
|
GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
|
|
|
|
const LangOptions& lopts) {
|
|
|
|
return new CFRefCount(Ctx, GCEnabled, lopts);
|
2008-04-11 02:58:08 +04:00
|
|
|
}
|