2011-09-10 00:11:56 +08:00
|
|
|
//===- ThreadSafety.cpp ----------------------------------------*- C++ --*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// A intra-procedural analysis for thread safety (e.g. deadlocks and race
|
|
|
|
// conditions), based off of an annotation system.
|
|
|
|
//
|
2014-04-08 02:09:54 +08:00
|
|
|
// See http://clang.llvm.org/docs/ThreadSafetyAnalysis.html
|
2013-06-27 03:17:19 +08:00
|
|
|
// for more information.
|
2011-09-10 00:11:56 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-01 23:09:41 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2011-09-10 00:11:56 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
|
|
|
#include "clang/AST/ExprCXX.h"
|
|
|
|
#include "clang/AST/StmtCXX.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Analysis/Analyses/PostOrderCFGView.h"
|
2014-04-08 02:09:54 +08:00
|
|
|
#include "clang/Analysis/Analyses/ThreadSafety.h"
|
2014-05-10 02:26:23 +08:00
|
|
|
#include "clang/Analysis/Analyses/ThreadSafetyLogical.h"
|
2014-04-08 02:09:54 +08:00
|
|
|
#include "clang/Analysis/Analyses/ThreadSafetyTIL.h"
|
2014-04-10 06:39:43 +08:00
|
|
|
#include "clang/Analysis/Analyses/ThreadSafetyTraverse.h"
|
2014-04-08 02:09:54 +08:00
|
|
|
#include "clang/Analysis/Analyses/ThreadSafetyCommon.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Analysis/AnalysisContext.h"
|
|
|
|
#include "clang/Analysis/CFG.h"
|
|
|
|
#include "clang/Analysis/CFGStmtMap.h"
|
2012-07-04 03:47:18 +08:00
|
|
|
#include "clang/Basic/OperatorKinds.h"
|
2012-12-01 23:09:41 +08:00
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2011-09-10 00:11:56 +08:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
|
|
|
#include "llvm/ADT/ImmutableMap.h"
|
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2012-01-07 02:36:09 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-09-10 00:11:56 +08:00
|
|
|
#include <algorithm>
|
2014-07-28 23:57:27 +08:00
|
|
|
#include <ostream>
|
|
|
|
#include <sstream>
|
2012-01-07 02:36:09 +08:00
|
|
|
#include <utility>
|
2011-09-10 00:11:56 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace threadSafety {
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2011-09-15 04:05:09 +08:00
|
|
|
// Key method definition
|
|
|
|
ThreadSafetyHandler::~ThreadSafetyHandler() {}
|
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
class TILPrinter :
|
|
|
|
public til::PrettyPrinter<TILPrinter, llvm::raw_ostream> {};
|
2011-10-18 05:33:35 +08:00
|
|
|
|
2011-10-18 05:38:02 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
/// Issue a warning about an invalid lock expression
|
|
|
|
static void warnInvalidLock(ThreadSafetyHandler &Handler,
|
|
|
|
const Expr *MutexExp, const NamedDecl *D,
|
|
|
|
const Expr *DeclExp, StringRef Kind) {
|
|
|
|
SourceLocation Loc;
|
|
|
|
if (DeclExp)
|
|
|
|
Loc = DeclExp->getExprLoc();
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
// FIXME: add a note about the attribute location in MutexExp or D
|
|
|
|
if (Loc.isValid())
|
|
|
|
Handler.handleInvalidLockExp(Kind, Loc);
|
|
|
|
}
|
2012-08-11 04:29:46 +08:00
|
|
|
|
2011-12-09 04:23:06 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
// Various helper functions on til::SExpr
|
|
|
|
namespace sx {
|
2011-09-15 04:00:24 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
bool isUniversal(const til::SExpr *E) {
|
|
|
|
return isa<til::Wildcard>(E);
|
|
|
|
}
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
bool equals(const til::SExpr *E1, const til::SExpr *E2) {
|
|
|
|
return til::EqualsComparator::compareExprs(E1, E2);
|
|
|
|
}
|
2012-09-01 05:57:32 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
const til::SExpr* ignorePtrCasts(const til::SExpr *E) {
|
|
|
|
if (auto *CE = dyn_cast<til::Cast>(E)) {
|
|
|
|
if (CE->castOpcode() == til::CAST_objToPtr)
|
|
|
|
return CE->expr();
|
2012-09-08 01:34:53 +08:00
|
|
|
}
|
2014-07-28 23:57:27 +08:00
|
|
|
return E;
|
|
|
|
}
|
2012-09-08 01:34:53 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
bool matches(const til::SExpr *E1, const til::SExpr *E2) {
|
|
|
|
// We treat a top-level wildcard as the "univsersal" lock.
|
|
|
|
// It matches everything for the purpose of checking locks, but not
|
|
|
|
// for unlocking them.
|
|
|
|
if (isa<til::Wildcard>(E1))
|
|
|
|
return isa<til::Wildcard>(E2);
|
|
|
|
if (isa<til::Wildcard>(E2))
|
|
|
|
return isa<til::Wildcard>(E1);
|
2011-10-22 02:10:14 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
return til::MatchComparator::compareExprs(E1, E2);
|
|
|
|
}
|
2011-10-22 02:10:14 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
bool partiallyMatches(const til::SExpr *E1, const til::SExpr *E2) {
|
|
|
|
auto *PE1 = dyn_cast_or_null<til::Project>(E1);
|
|
|
|
if (!PE1)
|
|
|
|
return false;
|
|
|
|
auto *PE2 = dyn_cast_or_null<til::Project>(E2);
|
|
|
|
if (!PE2)
|
|
|
|
return false;
|
|
|
|
return PE1->clangDecl() == PE2->clangDecl();
|
|
|
|
}
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
std::string toString(const til::SExpr *E) {
|
|
|
|
std::stringstream ss;
|
|
|
|
til::StdPrinter::print(E, ss);
|
|
|
|
return ss.str();
|
|
|
|
}
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
bool shouldIgnore(const til::SExpr *E) {
|
|
|
|
if (!E)
|
|
|
|
return true;
|
|
|
|
// Trap mutex expressions like nullptr, or 0.
|
|
|
|
// Any literal value is nonsense.
|
|
|
|
if (isa<til::Literal>(E))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-11 04:29:46 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
} // end namespace sx
|
2012-09-11 03:58:23 +08:00
|
|
|
|
2012-08-11 04:29:46 +08:00
|
|
|
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2012-08-11 04:19:55 +08:00
|
|
|
/// \brief A short list of SExprs
|
2014-07-28 23:57:27 +08:00
|
|
|
class MutexIDList : public SmallVector<const til::SExpr*, 3> {
|
2012-07-06 05:16:29 +08:00
|
|
|
public:
|
2014-03-07 03:10:16 +08:00
|
|
|
/// \brief Push M onto list, but discard duplicates.
|
2014-07-28 23:57:27 +08:00
|
|
|
void push_back_nodup(const til::SExpr *E) {
|
|
|
|
iterator It = std::find_if(begin(), end(), [=](const til::SExpr *E2) {
|
|
|
|
return sx::equals(E, E2);
|
|
|
|
});
|
|
|
|
if (It == end())
|
|
|
|
push_back(E);
|
2012-07-06 05:16:29 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-09-10 00:11:56 +08:00
|
|
|
/// \brief This is a helper class that stores info about the most recent
|
|
|
|
/// accquire of a Lock.
|
|
|
|
///
|
|
|
|
/// The main body of the analysis maps MutexIDs to LockDatas.
|
|
|
|
struct LockData {
|
|
|
|
SourceLocation AcquireLoc;
|
|
|
|
|
|
|
|
/// \brief LKind stores whether a lock is held shared or exclusively.
|
|
|
|
/// Note that this analysis does not currently support either re-entrant
|
|
|
|
/// locking or lock "upgrading" and "downgrading" between exclusive and
|
|
|
|
/// shared.
|
|
|
|
///
|
|
|
|
/// FIXME: add support for re-entrant locking and lock up/downgrading
|
|
|
|
LockKind LKind;
|
2013-05-18 07:02:59 +08:00
|
|
|
bool Asserted; // for asserted locks
|
2012-06-29 06:42:48 +08:00
|
|
|
bool Managed; // for ScopedLockable objects
|
2014-07-28 23:57:27 +08:00
|
|
|
const til::SExpr* UnderlyingMutex; // for ScopedLockable objects
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2013-05-18 07:02:59 +08:00
|
|
|
LockData(SourceLocation AcquireLoc, LockKind LKind, bool M=false,
|
|
|
|
bool Asrt=false)
|
|
|
|
: AcquireLoc(AcquireLoc), LKind(LKind), Asserted(Asrt), Managed(M),
|
2014-07-28 23:57:27 +08:00
|
|
|
UnderlyingMutex(nullptr)
|
2011-12-09 04:23:06 +08:00
|
|
|
{}
|
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
LockData(SourceLocation AcquireLoc, LockKind LKind, const til::SExpr *Mu)
|
2013-05-18 07:02:59 +08:00
|
|
|
: AcquireLoc(AcquireLoc), LKind(LKind), Asserted(false), Managed(false),
|
2012-06-29 06:42:48 +08:00
|
|
|
UnderlyingMutex(Mu)
|
|
|
|
{}
|
2011-09-10 00:11:56 +08:00
|
|
|
|
|
|
|
bool operator==(const LockData &other) const {
|
|
|
|
return AcquireLoc == other.AcquireLoc && LKind == other.LKind;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const LockData &other) const {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const {
|
2011-10-22 00:14:33 +08:00
|
|
|
ID.AddInteger(AcquireLoc.getRawEncoding());
|
|
|
|
ID.AddInteger(LKind);
|
|
|
|
}
|
2012-09-08 01:34:53 +08:00
|
|
|
|
|
|
|
bool isAtLeast(LockKind LK) {
|
|
|
|
return (LK == LK_Shared) || (LKind == LK_Exclusive);
|
|
|
|
}
|
2011-09-10 00:11:56 +08:00
|
|
|
};
|
|
|
|
|
2011-10-22 00:14:33 +08:00
|
|
|
|
2012-08-11 02:39:05 +08:00
|
|
|
/// \brief A FactEntry stores a single fact that is known at a particular point
|
|
|
|
/// in the program execution. Currently, this is information regarding a lock
|
2013-04-02 01:47:37 +08:00
|
|
|
/// that is held at that point.
|
2012-08-11 02:39:05 +08:00
|
|
|
struct FactEntry {
|
2014-07-28 23:57:27 +08:00
|
|
|
const til::SExpr *MutID;
|
2012-08-11 02:39:05 +08:00
|
|
|
LockData LDat;
|
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
FactEntry(const til::SExpr* M, const LockData& L)
|
2012-08-11 02:39:05 +08:00
|
|
|
: MutID(M), LDat(L)
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef unsigned short FactID;
|
|
|
|
|
2013-04-02 01:47:37 +08:00
|
|
|
/// \brief FactManager manages the memory for all facts that are created during
|
2012-08-11 02:39:05 +08:00
|
|
|
/// the analysis of a single routine.
|
|
|
|
class FactManager {
|
|
|
|
private:
|
|
|
|
std::vector<FactEntry> Facts;
|
|
|
|
|
|
|
|
public:
|
2014-07-28 23:57:27 +08:00
|
|
|
FactID newLock(const til::SExpr *M, const LockData& L) {
|
|
|
|
Facts.push_back(FactEntry(M, L));
|
2012-08-11 02:39:05 +08:00
|
|
|
return static_cast<unsigned short>(Facts.size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const FactEntry& operator[](FactID F) const { return Facts[F]; }
|
|
|
|
FactEntry& operator[](FactID F) { return Facts[F]; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// \brief A FactSet is the set of facts that are known to be true at a
|
2013-04-02 01:47:37 +08:00
|
|
|
/// particular program point. FactSets must be small, because they are
|
2012-08-11 02:39:05 +08:00
|
|
|
/// frequently copied, and are thus implemented as a set of indices into a
|
2013-04-02 01:47:37 +08:00
|
|
|
/// table maintained by a FactManager. A typical FactSet only holds 1 or 2
|
2012-08-11 02:39:05 +08:00
|
|
|
/// locks, so we can get away with doing a linear search for lookup. Note
|
|
|
|
/// that a hashtable or map is inappropriate in this case, because lookups
|
|
|
|
/// may involve partial pattern matches, rather than exact matches.
|
|
|
|
class FactSet {
|
|
|
|
private:
|
|
|
|
typedef SmallVector<FactID, 4> FactVec;
|
|
|
|
|
|
|
|
FactVec FactIDs;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef FactVec::iterator iterator;
|
|
|
|
typedef FactVec::const_iterator const_iterator;
|
|
|
|
|
|
|
|
iterator begin() { return FactIDs.begin(); }
|
|
|
|
const_iterator begin() const { return FactIDs.begin(); }
|
|
|
|
|
|
|
|
iterator end() { return FactIDs.end(); }
|
|
|
|
const_iterator end() const { return FactIDs.end(); }
|
|
|
|
|
|
|
|
bool isEmpty() const { return FactIDs.size() == 0; }
|
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
FactID addLock(FactManager& FM, const til::SExpr *M, const LockData& L) {
|
2012-08-11 02:39:05 +08:00
|
|
|
FactID F = FM.newLock(M, L);
|
|
|
|
FactIDs.push_back(F);
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
bool removeLock(FactManager& FM, const til::SExpr *M) {
|
2012-08-11 02:39:05 +08:00
|
|
|
unsigned n = FactIDs.size();
|
|
|
|
if (n == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < n-1; ++i) {
|
2014-07-28 23:57:27 +08:00
|
|
|
if (sx::matches(FM[FactIDs[i]].MutID, M)) {
|
2012-08-11 02:39:05 +08:00
|
|
|
FactIDs[i] = FactIDs[n-1];
|
|
|
|
FactIDs.pop_back();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-07-28 23:57:27 +08:00
|
|
|
if (sx::matches(FM[FactIDs[n-1]].MutID, M)) {
|
2012-08-11 02:39:05 +08:00
|
|
|
FactIDs.pop_back();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
iterator findLockIter(FactManager &FM, const til::SExpr *M) {
|
2014-05-15 02:32:59 +08:00
|
|
|
return std::find_if(begin(), end(), [&](FactID ID) {
|
2014-07-28 23:57:27 +08:00
|
|
|
return sx::matches(FM[ID].MutID, M);
|
2014-05-14 23:01:43 +08:00
|
|
|
});
|
2013-05-21 01:57:55 +08:00
|
|
|
}
|
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
LockData *findLock(FactManager &FM, const til::SExpr *M) const {
|
2014-05-15 02:32:59 +08:00
|
|
|
auto I = std::find_if(begin(), end(), [&](FactID ID) {
|
2014-07-28 23:57:27 +08:00
|
|
|
return sx::matches(FM[ID].MutID, M);
|
2014-05-14 23:01:43 +08:00
|
|
|
});
|
2012-09-08 01:34:53 +08:00
|
|
|
|
2014-05-14 23:01:43 +08:00
|
|
|
return I != end() ? &FM[*I].LDat : nullptr;
|
2012-08-11 02:39:05 +08:00
|
|
|
}
|
2012-09-11 03:58:23 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
LockData *findLockUniv(FactManager &FM, const til::SExpr *M) const {
|
2014-05-15 02:32:59 +08:00
|
|
|
auto I = std::find_if(begin(), end(), [&](FactID ID) -> bool {
|
2014-07-28 23:57:27 +08:00
|
|
|
const til::SExpr *E = FM[ID].MutID;
|
|
|
|
return sx::isUniversal(E) || sx::matches(E, M);
|
2014-05-14 23:01:43 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return I != end() ? &FM[*I].LDat : nullptr;
|
2012-09-11 03:58:23 +08:00
|
|
|
}
|
2012-08-11 02:39:05 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
FactEntry *findPartialMatch(FactManager &FM, const til::SExpr *M) const {
|
2014-05-15 02:32:59 +08:00
|
|
|
auto I = std::find_if(begin(), end(), [&](FactID ID) {
|
2014-07-28 23:57:27 +08:00
|
|
|
return sx::partiallyMatches(FM[ID].MutID, M);
|
2014-05-14 23:01:43 +08:00
|
|
|
});
|
2012-08-11 02:39:05 +08:00
|
|
|
|
2014-05-14 23:01:43 +08:00
|
|
|
return I != end() ? &FM[*I] : nullptr;
|
|
|
|
}
|
|
|
|
};
|
2012-08-11 02:39:05 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
|
2012-08-11 04:19:55 +08:00
|
|
|
/// A Lockset maps each SExpr (defined above) to information about how it has
|
2011-09-10 00:11:56 +08:00
|
|
|
/// been locked.
|
2014-07-28 23:57:27 +08:00
|
|
|
typedef llvm::ImmutableMap<til::SExpr*, LockData> Lockset;
|
2012-04-20 00:48:43 +08:00
|
|
|
typedef llvm::ImmutableMap<const NamedDecl*, unsigned> LocalVarContext;
|
2012-01-07 02:36:09 +08:00
|
|
|
|
|
|
|
class LocalVariableMap;
|
|
|
|
|
2012-02-03 12:45:26 +08:00
|
|
|
/// A side (entry or exit) of a CFG node.
|
|
|
|
enum CFGBlockSide { CBS_Entry, CBS_Exit };
|
2012-01-07 02:36:09 +08:00
|
|
|
|
|
|
|
/// CFGBlockInfo is a struct which contains all the information that is
|
|
|
|
/// maintained for each block in the CFG. See LocalVariableMap for more
|
|
|
|
/// information about the contexts.
|
|
|
|
struct CFGBlockInfo {
|
2012-08-11 02:39:05 +08:00
|
|
|
FactSet EntrySet; // Lockset held at entry to block
|
|
|
|
FactSet ExitSet; // Lockset held at exit from block
|
2012-01-07 02:36:09 +08:00
|
|
|
LocalVarContext EntryContext; // Context held at entry to block
|
|
|
|
LocalVarContext ExitContext; // Context held at exit from block
|
2012-02-03 12:45:26 +08:00
|
|
|
SourceLocation EntryLoc; // Location of first statement in block
|
|
|
|
SourceLocation ExitLoc; // Location of last statement in block.
|
2012-01-07 02:36:09 +08:00
|
|
|
unsigned EntryIndex; // Used to replay contexts later
|
2012-09-22 01:57:00 +08:00
|
|
|
bool Reachable; // Is this block reachable?
|
2012-01-07 02:36:09 +08:00
|
|
|
|
2012-08-11 02:39:05 +08:00
|
|
|
const FactSet &getSet(CFGBlockSide Side) const {
|
2012-02-03 12:45:26 +08:00
|
|
|
return Side == CBS_Entry ? EntrySet : ExitSet;
|
|
|
|
}
|
|
|
|
SourceLocation getLocation(CFGBlockSide Side) const {
|
|
|
|
return Side == CBS_Entry ? EntryLoc : ExitLoc;
|
|
|
|
}
|
|
|
|
|
2012-01-07 02:36:09 +08:00
|
|
|
private:
|
2012-08-11 02:39:05 +08:00
|
|
|
CFGBlockInfo(LocalVarContext EmptyCtx)
|
2012-09-22 01:57:00 +08:00
|
|
|
: EntryContext(EmptyCtx), ExitContext(EmptyCtx), Reachable(false)
|
2012-01-07 02:36:09 +08:00
|
|
|
{ }
|
|
|
|
|
|
|
|
public:
|
2012-08-11 02:39:05 +08:00
|
|
|
static CFGBlockInfo getEmptyBlockInfo(LocalVariableMap &M);
|
2012-01-07 02:36:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// A LocalVariableMap maintains a map from local variables to their currently
|
|
|
|
// valid definitions. It provides SSA-like functionality when traversing the
|
|
|
|
// CFG. Like SSA, each definition or assignment to a variable is assigned a
|
|
|
|
// unique name (an integer), which acts as the SSA name for that definition.
|
|
|
|
// The total set of names is shared among all CFG basic blocks.
|
|
|
|
// Unlike SSA, we do not rewrite expressions to replace local variables declrefs
|
|
|
|
// with their SSA-names. Instead, we compute a Context for each point in the
|
|
|
|
// code, which maps local variables to the appropriate SSA-name. This map
|
|
|
|
// changes with each assignment.
|
|
|
|
//
|
|
|
|
// The map is computed in a single pass over the CFG. Subsequent analyses can
|
|
|
|
// then query the map to find the appropriate Context for a statement, and use
|
|
|
|
// that Context to look up the definitions of variables.
|
|
|
|
class LocalVariableMap {
|
|
|
|
public:
|
|
|
|
typedef LocalVarContext Context;
|
|
|
|
|
|
|
|
/// A VarDefinition consists of an expression, representing the value of the
|
|
|
|
/// variable, along with the context in which that expression should be
|
|
|
|
/// interpreted. A reference VarDefinition does not itself contain this
|
|
|
|
/// information, but instead contains a pointer to a previous VarDefinition.
|
|
|
|
struct VarDefinition {
|
|
|
|
public:
|
|
|
|
friend class LocalVariableMap;
|
|
|
|
|
2012-04-20 00:48:43 +08:00
|
|
|
const NamedDecl *Dec; // The original declaration for this variable.
|
|
|
|
const Expr *Exp; // The expression for this variable, OR
|
|
|
|
unsigned Ref; // Reference to another VarDefinition
|
|
|
|
Context Ctx; // The map with which Exp should be interpreted.
|
2012-01-07 02:36:09 +08:00
|
|
|
|
|
|
|
bool isReference() { return !Exp; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Create ordinary variable definition
|
2012-04-20 00:48:43 +08:00
|
|
|
VarDefinition(const NamedDecl *D, const Expr *E, Context C)
|
2012-01-07 02:36:09 +08:00
|
|
|
: Dec(D), Exp(E), Ref(0), Ctx(C)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
// Create reference to previous definition
|
2012-04-20 00:48:43 +08:00
|
|
|
VarDefinition(const NamedDecl *D, unsigned R, Context C)
|
2014-05-20 12:30:07 +08:00
|
|
|
: Dec(D), Exp(nullptr), Ref(R), Ctx(C)
|
2012-01-07 02:36:09 +08:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
Context::Factory ContextFactory;
|
|
|
|
std::vector<VarDefinition> VarDefinitions;
|
|
|
|
std::vector<unsigned> CtxIndices;
|
|
|
|
std::vector<std::pair<Stmt*, Context> > SavedContexts;
|
|
|
|
|
|
|
|
public:
|
|
|
|
LocalVariableMap() {
|
|
|
|
// index 0 is a placeholder for undefined variables (aka phi-nodes).
|
2014-05-20 12:30:07 +08:00
|
|
|
VarDefinitions.push_back(VarDefinition(nullptr, 0u, getEmptyContext()));
|
2012-01-07 02:36:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Look up a definition, within the given context.
|
2012-04-20 00:48:43 +08:00
|
|
|
const VarDefinition* lookup(const NamedDecl *D, Context Ctx) {
|
2012-01-07 02:36:09 +08:00
|
|
|
const unsigned *i = Ctx.lookup(D);
|
|
|
|
if (!i)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-01-07 02:36:09 +08:00
|
|
|
assert(*i < VarDefinitions.size());
|
|
|
|
return &VarDefinitions[*i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Look up the definition for D within the given context. Returns
|
2012-01-07 03:16:50 +08:00
|
|
|
/// NULL if the expression is not statically known. If successful, also
|
|
|
|
/// modifies Ctx to hold the context of the return Expr.
|
2012-04-20 00:48:43 +08:00
|
|
|
const Expr* lookupExpr(const NamedDecl *D, Context &Ctx) {
|
2012-01-07 02:36:09 +08:00
|
|
|
const unsigned *P = Ctx.lookup(D);
|
|
|
|
if (!P)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-01-07 02:36:09 +08:00
|
|
|
|
|
|
|
unsigned i = *P;
|
|
|
|
while (i > 0) {
|
2012-01-07 03:16:50 +08:00
|
|
|
if (VarDefinitions[i].Exp) {
|
|
|
|
Ctx = VarDefinitions[i].Ctx;
|
2012-01-07 02:36:09 +08:00
|
|
|
return VarDefinitions[i].Exp;
|
2012-01-07 03:16:50 +08:00
|
|
|
}
|
2012-01-07 02:36:09 +08:00
|
|
|
i = VarDefinitions[i].Ref;
|
|
|
|
}
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-01-07 02:36:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Context getEmptyContext() { return ContextFactory.getEmptyMap(); }
|
|
|
|
|
|
|
|
/// Return the next context after processing S. This function is used by
|
|
|
|
/// clients of the class to get the appropriate context when traversing the
|
|
|
|
/// CFG. It must be called for every assignment or DeclStmt.
|
|
|
|
Context getNextContext(unsigned &CtxIndex, Stmt *S, Context C) {
|
|
|
|
if (SavedContexts[CtxIndex+1].first == S) {
|
|
|
|
CtxIndex++;
|
|
|
|
Context Result = SavedContexts[CtxIndex].second;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dumpVarDefinitionName(unsigned i) {
|
|
|
|
if (i == 0) {
|
|
|
|
llvm::errs() << "Undefined";
|
|
|
|
return;
|
|
|
|
}
|
2012-04-20 00:48:43 +08:00
|
|
|
const NamedDecl *Dec = VarDefinitions[i].Dec;
|
2012-01-07 02:36:09 +08:00
|
|
|
if (!Dec) {
|
|
|
|
llvm::errs() << "<<NULL>>";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Dec->printName(llvm::errs());
|
2012-09-06 23:59:27 +08:00
|
|
|
llvm::errs() << "." << i << " " << ((const void*) Dec);
|
2012-01-07 02:36:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Dumps an ASCII representation of the variable map to llvm::errs()
|
|
|
|
void dump() {
|
|
|
|
for (unsigned i = 1, e = VarDefinitions.size(); i < e; ++i) {
|
2012-04-20 00:48:43 +08:00
|
|
|
const Expr *Exp = VarDefinitions[i].Exp;
|
2012-01-07 02:36:09 +08:00
|
|
|
unsigned Ref = VarDefinitions[i].Ref;
|
|
|
|
|
|
|
|
dumpVarDefinitionName(i);
|
|
|
|
llvm::errs() << " = ";
|
|
|
|
if (Exp) Exp->dump();
|
|
|
|
else {
|
|
|
|
dumpVarDefinitionName(Ref);
|
|
|
|
llvm::errs() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Dumps an ASCII representation of a Context to llvm::errs()
|
|
|
|
void dumpContext(Context C) {
|
|
|
|
for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) {
|
2012-04-20 00:48:43 +08:00
|
|
|
const NamedDecl *D = I.getKey();
|
2012-01-07 02:36:09 +08:00
|
|
|
D->printName(llvm::errs());
|
|
|
|
const unsigned *i = C.lookup(D);
|
|
|
|
llvm::errs() << " -> ";
|
|
|
|
dumpVarDefinitionName(*i);
|
|
|
|
llvm::errs() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds the variable map.
|
2014-04-18 05:44:08 +08:00
|
|
|
void traverseCFG(CFG *CFGraph, const PostOrderCFGView *SortedGraph,
|
|
|
|
std::vector<CFGBlockInfo> &BlockInfo);
|
2012-01-07 02:36:09 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// Get the current context index
|
|
|
|
unsigned getContextIndex() { return SavedContexts.size()-1; }
|
|
|
|
|
|
|
|
// Save the current context for later replay
|
|
|
|
void saveContext(Stmt *S, Context C) {
|
|
|
|
SavedContexts.push_back(std::make_pair(S,C));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a new definition to the given context, and returns a new context.
|
|
|
|
// This method should be called when declaring a new variable.
|
2014-05-15 04:42:13 +08:00
|
|
|
Context addDefinition(const NamedDecl *D, const Expr *Exp, Context Ctx) {
|
2012-01-07 02:36:09 +08:00
|
|
|
assert(!Ctx.contains(D));
|
|
|
|
unsigned newID = VarDefinitions.size();
|
|
|
|
Context NewCtx = ContextFactory.add(Ctx, D, newID);
|
|
|
|
VarDefinitions.push_back(VarDefinition(D, Exp, Ctx));
|
|
|
|
return NewCtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new reference to an existing definition.
|
2012-04-20 00:48:43 +08:00
|
|
|
Context addReference(const NamedDecl *D, unsigned i, Context Ctx) {
|
2012-01-07 02:36:09 +08:00
|
|
|
unsigned newID = VarDefinitions.size();
|
|
|
|
Context NewCtx = ContextFactory.add(Ctx, D, newID);
|
|
|
|
VarDefinitions.push_back(VarDefinition(D, i, Ctx));
|
|
|
|
return NewCtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates a definition only if that definition is already in the map.
|
|
|
|
// This method should be called when assigning to an existing variable.
|
2012-04-20 00:48:43 +08:00
|
|
|
Context updateDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) {
|
2012-01-07 02:36:09 +08:00
|
|
|
if (Ctx.contains(D)) {
|
|
|
|
unsigned newID = VarDefinitions.size();
|
|
|
|
Context NewCtx = ContextFactory.remove(Ctx, D);
|
|
|
|
NewCtx = ContextFactory.add(NewCtx, D, newID);
|
|
|
|
VarDefinitions.push_back(VarDefinition(D, Exp, Ctx));
|
|
|
|
return NewCtx;
|
|
|
|
}
|
|
|
|
return Ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes a definition from the context, but keeps the variable name
|
|
|
|
// as a valid variable. The index 0 is a placeholder for cleared definitions.
|
2012-04-20 00:48:43 +08:00
|
|
|
Context clearDefinition(const NamedDecl *D, Context Ctx) {
|
2012-01-07 02:36:09 +08:00
|
|
|
Context NewCtx = Ctx;
|
|
|
|
if (NewCtx.contains(D)) {
|
|
|
|
NewCtx = ContextFactory.remove(NewCtx, D);
|
|
|
|
NewCtx = ContextFactory.add(NewCtx, D, 0);
|
|
|
|
}
|
|
|
|
return NewCtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a definition entirely frmo the context.
|
2012-04-20 00:48:43 +08:00
|
|
|
Context removeDefinition(const NamedDecl *D, Context Ctx) {
|
2012-01-07 02:36:09 +08:00
|
|
|
Context NewCtx = Ctx;
|
|
|
|
if (NewCtx.contains(D)) {
|
|
|
|
NewCtx = ContextFactory.remove(NewCtx, D);
|
|
|
|
}
|
|
|
|
return NewCtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
Context intersectContexts(Context C1, Context C2);
|
|
|
|
Context createReferenceContext(Context C);
|
|
|
|
void intersectBackEdge(Context C1, Context C2);
|
|
|
|
|
|
|
|
friend class VarMapBuilder;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// This has to be defined after LocalVariableMap.
|
2012-08-11 02:39:05 +08:00
|
|
|
CFGBlockInfo CFGBlockInfo::getEmptyBlockInfo(LocalVariableMap &M) {
|
|
|
|
return CFGBlockInfo(M.getEmptyContext());
|
2012-01-07 02:36:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Visitor which builds a LocalVariableMap
|
|
|
|
class VarMapBuilder : public StmtVisitor<VarMapBuilder> {
|
|
|
|
public:
|
|
|
|
LocalVariableMap* VMap;
|
|
|
|
LocalVariableMap::Context Ctx;
|
|
|
|
|
|
|
|
VarMapBuilder(LocalVariableMap *VM, LocalVariableMap::Context C)
|
|
|
|
: VMap(VM), Ctx(C) {}
|
|
|
|
|
|
|
|
void VisitDeclStmt(DeclStmt *S);
|
|
|
|
void VisitBinaryOperator(BinaryOperator *BO);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Add new local variables to the variable map
|
|
|
|
void VarMapBuilder::VisitDeclStmt(DeclStmt *S) {
|
|
|
|
bool modifiedCtx = false;
|
|
|
|
DeclGroupRef DGrp = S->getDeclGroup();
|
2014-05-15 04:42:13 +08:00
|
|
|
for (const auto *D : DGrp) {
|
|
|
|
if (const auto *VD = dyn_cast_or_null<VarDecl>(D)) {
|
|
|
|
const Expr *E = VD->getInit();
|
2012-01-07 02:36:09 +08:00
|
|
|
|
|
|
|
// Add local variables with trivial type to the variable map
|
|
|
|
QualType T = VD->getType();
|
|
|
|
if (T.isTrivialType(VD->getASTContext())) {
|
|
|
|
Ctx = VMap->addDefinition(VD, E, Ctx);
|
|
|
|
modifiedCtx = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (modifiedCtx)
|
|
|
|
VMap->saveContext(S, Ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update local variable definitions in variable map
|
|
|
|
void VarMapBuilder::VisitBinaryOperator(BinaryOperator *BO) {
|
|
|
|
if (!BO->isAssignmentOp())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Expr *LHSExp = BO->getLHS()->IgnoreParenCasts();
|
|
|
|
|
|
|
|
// Update the variable map and current context.
|
|
|
|
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LHSExp)) {
|
|
|
|
ValueDecl *VDec = DRE->getDecl();
|
|
|
|
if (Ctx.lookup(VDec)) {
|
|
|
|
if (BO->getOpcode() == BO_Assign)
|
|
|
|
Ctx = VMap->updateDefinition(VDec, BO->getRHS(), Ctx);
|
|
|
|
else
|
|
|
|
// FIXME -- handle compound assignment operators
|
|
|
|
Ctx = VMap->clearDefinition(VDec, Ctx);
|
|
|
|
VMap->saveContext(BO, Ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Computes the intersection of two contexts. The intersection is the
|
|
|
|
// set of variables which have the same definition in both contexts;
|
|
|
|
// variables with different definitions are discarded.
|
|
|
|
LocalVariableMap::Context
|
|
|
|
LocalVariableMap::intersectContexts(Context C1, Context C2) {
|
|
|
|
Context Result = C1;
|
2014-05-15 04:42:13 +08:00
|
|
|
for (const auto &P : C1) {
|
|
|
|
const NamedDecl *Dec = P.first;
|
2012-01-07 02:36:09 +08:00
|
|
|
const unsigned *i2 = C2.lookup(Dec);
|
|
|
|
if (!i2) // variable doesn't exist on second path
|
|
|
|
Result = removeDefinition(Dec, Result);
|
2014-05-15 04:42:13 +08:00
|
|
|
else if (*i2 != P.second) // variable exists, but has different definition
|
2012-01-07 02:36:09 +08:00
|
|
|
Result = clearDefinition(Dec, Result);
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For every variable in C, create a new variable that refers to the
|
|
|
|
// definition in C. Return a new context that contains these new variables.
|
|
|
|
// (We use this for a naive implementation of SSA on loop back-edges.)
|
|
|
|
LocalVariableMap::Context LocalVariableMap::createReferenceContext(Context C) {
|
|
|
|
Context Result = getEmptyContext();
|
2014-05-15 04:42:13 +08:00
|
|
|
for (const auto &P : C)
|
|
|
|
Result = addReference(P.first, P.second, Result);
|
2012-01-07 02:36:09 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This routine also takes the intersection of C1 and C2, but it does so by
|
|
|
|
// altering the VarDefinitions. C1 must be the result of an earlier call to
|
|
|
|
// createReferenceContext.
|
|
|
|
void LocalVariableMap::intersectBackEdge(Context C1, Context C2) {
|
2014-05-15 04:42:13 +08:00
|
|
|
for (const auto &P : C1) {
|
|
|
|
unsigned i1 = P.second;
|
2012-01-07 02:36:09 +08:00
|
|
|
VarDefinition *VDef = &VarDefinitions[i1];
|
|
|
|
assert(VDef->isReference());
|
|
|
|
|
2014-05-15 04:42:13 +08:00
|
|
|
const unsigned *i2 = C2.lookup(P.first);
|
2012-01-07 02:36:09 +08:00
|
|
|
if (!i2 || (*i2 != i1))
|
|
|
|
VDef->Ref = 0; // Mark this variable as undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Traverse the CFG in topological order, so all predecessors of a block
|
|
|
|
// (excluding back-edges) are visited before the block itself. At
|
|
|
|
// each point in the code, we calculate a Context, which holds the set of
|
|
|
|
// variable definitions which are visible at that point in execution.
|
|
|
|
// Visible variables are mapped to their definitions using an array that
|
|
|
|
// contains all definitions.
|
|
|
|
//
|
|
|
|
// At join points in the CFG, the set is computed as the intersection of
|
|
|
|
// the incoming sets along each edge, E.g.
|
|
|
|
//
|
|
|
|
// { Context | VarDefinitions }
|
|
|
|
// int x = 0; { x -> x1 | x1 = 0 }
|
|
|
|
// int y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 }
|
|
|
|
// if (b) x = 1; { x -> x2, y -> y1 | x2 = 1, y1 = 0, ... }
|
|
|
|
// else x = 2; { x -> x3, y -> y1 | x3 = 2, x2 = 1, ... }
|
|
|
|
// ... { y -> y1 (x is unknown) | x3 = 2, x2 = 1, ... }
|
|
|
|
//
|
|
|
|
// This is essentially a simpler and more naive version of the standard SSA
|
|
|
|
// algorithm. Those definitions that remain in the intersection are from blocks
|
|
|
|
// that strictly dominate the current block. We do not bother to insert proper
|
|
|
|
// phi nodes, because they are not used in our analysis; instead, wherever
|
|
|
|
// a phi node would be required, we simply remove that definition from the
|
|
|
|
// context (E.g. x above).
|
|
|
|
//
|
|
|
|
// The initial traversal does not capture back-edges, so those need to be
|
|
|
|
// handled on a separate pass. Whenever the first pass encounters an
|
|
|
|
// incoming back edge, it duplicates the context, creating new definitions
|
|
|
|
// that refer back to the originals. (These correspond to places where SSA
|
|
|
|
// might have to insert a phi node.) On the second pass, these definitions are
|
2012-07-23 16:59:39 +08:00
|
|
|
// set to NULL if the variable has changed on the back-edge (i.e. a phi
|
2012-01-07 02:36:09 +08:00
|
|
|
// node was actually required.) E.g.
|
|
|
|
//
|
|
|
|
// { Context | VarDefinitions }
|
|
|
|
// int x = 0, y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 }
|
|
|
|
// while (b) { x -> x2, y -> y1 | [1st:] x2=x1; [2nd:] x2=NULL; }
|
|
|
|
// x = x+1; { x -> x3, y -> y1 | x3 = x2 + 1, ... }
|
|
|
|
// ... { y -> y1 | x3 = 2, x2 = 1, ... }
|
|
|
|
//
|
|
|
|
void LocalVariableMap::traverseCFG(CFG *CFGraph,
|
2014-04-18 05:44:08 +08:00
|
|
|
const PostOrderCFGView *SortedGraph,
|
2012-01-07 02:36:09 +08:00
|
|
|
std::vector<CFGBlockInfo> &BlockInfo) {
|
|
|
|
PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph);
|
|
|
|
|
|
|
|
CtxIndices.resize(CFGraph->getNumBlockIDs());
|
|
|
|
|
2014-04-18 05:44:08 +08:00
|
|
|
for (const auto *CurrBlock : *SortedGraph) {
|
2012-01-07 02:36:09 +08:00
|
|
|
int CurrBlockID = CurrBlock->getBlockID();
|
|
|
|
CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID];
|
|
|
|
|
|
|
|
VisitedBlocks.insert(CurrBlock);
|
|
|
|
|
|
|
|
// Calculate the entry context for the current block
|
|
|
|
bool HasBackEdges = false;
|
|
|
|
bool CtxInit = true;
|
|
|
|
for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(),
|
|
|
|
PE = CurrBlock->pred_end(); PI != PE; ++PI) {
|
|
|
|
// if *PI -> CurrBlock is a back edge, so skip it
|
2014-05-20 12:30:07 +08:00
|
|
|
if (*PI == nullptr || !VisitedBlocks.alreadySet(*PI)) {
|
2012-01-07 02:36:09 +08:00
|
|
|
HasBackEdges = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PrevBlockID = (*PI)->getBlockID();
|
|
|
|
CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
|
|
|
|
|
|
|
|
if (CtxInit) {
|
|
|
|
CurrBlockInfo->EntryContext = PrevBlockInfo->ExitContext;
|
|
|
|
CtxInit = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CurrBlockInfo->EntryContext =
|
|
|
|
intersectContexts(CurrBlockInfo->EntryContext,
|
|
|
|
PrevBlockInfo->ExitContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Duplicate the context if we have back-edges, so we can call
|
|
|
|
// intersectBackEdges later.
|
|
|
|
if (HasBackEdges)
|
|
|
|
CurrBlockInfo->EntryContext =
|
|
|
|
createReferenceContext(CurrBlockInfo->EntryContext);
|
|
|
|
|
|
|
|
// Create a starting context index for the current block
|
2014-05-20 12:30:07 +08:00
|
|
|
saveContext(nullptr, CurrBlockInfo->EntryContext);
|
2012-01-07 02:36:09 +08:00
|
|
|
CurrBlockInfo->EntryIndex = getContextIndex();
|
|
|
|
|
|
|
|
// Visit all the statements in the basic block.
|
|
|
|
VarMapBuilder VMapBuilder(this, CurrBlockInfo->EntryContext);
|
|
|
|
for (CFGBlock::const_iterator BI = CurrBlock->begin(),
|
|
|
|
BE = CurrBlock->end(); BI != BE; ++BI) {
|
|
|
|
switch (BI->getKind()) {
|
|
|
|
case CFGElement::Statement: {
|
2013-02-22 04:58:29 +08:00
|
|
|
CFGStmt CS = BI->castAs<CFGStmt>();
|
|
|
|
VMapBuilder.Visit(const_cast<Stmt*>(CS.getStmt()));
|
2012-01-07 02:36:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CurrBlockInfo->ExitContext = VMapBuilder.Ctx;
|
|
|
|
|
|
|
|
// Mark variables on back edges as "unknown" if they've been changed.
|
|
|
|
for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
|
|
|
|
SE = CurrBlock->succ_end(); SI != SE; ++SI) {
|
|
|
|
// if CurrBlock -> *SI is *not* a back edge
|
2014-05-20 12:30:07 +08:00
|
|
|
if (*SI == nullptr || !VisitedBlocks.alreadySet(*SI))
|
2012-01-07 02:36:09 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
CFGBlock *FirstLoopBlock = *SI;
|
|
|
|
Context LoopBegin = BlockInfo[FirstLoopBlock->getBlockID()].EntryContext;
|
|
|
|
Context LoopEnd = CurrBlockInfo->ExitContext;
|
|
|
|
intersectBackEdge(LoopBegin, LoopEnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put an extra entry at the end of the indexed context array
|
|
|
|
unsigned exitID = CFGraph->getExit().getBlockID();
|
2014-05-20 12:30:07 +08:00
|
|
|
saveContext(nullptr, BlockInfo[exitID].ExitContext);
|
2012-01-07 02:36:09 +08:00
|
|
|
}
|
|
|
|
|
2012-02-03 12:45:26 +08:00
|
|
|
/// Find the appropriate source locations to use when producing diagnostics for
|
|
|
|
/// each block in the CFG.
|
|
|
|
static void findBlockLocations(CFG *CFGraph,
|
2014-04-18 05:44:08 +08:00
|
|
|
const PostOrderCFGView *SortedGraph,
|
2012-02-03 12:45:26 +08:00
|
|
|
std::vector<CFGBlockInfo> &BlockInfo) {
|
2014-04-18 05:44:08 +08:00
|
|
|
for (const auto *CurrBlock : *SortedGraph) {
|
2012-02-03 12:45:26 +08:00
|
|
|
CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlock->getBlockID()];
|
|
|
|
|
|
|
|
// Find the source location of the last statement in the block, if the
|
|
|
|
// block is not empty.
|
|
|
|
if (const Stmt *S = CurrBlock->getTerminator()) {
|
|
|
|
CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc = S->getLocStart();
|
|
|
|
} else {
|
|
|
|
for (CFGBlock::const_reverse_iterator BI = CurrBlock->rbegin(),
|
|
|
|
BE = CurrBlock->rend(); BI != BE; ++BI) {
|
|
|
|
// FIXME: Handle other CFGElement kinds.
|
2013-02-23 08:29:34 +08:00
|
|
|
if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>()) {
|
|
|
|
CurrBlockInfo->ExitLoc = CS->getStmt()->getLocStart();
|
2012-02-03 12:45:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CurrBlockInfo->ExitLoc.isInvalid()) {
|
|
|
|
// This block contains at least one statement. Find the source location
|
|
|
|
// of the first statement in the block.
|
|
|
|
for (CFGBlock::const_iterator BI = CurrBlock->begin(),
|
|
|
|
BE = CurrBlock->end(); BI != BE; ++BI) {
|
|
|
|
// FIXME: Handle other CFGElement kinds.
|
2013-02-23 08:29:34 +08:00
|
|
|
if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>()) {
|
|
|
|
CurrBlockInfo->EntryLoc = CS->getStmt()->getLocStart();
|
2012-02-03 12:45:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (CurrBlock->pred_size() == 1 && *CurrBlock->pred_begin() &&
|
|
|
|
CurrBlock != &CFGraph->getExit()) {
|
|
|
|
// The block is empty, and has a single predecessor. Use its exit
|
|
|
|
// location.
|
|
|
|
CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc =
|
|
|
|
BlockInfo[(*CurrBlock->pred_begin())->getBlockID()].ExitLoc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-07 02:36:09 +08:00
|
|
|
|
|
|
|
/// \brief Class which implements the core thread safety analysis routines.
|
|
|
|
class ThreadSafetyAnalyzer {
|
|
|
|
friend class BuildLockset;
|
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
llvm::BumpPtrAllocator Bpa;
|
|
|
|
threadSafety::til::MemRegionRef Arena;
|
|
|
|
threadSafety::SExprBuilder SxBuilder;
|
|
|
|
|
2012-04-20 00:48:43 +08:00
|
|
|
ThreadSafetyHandler &Handler;
|
|
|
|
LocalVariableMap LocalVarMap;
|
2012-08-11 02:39:05 +08:00
|
|
|
FactManager FactMan;
|
2012-04-20 00:48:43 +08:00
|
|
|
std::vector<CFGBlockInfo> BlockInfo;
|
2012-01-07 02:36:09 +08:00
|
|
|
|
|
|
|
public:
|
2014-07-28 23:57:27 +08:00
|
|
|
ThreadSafetyAnalyzer(ThreadSafetyHandler &H)
|
|
|
|
: Arena(&Bpa), SxBuilder(Arena), Handler(H) {}
|
2012-01-07 02:36:09 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
void addLock(FactSet &FSet, const til::SExpr *Mutex, const LockData &LDat,
|
2014-04-02 05:43:23 +08:00
|
|
|
StringRef DiagKind);
|
2014-07-28 23:57:27 +08:00
|
|
|
void removeLock(FactSet &FSet, const til::SExpr *Mutex,
|
|
|
|
SourceLocation UnlockLoc, bool FullyRemove, LockKind Kind,
|
|
|
|
StringRef DiagKind);
|
2011-10-22 00:14:33 +08:00
|
|
|
|
2012-07-06 05:16:29 +08:00
|
|
|
template <typename AttrType>
|
|
|
|
void getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, Expr *Exp,
|
2014-05-20 12:30:07 +08:00
|
|
|
const NamedDecl *D, VarDecl *SelfDecl = nullptr);
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2012-01-07 03:16:50 +08:00
|
|
|
template <class AttrType>
|
2012-07-06 05:16:29 +08:00
|
|
|
void getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, Expr *Exp,
|
|
|
|
const NamedDecl *D,
|
|
|
|
const CFGBlock *PredBlock, const CFGBlock *CurrBlock,
|
|
|
|
Expr *BrE, bool Neg);
|
|
|
|
|
2012-04-20 00:48:43 +08:00
|
|
|
const CallExpr* getTrylockCallExpr(const Stmt *Cond, LocalVarContext C,
|
|
|
|
bool &Negate);
|
2012-01-07 03:16:50 +08:00
|
|
|
|
2012-08-11 02:39:05 +08:00
|
|
|
void getEdgeLockset(FactSet &Result, const FactSet &ExitSet,
|
|
|
|
const CFGBlock* PredBlock,
|
|
|
|
const CFGBlock *CurrBlock);
|
2012-06-23 01:07:28 +08:00
|
|
|
|
2012-08-11 02:39:05 +08:00
|
|
|
void intersectAndWarn(FactSet &FSet1, const FactSet &FSet2,
|
|
|
|
SourceLocation JoinLoc,
|
|
|
|
LockErrorKind LEK1, LockErrorKind LEK2,
|
|
|
|
bool Modify=true);
|
2012-07-03 06:16:54 +08:00
|
|
|
|
2012-08-11 02:39:05 +08:00
|
|
|
void intersectAndWarn(FactSet &FSet1, const FactSet &FSet2,
|
|
|
|
SourceLocation JoinLoc, LockErrorKind LEK1,
|
|
|
|
bool Modify=true) {
|
|
|
|
intersectAndWarn(FSet1, FSet2, JoinLoc, LEK1, LEK1, Modify);
|
2012-07-03 06:16:54 +08:00
|
|
|
}
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2012-04-20 00:48:43 +08:00
|
|
|
void runAnalysis(AnalysisDeclContext &AC);
|
2011-09-10 00:11:56 +08:00
|
|
|
};
|
|
|
|
|
2014-04-02 05:43:23 +08:00
|
|
|
/// \brief Gets the value decl pointer from DeclRefExprs or MemberExprs.
|
|
|
|
static const ValueDecl *getValueDecl(const Expr *Exp) {
|
|
|
|
if (const auto *CE = dyn_cast<ImplicitCastExpr>(Exp))
|
|
|
|
return getValueDecl(CE->getSubExpr());
|
|
|
|
|
|
|
|
if (const auto *DR = dyn_cast<DeclRefExpr>(Exp))
|
|
|
|
return DR->getDecl();
|
|
|
|
|
|
|
|
if (const auto *ME = dyn_cast<MemberExpr>(Exp))
|
|
|
|
return ME->getMemberDecl();
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Ty>
|
2014-05-02 21:35:42 +08:00
|
|
|
class has_arg_iterator_range {
|
2014-04-02 05:43:23 +08:00
|
|
|
typedef char yes[1];
|
|
|
|
typedef char no[2];
|
|
|
|
|
|
|
|
template <typename Inner>
|
2014-05-02 21:35:42 +08:00
|
|
|
static yes& test(Inner *I, decltype(I->args()) * = nullptr);
|
2014-04-02 05:43:23 +08:00
|
|
|
|
|
|
|
template <typename>
|
|
|
|
static no& test(...);
|
|
|
|
|
|
|
|
public:
|
|
|
|
static const bool value = sizeof(test<Ty>(nullptr)) == sizeof(yes);
|
|
|
|
};
|
|
|
|
|
|
|
|
static StringRef ClassifyDiagnostic(const CapabilityAttr *A) {
|
|
|
|
return A->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
static StringRef ClassifyDiagnostic(QualType VDT) {
|
|
|
|
// We need to look at the declaration of the type of the value to determine
|
|
|
|
// which it is. The type should either be a record or a typedef, or a pointer
|
|
|
|
// or reference thereof.
|
|
|
|
if (const auto *RT = VDT->getAs<RecordType>()) {
|
|
|
|
if (const auto *RD = RT->getDecl())
|
|
|
|
if (const auto *CA = RD->getAttr<CapabilityAttr>())
|
|
|
|
return ClassifyDiagnostic(CA);
|
|
|
|
} else if (const auto *TT = VDT->getAs<TypedefType>()) {
|
|
|
|
if (const auto *TD = TT->getDecl())
|
|
|
|
if (const auto *CA = TD->getAttr<CapabilityAttr>())
|
|
|
|
return ClassifyDiagnostic(CA);
|
|
|
|
} else if (VDT->isPointerType() || VDT->isReferenceType())
|
|
|
|
return ClassifyDiagnostic(VDT->getPointeeType());
|
|
|
|
|
|
|
|
return "mutex";
|
|
|
|
}
|
|
|
|
|
|
|
|
static StringRef ClassifyDiagnostic(const ValueDecl *VD) {
|
|
|
|
assert(VD && "No ValueDecl passed");
|
|
|
|
|
|
|
|
// The ValueDecl is the declaration of a mutex or role (hopefully).
|
|
|
|
return ClassifyDiagnostic(VD->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename AttrTy>
|
2014-05-02 21:35:42 +08:00
|
|
|
static typename std::enable_if<!has_arg_iterator_range<AttrTy>::value,
|
2014-04-02 05:43:23 +08:00
|
|
|
StringRef>::type
|
|
|
|
ClassifyDiagnostic(const AttrTy *A) {
|
|
|
|
if (const ValueDecl *VD = getValueDecl(A->getArg()))
|
|
|
|
return ClassifyDiagnostic(VD);
|
|
|
|
return "mutex";
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename AttrTy>
|
2014-05-02 21:35:42 +08:00
|
|
|
static typename std::enable_if<has_arg_iterator_range<AttrTy>::value,
|
2014-04-02 05:43:23 +08:00
|
|
|
StringRef>::type
|
|
|
|
ClassifyDiagnostic(const AttrTy *A) {
|
2014-05-02 21:35:42 +08:00
|
|
|
for (const auto *Arg : A->args()) {
|
|
|
|
if (const ValueDecl *VD = getValueDecl(Arg))
|
2014-04-02 05:43:23 +08:00
|
|
|
return ClassifyDiagnostic(VD);
|
|
|
|
}
|
|
|
|
return "mutex";
|
|
|
|
}
|
2012-04-20 00:48:43 +08:00
|
|
|
|
2011-09-10 00:11:56 +08:00
|
|
|
/// \brief Add a new lock to the lockset, warning if the lock is already there.
|
2011-12-09 04:23:06 +08:00
|
|
|
/// \param Mutex -- the Mutex expression for the lock
|
|
|
|
/// \param LDat -- the LockData for the lock
|
2014-07-28 23:57:27 +08:00
|
|
|
void ThreadSafetyAnalyzer::addLock(FactSet &FSet, const til::SExpr *Mutex,
|
2014-04-02 05:43:23 +08:00
|
|
|
const LockData &LDat, StringRef DiagKind) {
|
2011-12-09 04:23:06 +08:00
|
|
|
// FIXME: deal with acquired before/after annotations.
|
2011-09-10 00:11:56 +08:00
|
|
|
// FIXME: Don't always warn when we have support for reentrant locks.
|
2014-07-28 23:57:27 +08:00
|
|
|
if (sx::shouldIgnore(Mutex))
|
2012-09-01 05:57:32 +08:00
|
|
|
return;
|
|
|
|
|
2012-08-11 02:39:05 +08:00
|
|
|
if (FSet.findLock(FactMan, Mutex)) {
|
2013-05-18 07:02:59 +08:00
|
|
|
if (!LDat.Asserted)
|
2014-07-28 23:57:27 +08:00
|
|
|
Handler.handleDoubleLock(DiagKind, sx::toString(Mutex), LDat.AcquireLoc);
|
2012-04-20 00:48:43 +08:00
|
|
|
} else {
|
2012-08-11 02:39:05 +08:00
|
|
|
FSet.addLock(FactMan, Mutex, LDat);
|
2012-04-20 00:48:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-10 00:11:56 +08:00
|
|
|
/// \brief Remove a lock from the lockset, warning if the lock is not there.
|
2012-08-23 07:50:41 +08:00
|
|
|
/// \param Mutex The lock expression corresponding to the lock to be removed
|
2011-09-10 00:11:56 +08:00
|
|
|
/// \param UnlockLoc The source location of the unlock (only used in error msg)
|
2014-07-28 23:57:27 +08:00
|
|
|
void ThreadSafetyAnalyzer::removeLock(FactSet &FSet, const til::SExpr *Mutex,
|
2012-08-11 02:39:05 +08:00
|
|
|
SourceLocation UnlockLoc,
|
2014-04-02 05:43:23 +08:00
|
|
|
bool FullyRemove, LockKind ReceivedKind,
|
|
|
|
StringRef DiagKind) {
|
2014-07-28 23:57:27 +08:00
|
|
|
if (sx::shouldIgnore(Mutex))
|
2012-09-01 05:57:32 +08:00
|
|
|
return;
|
|
|
|
|
2012-08-11 02:39:05 +08:00
|
|
|
const LockData *LDat = FSet.findLock(FactMan, Mutex);
|
2012-04-20 00:48:43 +08:00
|
|
|
if (!LDat) {
|
2014-07-28 23:57:27 +08:00
|
|
|
Handler.handleUnmatchedUnlock(DiagKind, sx::toString(Mutex), UnlockLoc);
|
2012-08-11 02:39:05 +08:00
|
|
|
return;
|
2012-04-20 00:48:43 +08:00
|
|
|
}
|
2012-08-11 02:39:05 +08:00
|
|
|
|
2014-03-21 22:48:48 +08:00
|
|
|
// Generic lock removal doesn't care about lock kind mismatches, but
|
|
|
|
// otherwise diagnose when the lock kinds are mismatched.
|
|
|
|
if (ReceivedKind != LK_Generic && LDat->LKind != ReceivedKind) {
|
2014-07-28 23:57:27 +08:00
|
|
|
Handler.handleIncorrectUnlockKind(DiagKind, sx::toString(Mutex),
|
|
|
|
LDat->LKind, ReceivedKind, UnlockLoc);
|
2014-03-21 22:48:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
if (LDat->UnderlyingMutex) {
|
2012-07-06 05:16:29 +08:00
|
|
|
// This is scoped lockable object, which manages the real mutex.
|
|
|
|
if (FullyRemove) {
|
|
|
|
// We're destroying the managing object.
|
|
|
|
// Remove the underlying mutex if it exists; but don't warn.
|
2012-08-11 02:39:05 +08:00
|
|
|
if (FSet.findLock(FactMan, LDat->UnderlyingMutex))
|
|
|
|
FSet.removeLock(FactMan, LDat->UnderlyingMutex);
|
2012-07-06 05:16:29 +08:00
|
|
|
} else {
|
|
|
|
// We're releasing the underlying mutex, but not destroying the
|
|
|
|
// managing object. Warn on dual release.
|
2012-08-11 02:39:05 +08:00
|
|
|
if (!FSet.findLock(FactMan, LDat->UnderlyingMutex)) {
|
2014-04-02 05:43:23 +08:00
|
|
|
Handler.handleUnmatchedUnlock(
|
2014-07-28 23:57:27 +08:00
|
|
|
DiagKind, sx::toString(LDat->UnderlyingMutex), UnlockLoc);
|
2012-07-06 05:16:29 +08:00
|
|
|
}
|
2012-08-11 02:39:05 +08:00
|
|
|
FSet.removeLock(FactMan, LDat->UnderlyingMutex);
|
|
|
|
return;
|
2012-06-29 06:42:48 +08:00
|
|
|
}
|
2011-12-09 04:23:06 +08:00
|
|
|
}
|
2012-08-11 02:39:05 +08:00
|
|
|
FSet.removeLock(FactMan, Mutex);
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
|
2012-06-29 06:42:48 +08:00
|
|
|
|
2012-07-06 05:16:29 +08:00
|
|
|
/// \brief Extract the list of mutexIDs from the attribute on an expression,
|
|
|
|
/// and push them onto Mtxs, discarding any duplicates.
|
2011-10-22 00:14:33 +08:00
|
|
|
template <typename AttrType>
|
2012-07-06 05:16:29 +08:00
|
|
|
void ThreadSafetyAnalyzer::getMutexIDs(MutexIDList &Mtxs, AttrType *Attr,
|
2012-10-06 06:38:19 +08:00
|
|
|
Expr *Exp, const NamedDecl *D,
|
|
|
|
VarDecl *SelfDecl) {
|
2011-10-22 00:14:33 +08:00
|
|
|
if (Attr->args_size() == 0) {
|
|
|
|
// The mutex held is the "this" object.
|
2014-07-28 23:57:27 +08:00
|
|
|
til::SExpr *Mu = SxBuilder.translateAttrExpr(nullptr, D, Exp, SelfDecl);
|
|
|
|
if (Mu && isa<til::Undefined>(Mu)) {
|
|
|
|
warnInvalidLock(Handler, nullptr, D, Exp, ClassifyDiagnostic(Attr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//else
|
|
|
|
if (Mu)
|
2012-07-06 05:16:29 +08:00
|
|
|
Mtxs.push_back_nodup(Mu);
|
|
|
|
return;
|
2011-10-22 00:14:33 +08:00
|
|
|
}
|
|
|
|
|
2014-05-02 21:35:42 +08:00
|
|
|
for (const auto *Arg : Attr->args()) {
|
2014-07-28 23:57:27 +08:00
|
|
|
til::SExpr *Mu = SxBuilder.translateAttrExpr(Arg, D, Exp, SelfDecl);
|
|
|
|
if (Mu && isa<til::Undefined>(Mu)) {
|
|
|
|
warnInvalidLock(Handler, nullptr, D, Exp, ClassifyDiagnostic(Attr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//else
|
|
|
|
if (Mu)
|
2012-07-06 05:16:29 +08:00
|
|
|
Mtxs.push_back_nodup(Mu);
|
2012-04-20 00:48:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-06 05:16:29 +08:00
|
|
|
/// \brief Extract the list of mutexIDs from a trylock attribute. If the
|
|
|
|
/// trylock applies to the given edge, then push them onto Mtxs, discarding
|
|
|
|
/// any duplicates.
|
2012-04-20 00:48:43 +08:00
|
|
|
template <class AttrType>
|
2012-07-06 05:16:29 +08:00
|
|
|
void ThreadSafetyAnalyzer::getMutexIDs(MutexIDList &Mtxs, AttrType *Attr,
|
|
|
|
Expr *Exp, const NamedDecl *D,
|
|
|
|
const CFGBlock *PredBlock,
|
|
|
|
const CFGBlock *CurrBlock,
|
|
|
|
Expr *BrE, bool Neg) {
|
2012-04-20 00:48:43 +08:00
|
|
|
// Find out which branch has the lock
|
2014-05-14 21:03:55 +08:00
|
|
|
bool branch = false;
|
|
|
|
if (CXXBoolLiteralExpr *BLE = dyn_cast_or_null<CXXBoolLiteralExpr>(BrE))
|
2012-04-20 00:48:43 +08:00
|
|
|
branch = BLE->getValue();
|
2014-05-14 21:03:55 +08:00
|
|
|
else if (IntegerLiteral *ILE = dyn_cast_or_null<IntegerLiteral>(BrE))
|
2012-04-20 00:48:43 +08:00
|
|
|
branch = ILE->getValue().getBoolValue();
|
2014-05-14 21:03:55 +08:00
|
|
|
|
2012-04-20 00:48:43 +08:00
|
|
|
int branchnum = branch ? 0 : 1;
|
2014-05-14 21:03:55 +08:00
|
|
|
if (Neg)
|
|
|
|
branchnum = !branchnum;
|
2012-04-20 00:48:43 +08:00
|
|
|
|
|
|
|
// If we've taken the trylock branch, then add the lock
|
|
|
|
int i = 0;
|
|
|
|
for (CFGBlock::const_succ_iterator SI = PredBlock->succ_begin(),
|
|
|
|
SE = PredBlock->succ_end(); SI != SE && i < 2; ++SI, ++i) {
|
2014-05-14 21:03:55 +08:00
|
|
|
if (*SI == CurrBlock && i == branchnum)
|
2012-07-06 05:16:29 +08:00
|
|
|
getMutexIDs(Mtxs, Attr, Exp, D);
|
2012-04-20 00:48:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-11 05:47:55 +08:00
|
|
|
bool getStaticBooleanValue(Expr* E, bool& TCond) {
|
|
|
|
if (isa<CXXNullPtrLiteralExpr>(E) || isa<GNUNullExpr>(E)) {
|
|
|
|
TCond = false;
|
|
|
|
return true;
|
|
|
|
} else if (CXXBoolLiteralExpr *BLE = dyn_cast<CXXBoolLiteralExpr>(E)) {
|
|
|
|
TCond = BLE->getValue();
|
|
|
|
return true;
|
|
|
|
} else if (IntegerLiteral *ILE = dyn_cast<IntegerLiteral>(E)) {
|
|
|
|
TCond = ILE->getValue().getBoolValue();
|
|
|
|
return true;
|
|
|
|
} else if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) {
|
|
|
|
return getStaticBooleanValue(CE->getSubExpr(), TCond);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-20 00:48:43 +08:00
|
|
|
// If Cond can be traced back to a function call, return the call expression.
|
|
|
|
// The negate variable should be called with false, and will be set to true
|
|
|
|
// if the function call is negated, e.g. if (!mu.tryLock(...))
|
|
|
|
const CallExpr* ThreadSafetyAnalyzer::getTrylockCallExpr(const Stmt *Cond,
|
|
|
|
LocalVarContext C,
|
|
|
|
bool &Negate) {
|
|
|
|
if (!Cond)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-04-20 00:48:43 +08:00
|
|
|
|
|
|
|
if (const CallExpr *CallExp = dyn_cast<CallExpr>(Cond)) {
|
|
|
|
return CallExp;
|
|
|
|
}
|
2012-07-11 05:47:55 +08:00
|
|
|
else if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) {
|
|
|
|
return getTrylockCallExpr(PE->getSubExpr(), C, Negate);
|
|
|
|
}
|
2012-04-20 00:48:43 +08:00
|
|
|
else if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Cond)) {
|
|
|
|
return getTrylockCallExpr(CE->getSubExpr(), C, Negate);
|
|
|
|
}
|
2012-09-06 04:01:16 +08:00
|
|
|
else if (const ExprWithCleanups* EWC = dyn_cast<ExprWithCleanups>(Cond)) {
|
|
|
|
return getTrylockCallExpr(EWC->getSubExpr(), C, Negate);
|
|
|
|
}
|
2012-04-20 00:48:43 +08:00
|
|
|
else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Cond)) {
|
|
|
|
const Expr *E = LocalVarMap.lookupExpr(DRE->getDecl(), C);
|
|
|
|
return getTrylockCallExpr(E, C, Negate);
|
|
|
|
}
|
|
|
|
else if (const UnaryOperator *UOP = dyn_cast<UnaryOperator>(Cond)) {
|
|
|
|
if (UOP->getOpcode() == UO_LNot) {
|
|
|
|
Negate = !Negate;
|
|
|
|
return getTrylockCallExpr(UOP->getSubExpr(), C, Negate);
|
|
|
|
}
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-07-11 05:47:55 +08:00
|
|
|
}
|
|
|
|
else if (const BinaryOperator *BOP = dyn_cast<BinaryOperator>(Cond)) {
|
|
|
|
if (BOP->getOpcode() == BO_EQ || BOP->getOpcode() == BO_NE) {
|
|
|
|
if (BOP->getOpcode() == BO_NE)
|
|
|
|
Negate = !Negate;
|
|
|
|
|
|
|
|
bool TCond = false;
|
|
|
|
if (getStaticBooleanValue(BOP->getRHS(), TCond)) {
|
|
|
|
if (!TCond) Negate = !Negate;
|
|
|
|
return getTrylockCallExpr(BOP->getLHS(), C, Negate);
|
|
|
|
}
|
2013-08-16 07:06:33 +08:00
|
|
|
TCond = false;
|
|
|
|
if (getStaticBooleanValue(BOP->getLHS(), TCond)) {
|
2012-07-11 05:47:55 +08:00
|
|
|
if (!TCond) Negate = !Negate;
|
|
|
|
return getTrylockCallExpr(BOP->getRHS(), C, Negate);
|
|
|
|
}
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-07-11 05:47:55 +08:00
|
|
|
}
|
2013-08-16 07:06:33 +08:00
|
|
|
if (BOP->getOpcode() == BO_LAnd) {
|
|
|
|
// LHS must have been evaluated in a different block.
|
|
|
|
return getTrylockCallExpr(BOP->getRHS(), C, Negate);
|
|
|
|
}
|
|
|
|
if (BOP->getOpcode() == BO_LOr) {
|
|
|
|
return getTrylockCallExpr(BOP->getRHS(), C, Negate);
|
|
|
|
}
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-04-20 00:48:43 +08:00
|
|
|
}
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-04-20 00:48:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-23 01:07:28 +08:00
|
|
|
/// \brief Find the lockset that holds on the edge between PredBlock
|
|
|
|
/// and CurrBlock. The edge set is the exit set of PredBlock (passed
|
|
|
|
/// as the ExitSet parameter) plus any trylocks, which are conditionally held.
|
2012-08-11 02:39:05 +08:00
|
|
|
void ThreadSafetyAnalyzer::getEdgeLockset(FactSet& Result,
|
|
|
|
const FactSet &ExitSet,
|
|
|
|
const CFGBlock *PredBlock,
|
|
|
|
const CFGBlock *CurrBlock) {
|
|
|
|
Result = ExitSet;
|
|
|
|
|
2013-08-16 07:06:33 +08:00
|
|
|
const Stmt *Cond = PredBlock->getTerminatorCondition();
|
|
|
|
if (!Cond)
|
2012-08-11 02:39:05 +08:00
|
|
|
return;
|
2012-06-23 01:07:28 +08:00
|
|
|
|
2012-04-20 00:48:43 +08:00
|
|
|
bool Negate = false;
|
|
|
|
const CFGBlockInfo *PredBlockInfo = &BlockInfo[PredBlock->getBlockID()];
|
|
|
|
const LocalVarContext &LVarCtx = PredBlockInfo->ExitContext;
|
2014-04-02 05:43:23 +08:00
|
|
|
StringRef CapDiagKind = "mutex";
|
2012-04-20 00:48:43 +08:00
|
|
|
|
2012-07-06 05:16:29 +08:00
|
|
|
CallExpr *Exp =
|
|
|
|
const_cast<CallExpr*>(getTrylockCallExpr(Cond, LVarCtx, Negate));
|
2012-04-20 00:48:43 +08:00
|
|
|
if (!Exp)
|
2012-08-11 02:39:05 +08:00
|
|
|
return;
|
2012-04-20 00:48:43 +08:00
|
|
|
|
|
|
|
NamedDecl *FunDecl = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
|
|
|
|
if(!FunDecl || !FunDecl->hasAttrs())
|
2012-08-11 02:39:05 +08:00
|
|
|
return;
|
2012-04-20 00:48:43 +08:00
|
|
|
|
2012-07-06 05:16:29 +08:00
|
|
|
MutexIDList ExclusiveLocksToAdd;
|
|
|
|
MutexIDList SharedLocksToAdd;
|
2012-04-20 00:48:43 +08:00
|
|
|
|
|
|
|
// If the condition is a call to a Trylock function, then grab the attributes
|
2014-05-15 04:42:13 +08:00
|
|
|
for (auto *Attr : FunDecl->getAttrs()) {
|
2012-04-20 00:48:43 +08:00
|
|
|
switch (Attr->getKind()) {
|
|
|
|
case attr::ExclusiveTrylockFunction: {
|
|
|
|
ExclusiveTrylockFunctionAttr *A =
|
|
|
|
cast<ExclusiveTrylockFunctionAttr>(Attr);
|
2012-07-06 05:16:29 +08:00
|
|
|
getMutexIDs(ExclusiveLocksToAdd, A, Exp, FunDecl,
|
|
|
|
PredBlock, CurrBlock, A->getSuccessValue(), Negate);
|
2014-04-02 05:43:23 +08:00
|
|
|
CapDiagKind = ClassifyDiagnostic(A);
|
2012-04-20 00:48:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case attr::SharedTrylockFunction: {
|
|
|
|
SharedTrylockFunctionAttr *A =
|
|
|
|
cast<SharedTrylockFunctionAttr>(Attr);
|
2012-09-21 07:14:43 +08:00
|
|
|
getMutexIDs(SharedLocksToAdd, A, Exp, FunDecl,
|
2012-07-06 05:16:29 +08:00
|
|
|
PredBlock, CurrBlock, A->getSuccessValue(), Negate);
|
2014-04-02 05:43:23 +08:00
|
|
|
CapDiagKind = ClassifyDiagnostic(A);
|
2012-04-20 00:48:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-10-22 02:10:14 +08:00
|
|
|
}
|
2012-07-06 05:16:29 +08:00
|
|
|
|
|
|
|
// Add and remove locks.
|
|
|
|
SourceLocation Loc = Exp->getExprLoc();
|
2014-04-02 05:43:23 +08:00
|
|
|
for (const auto &ExclusiveLockToAdd : ExclusiveLocksToAdd)
|
|
|
|
addLock(Result, ExclusiveLockToAdd, LockData(Loc, LK_Exclusive),
|
|
|
|
CapDiagKind);
|
|
|
|
for (const auto &SharedLockToAdd : SharedLocksToAdd)
|
|
|
|
addLock(Result, SharedLockToAdd, LockData(Loc, LK_Shared), CapDiagKind);
|
2011-10-22 02:10:14 +08:00
|
|
|
}
|
|
|
|
|
2012-04-20 00:48:43 +08:00
|
|
|
/// \brief We use this class to visit different types of expressions in
|
|
|
|
/// CFGBlocks, and build up the lockset.
|
|
|
|
/// An expression may cause us to add or remove locks from the lockset, or else
|
|
|
|
/// output error messages related to missing locks.
|
|
|
|
/// FIXME: In future, we may be able to not inherit from a visitor.
|
|
|
|
class BuildLockset : public StmtVisitor<BuildLockset> {
|
|
|
|
friend class ThreadSafetyAnalyzer;
|
|
|
|
|
|
|
|
ThreadSafetyAnalyzer *Analyzer;
|
2012-08-11 02:39:05 +08:00
|
|
|
FactSet FSet;
|
2012-04-20 00:48:43 +08:00
|
|
|
LocalVariableMap::Context LVarCtx;
|
|
|
|
unsigned CtxIndex;
|
|
|
|
|
|
|
|
// Helper functions
|
|
|
|
|
2012-12-05 08:52:33 +08:00
|
|
|
void warnIfMutexNotHeld(const NamedDecl *D, const Expr *Exp, AccessKind AK,
|
2014-04-02 05:43:23 +08:00
|
|
|
Expr *MutexExp, ProtectedOperationKind POK,
|
|
|
|
StringRef DiagKind);
|
|
|
|
void warnIfMutexHeld(const NamedDecl *D, const Expr *Exp, Expr *MutexExp,
|
|
|
|
StringRef DiagKind);
|
2012-12-05 08:52:33 +08:00
|
|
|
|
|
|
|
void checkAccess(const Expr *Exp, AccessKind AK);
|
|
|
|
void checkPtAccess(const Expr *Exp, AccessKind AK);
|
2012-04-20 00:48:43 +08:00
|
|
|
|
2014-05-20 12:30:07 +08:00
|
|
|
void handleCall(Expr *Exp, const NamedDecl *D, VarDecl *VD = nullptr);
|
2012-04-20 00:48:43 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
BuildLockset(ThreadSafetyAnalyzer *Anlzr, CFGBlockInfo &Info)
|
|
|
|
: StmtVisitor<BuildLockset>(),
|
|
|
|
Analyzer(Anlzr),
|
2012-08-11 02:39:05 +08:00
|
|
|
FSet(Info.EntrySet),
|
2012-04-20 00:48:43 +08:00
|
|
|
LVarCtx(Info.EntryContext),
|
|
|
|
CtxIndex(Info.EntryIndex)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void VisitUnaryOperator(UnaryOperator *UO);
|
|
|
|
void VisitBinaryOperator(BinaryOperator *BO);
|
|
|
|
void VisitCastExpr(CastExpr *CE);
|
|
|
|
void VisitCallExpr(CallExpr *Exp);
|
|
|
|
void VisitCXXConstructExpr(CXXConstructExpr *Exp);
|
|
|
|
void VisitDeclStmt(DeclStmt *S);
|
|
|
|
};
|
|
|
|
|
2011-09-10 00:11:56 +08:00
|
|
|
/// \brief Warn if the LSet does not contain a lock sufficient to protect access
|
2011-10-18 05:33:35 +08:00
|
|
|
/// of at least the passed in AccessKind.
|
2012-12-05 08:52:33 +08:00
|
|
|
void BuildLockset::warnIfMutexNotHeld(const NamedDecl *D, const Expr *Exp,
|
2011-09-10 00:11:56 +08:00
|
|
|
AccessKind AK, Expr *MutexExp,
|
2014-04-02 05:43:23 +08:00
|
|
|
ProtectedOperationKind POK,
|
|
|
|
StringRef DiagKind) {
|
2011-09-10 00:11:56 +08:00
|
|
|
LockKind LK = getLockKindFromAccessKind(AK);
|
2011-10-18 05:33:35 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
til::SExpr *Mutex = Analyzer->SxBuilder.translateAttrExpr(MutexExp, D, Exp);
|
|
|
|
if (!Mutex) {
|
|
|
|
// TODO: invalid locks?
|
|
|
|
// warnInvalidLock(Analyzer->Handler, MutexExp, D, Exp, DiagKind);
|
2012-09-08 01:34:53 +08:00
|
|
|
return;
|
2014-07-28 23:57:27 +08:00
|
|
|
} else if (sx::shouldIgnore(Mutex)) {
|
2012-09-08 01:34:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LockData* LDat = FSet.findLockUniv(Analyzer->FactMan, Mutex);
|
2012-09-11 03:58:23 +08:00
|
|
|
bool NoError = true;
|
|
|
|
if (!LDat) {
|
|
|
|
// No exact match found. Look for a partial match.
|
|
|
|
FactEntry* FEntry = FSet.findPartialMatch(Analyzer->FactMan, Mutex);
|
|
|
|
if (FEntry) {
|
|
|
|
// Warn that there's no precise match.
|
|
|
|
LDat = &FEntry->LDat;
|
2014-07-28 23:57:27 +08:00
|
|
|
std::string PartMatchStr = sx::toString(FEntry->MutID);
|
2012-09-11 03:58:23 +08:00
|
|
|
StringRef PartMatchName(PartMatchStr);
|
2014-07-28 23:57:27 +08:00
|
|
|
Analyzer->Handler.handleMutexNotHeld(DiagKind, D, POK,
|
|
|
|
sx::toString(Mutex),
|
2014-04-02 05:43:23 +08:00
|
|
|
LK, Exp->getExprLoc(),
|
|
|
|
&PartMatchName);
|
2012-09-11 03:58:23 +08:00
|
|
|
} else {
|
|
|
|
// Warn that there's no match at all.
|
2014-07-28 23:57:27 +08:00
|
|
|
Analyzer->Handler.handleMutexNotHeld(DiagKind, D, POK,
|
|
|
|
sx::toString(Mutex),
|
2014-04-02 05:43:23 +08:00
|
|
|
LK, Exp->getExprLoc());
|
2012-09-11 03:58:23 +08:00
|
|
|
}
|
|
|
|
NoError = false;
|
|
|
|
}
|
|
|
|
// Make sure the mutex we found is the right kind.
|
|
|
|
if (NoError && LDat && !LDat->isAtLeast(LK))
|
2014-07-28 23:57:27 +08:00
|
|
|
Analyzer->Handler.handleMutexNotHeld(DiagKind, D, POK,
|
|
|
|
sx::toString(Mutex),
|
|
|
|
LK, Exp->getExprLoc());
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
|
2012-09-08 01:34:53 +08:00
|
|
|
/// \brief Warn if the LSet contains the given lock.
|
2014-04-02 05:43:23 +08:00
|
|
|
void BuildLockset::warnIfMutexHeld(const NamedDecl *D, const Expr *Exp,
|
|
|
|
Expr *MutexExp,
|
|
|
|
StringRef DiagKind) {
|
2014-07-28 23:57:27 +08:00
|
|
|
til::SExpr *Mutex = Analyzer->SxBuilder.translateAttrExpr(MutexExp, D, Exp);
|
|
|
|
if (!Mutex) {
|
|
|
|
// TODO: invalid locks?
|
|
|
|
// warnInvalidLock(Analyzer->Handler, MutexExp, D, Exp, DiagKind);
|
2012-09-08 01:34:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LockData* LDat = FSet.findLock(Analyzer->FactMan, Mutex);
|
2014-04-02 05:43:23 +08:00
|
|
|
if (LDat)
|
|
|
|
Analyzer->Handler.handleFunExcludesLock(
|
2014-07-28 23:57:27 +08:00
|
|
|
DiagKind, D->getNameAsString(), sx::toString(Mutex), Exp->getExprLoc());
|
2012-09-08 01:34:53 +08:00
|
|
|
}
|
|
|
|
|
2012-12-05 08:52:33 +08:00
|
|
|
/// \brief Checks guarded_by and pt_guarded_by attributes.
|
|
|
|
/// Whenever we identify an access (read or write) to a DeclRefExpr that is
|
|
|
|
/// marked with guarded_by, we must ensure the appropriate mutexes are held.
|
|
|
|
/// Similarly, we check if the access is to an expression that dereferences
|
|
|
|
/// a pointer marked with pt_guarded_by.
|
|
|
|
void BuildLockset::checkAccess(const Expr *Exp, AccessKind AK) {
|
|
|
|
Exp = Exp->IgnoreParenCasts();
|
|
|
|
|
|
|
|
if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Exp)) {
|
|
|
|
// For dereferences
|
|
|
|
if (UO->getOpcode() == clang::UO_Deref)
|
|
|
|
checkPtAccess(UO->getSubExpr(), AK);
|
2011-09-10 00:11:56 +08:00
|
|
|
return;
|
2012-12-05 08:52:33 +08:00
|
|
|
}
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2013-11-09 03:42:01 +08:00
|
|
|
if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(Exp)) {
|
2014-03-11 07:03:49 +08:00
|
|
|
checkPtAccess(AE->getLHS(), AK);
|
|
|
|
return;
|
2013-11-09 03:42:01 +08:00
|
|
|
}
|
|
|
|
|
2013-04-02 01:47:37 +08:00
|
|
|
if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) {
|
|
|
|
if (ME->isArrow())
|
|
|
|
checkPtAccess(ME->getBase(), AK);
|
|
|
|
else
|
|
|
|
checkAccess(ME->getBase(), AK);
|
2012-12-08 11:46:30 +08:00
|
|
|
}
|
|
|
|
|
2011-09-10 00:11:56 +08:00
|
|
|
const ValueDecl *D = getValueDecl(Exp);
|
2012-12-05 08:52:33 +08:00
|
|
|
if (!D || !D->hasAttrs())
|
2011-09-10 00:11:56 +08:00
|
|
|
return;
|
|
|
|
|
2013-12-19 10:39:40 +08:00
|
|
|
if (D->hasAttr<GuardedVarAttr>() && FSet.isEmpty())
|
2014-04-02 05:43:23 +08:00
|
|
|
Analyzer->Handler.handleNoMutexHeld("mutex", D, POK_VarAccess, AK,
|
2012-04-20 00:48:43 +08:00
|
|
|
Exp->getExprLoc());
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2014-03-11 01:08:28 +08:00
|
|
|
for (const auto *I : D->specific_attrs<GuardedByAttr>())
|
2014-04-02 05:43:23 +08:00
|
|
|
warnIfMutexNotHeld(D, Exp, AK, I->getArg(), POK_VarAccess,
|
|
|
|
ClassifyDiagnostic(I));
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
|
2012-12-05 08:52:33 +08:00
|
|
|
/// \brief Checks pt_guarded_by and pt_guarded_var attributes.
|
|
|
|
void BuildLockset::checkPtAccess(const Expr *Exp, AccessKind AK) {
|
2014-03-11 07:03:49 +08:00
|
|
|
while (true) {
|
|
|
|
if (const ParenExpr *PE = dyn_cast<ParenExpr>(Exp)) {
|
|
|
|
Exp = PE->getSubExpr();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (const CastExpr *CE = dyn_cast<CastExpr>(Exp)) {
|
|
|
|
if (CE->getCastKind() == CK_ArrayToPointerDecay) {
|
|
|
|
// If it's an actual array, and not a pointer, then it's elements
|
|
|
|
// are protected by GUARDED_BY, not PT_GUARDED_BY;
|
|
|
|
checkAccess(CE->getSubExpr(), AK);
|
|
|
|
return;
|
2013-11-09 03:42:01 +08:00
|
|
|
}
|
2014-03-11 07:03:49 +08:00
|
|
|
Exp = CE->getSubExpr();
|
|
|
|
continue;
|
2013-11-09 03:42:01 +08:00
|
|
|
}
|
2014-03-11 07:03:49 +08:00
|
|
|
break;
|
2013-11-09 03:42:01 +08:00
|
|
|
}
|
2012-12-05 08:52:33 +08:00
|
|
|
|
2011-09-10 00:11:56 +08:00
|
|
|
const ValueDecl *D = getValueDecl(Exp);
|
2012-12-05 08:52:33 +08:00
|
|
|
if (!D || !D->hasAttrs())
|
2011-09-10 00:11:56 +08:00
|
|
|
return;
|
|
|
|
|
2013-12-19 10:39:40 +08:00
|
|
|
if (D->hasAttr<PtGuardedVarAttr>() && FSet.isEmpty())
|
2014-04-02 05:43:23 +08:00
|
|
|
Analyzer->Handler.handleNoMutexHeld("mutex", D, POK_VarDereference, AK,
|
2012-04-20 00:48:43 +08:00
|
|
|
Exp->getExprLoc());
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2014-03-11 01:08:28 +08:00
|
|
|
for (auto const *I : D->specific_attrs<PtGuardedByAttr>())
|
2014-04-02 05:43:23 +08:00
|
|
|
warnIfMutexNotHeld(D, Exp, AK, I->getArg(), POK_VarDereference,
|
|
|
|
ClassifyDiagnostic(I));
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
|
2011-10-22 02:06:53 +08:00
|
|
|
/// \brief Process a function call, method call, constructor call,
|
|
|
|
/// or destructor call. This involves looking at the attributes on the
|
|
|
|
/// corresponding function/method/constructor/destructor, issuing warnings,
|
|
|
|
/// and updating the locksets accordingly.
|
2011-09-10 00:11:56 +08:00
|
|
|
///
|
|
|
|
/// FIXME: For classes annotated with one of the guarded annotations, we need
|
|
|
|
/// to treat const method calls as reads and non-const method calls as writes,
|
|
|
|
/// and check that the appropriate locks are held. Non-const method calls with
|
|
|
|
/// the same signature as const method calls can be also treated as reads.
|
|
|
|
///
|
2012-07-06 05:16:29 +08:00
|
|
|
void BuildLockset::handleCall(Expr *Exp, const NamedDecl *D, VarDecl *VD) {
|
2013-05-18 07:02:59 +08:00
|
|
|
SourceLocation Loc = Exp->getExprLoc();
|
2012-07-06 05:16:29 +08:00
|
|
|
const AttrVec &ArgAttrs = D->getAttrs();
|
2014-03-21 22:48:48 +08:00
|
|
|
MutexIDList ExclusiveLocksToAdd, SharedLocksToAdd;
|
|
|
|
MutexIDList ExclusiveLocksToRemove, SharedLocksToRemove, GenericLocksToRemove;
|
2014-04-02 05:43:23 +08:00
|
|
|
StringRef CapDiagKind = "mutex";
|
2012-07-06 05:16:29 +08:00
|
|
|
|
2011-09-10 00:11:56 +08:00
|
|
|
for(unsigned i = 0; i < ArgAttrs.size(); ++i) {
|
2012-07-06 05:16:29 +08:00
|
|
|
Attr *At = const_cast<Attr*>(ArgAttrs[i]);
|
|
|
|
switch (At->getKind()) {
|
2014-03-21 00:02:49 +08:00
|
|
|
// When we encounter a lock function, we need to add the lock to our
|
|
|
|
// lockset.
|
|
|
|
case attr::AcquireCapability: {
|
|
|
|
auto *A = cast<AcquireCapabilityAttr>(At);
|
|
|
|
Analyzer->getMutexIDs(A->isShared() ? SharedLocksToAdd
|
|
|
|
: ExclusiveLocksToAdd,
|
|
|
|
A, Exp, D, VD);
|
2014-04-02 05:43:23 +08:00
|
|
|
|
|
|
|
CapDiagKind = ClassifyDiagnostic(A);
|
2011-09-10 00:11:56 +08:00
|
|
|
break;
|
2011-10-18 05:33:35 +08:00
|
|
|
}
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2013-05-18 07:02:59 +08:00
|
|
|
// An assert will add a lock to the lockset, but will not generate
|
|
|
|
// a warning if it is already there, and will not generate a warning
|
|
|
|
// if it is not removed.
|
|
|
|
case attr::AssertExclusiveLock: {
|
|
|
|
AssertExclusiveLockAttr *A = cast<AssertExclusiveLockAttr>(At);
|
|
|
|
|
|
|
|
MutexIDList AssertLocks;
|
|
|
|
Analyzer->getMutexIDs(AssertLocks, A, Exp, D, VD);
|
2014-04-02 05:43:23 +08:00
|
|
|
for (const auto &AssertLock : AssertLocks)
|
|
|
|
Analyzer->addLock(FSet, AssertLock,
|
|
|
|
LockData(Loc, LK_Exclusive, false, true),
|
|
|
|
ClassifyDiagnostic(A));
|
2013-05-18 07:02:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case attr::AssertSharedLock: {
|
|
|
|
AssertSharedLockAttr *A = cast<AssertSharedLockAttr>(At);
|
|
|
|
|
|
|
|
MutexIDList AssertLocks;
|
|
|
|
Analyzer->getMutexIDs(AssertLocks, A, Exp, D, VD);
|
2014-04-02 05:43:23 +08:00
|
|
|
for (const auto &AssertLock : AssertLocks)
|
|
|
|
Analyzer->addLock(FSet, AssertLock,
|
|
|
|
LockData(Loc, LK_Shared, false, true),
|
|
|
|
ClassifyDiagnostic(A));
|
2013-05-18 07:02:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-09-10 00:11:56 +08:00
|
|
|
// When we encounter an unlock function, we need to remove unlocked
|
|
|
|
// mutexes from the lockset, and flag a warning if they are not there.
|
2014-03-21 00:02:49 +08:00
|
|
|
case attr::ReleaseCapability: {
|
|
|
|
auto *A = cast<ReleaseCapabilityAttr>(At);
|
2014-03-21 22:48:48 +08:00
|
|
|
if (A->isGeneric())
|
|
|
|
Analyzer->getMutexIDs(GenericLocksToRemove, A, Exp, D, VD);
|
|
|
|
else if (A->isShared())
|
|
|
|
Analyzer->getMutexIDs(SharedLocksToRemove, A, Exp, D, VD);
|
|
|
|
else
|
|
|
|
Analyzer->getMutexIDs(ExclusiveLocksToRemove, A, Exp, D, VD);
|
2014-04-02 05:43:23 +08:00
|
|
|
|
|
|
|
CapDiagKind = ClassifyDiagnostic(A);
|
2011-09-10 00:11:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:36:50 +08:00
|
|
|
case attr::RequiresCapability: {
|
|
|
|
RequiresCapabilityAttr *A = cast<RequiresCapabilityAttr>(At);
|
2014-05-02 21:35:42 +08:00
|
|
|
for (auto *Arg : A->args())
|
|
|
|
warnIfMutexNotHeld(D, Exp, A->isShared() ? AK_Read : AK_Written, Arg,
|
2014-04-02 05:43:23 +08:00
|
|
|
POK_FunctionCall, ClassifyDiagnostic(A));
|
2011-09-10 00:11:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case attr::LocksExcluded: {
|
2012-07-06 05:16:29 +08:00
|
|
|
LocksExcludedAttr *A = cast<LocksExcludedAttr>(At);
|
2014-05-02 21:35:42 +08:00
|
|
|
for (auto *Arg : A->args())
|
|
|
|
warnIfMutexHeld(D, Exp, Arg, ClassifyDiagnostic(A));
|
2011-09-10 00:11:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-12-05 12:47:09 +08:00
|
|
|
// Ignore attributes unrelated to thread-safety
|
2011-09-10 00:11:56 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-07-06 05:16:29 +08:00
|
|
|
|
|
|
|
// Figure out if we're calling the constructor of scoped lockable class
|
|
|
|
bool isScopedVar = false;
|
|
|
|
if (VD) {
|
|
|
|
if (const CXXConstructorDecl *CD = dyn_cast<const CXXConstructorDecl>(D)) {
|
|
|
|
const CXXRecordDecl* PD = CD->getParent();
|
2013-12-19 10:39:40 +08:00
|
|
|
if (PD && PD->hasAttr<ScopedLockableAttr>())
|
2012-07-06 05:16:29 +08:00
|
|
|
isScopedVar = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add locks.
|
2014-03-21 22:48:48 +08:00
|
|
|
for (const auto &M : ExclusiveLocksToAdd)
|
2014-04-02 05:43:23 +08:00
|
|
|
Analyzer->addLock(FSet, M, LockData(Loc, LK_Exclusive, isScopedVar),
|
|
|
|
CapDiagKind);
|
2014-03-21 22:48:48 +08:00
|
|
|
for (const auto &M : SharedLocksToAdd)
|
2014-04-02 05:43:23 +08:00
|
|
|
Analyzer->addLock(FSet, M, LockData(Loc, LK_Shared, isScopedVar),
|
|
|
|
CapDiagKind);
|
2012-07-06 05:16:29 +08:00
|
|
|
|
|
|
|
// Add the managing object as a dummy mutex, mapped to the underlying mutex.
|
|
|
|
// FIXME -- this doesn't work if we acquire multiple locks.
|
|
|
|
if (isScopedVar) {
|
|
|
|
SourceLocation MLoc = VD->getLocation();
|
|
|
|
DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue, VD->getLocation());
|
2014-07-28 23:57:27 +08:00
|
|
|
til::SExpr *SMutex = Analyzer->SxBuilder.translateAttrExpr(&DRE, nullptr);
|
2012-07-06 05:16:29 +08:00
|
|
|
|
2014-03-21 22:48:48 +08:00
|
|
|
for (const auto &M : ExclusiveLocksToAdd)
|
2014-04-02 05:43:23 +08:00
|
|
|
Analyzer->addLock(FSet, SMutex, LockData(MLoc, LK_Exclusive, M),
|
|
|
|
CapDiagKind);
|
2014-03-21 22:48:48 +08:00
|
|
|
for (const auto &M : SharedLocksToAdd)
|
2014-04-02 05:43:23 +08:00
|
|
|
Analyzer->addLock(FSet, SMutex, LockData(MLoc, LK_Shared, M),
|
|
|
|
CapDiagKind);
|
2014-07-28 23:57:27 +08:00
|
|
|
|
|
|
|
// handle corner case where the underlying mutex is invalid
|
|
|
|
if (ExclusiveLocksToAdd.size() == 0 && SharedLocksToAdd.size() == 0) {
|
|
|
|
Analyzer->addLock(FSet, SMutex, LockData(MLoc, LK_Exclusive),
|
|
|
|
CapDiagKind);
|
|
|
|
}
|
2012-07-06 05:16:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove locks.
|
|
|
|
// FIXME -- should only fully remove if the attribute refers to 'this'.
|
|
|
|
bool Dtor = isa<CXXDestructorDecl>(D);
|
2014-03-21 22:48:48 +08:00
|
|
|
for (const auto &M : ExclusiveLocksToRemove)
|
2014-04-02 05:43:23 +08:00
|
|
|
Analyzer->removeLock(FSet, M, Loc, Dtor, LK_Exclusive, CapDiagKind);
|
2014-03-21 22:48:48 +08:00
|
|
|
for (const auto &M : SharedLocksToRemove)
|
2014-04-02 05:43:23 +08:00
|
|
|
Analyzer->removeLock(FSet, M, Loc, Dtor, LK_Shared, CapDiagKind);
|
2014-03-21 22:48:48 +08:00
|
|
|
for (const auto &M : GenericLocksToRemove)
|
2014-04-02 05:43:23 +08:00
|
|
|
Analyzer->removeLock(FSet, M, Loc, Dtor, LK_Generic, CapDiagKind);
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
|
2012-01-07 03:16:50 +08:00
|
|
|
|
2011-10-22 02:06:53 +08:00
|
|
|
/// \brief For unary operations which read and write a variable, we need to
|
|
|
|
/// check whether we hold any required mutexes. Reads are checked in
|
|
|
|
/// VisitCastExpr.
|
|
|
|
void BuildLockset::VisitUnaryOperator(UnaryOperator *UO) {
|
|
|
|
switch (UO->getOpcode()) {
|
|
|
|
case clang::UO_PostDec:
|
|
|
|
case clang::UO_PostInc:
|
|
|
|
case clang::UO_PreDec:
|
|
|
|
case clang::UO_PreInc: {
|
2012-12-05 08:52:33 +08:00
|
|
|
checkAccess(UO->getSubExpr(), AK_Written);
|
2011-10-22 02:06:53 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// For binary operations which assign to a variable (writes), we need to check
|
|
|
|
/// whether we hold any required mutexes.
|
|
|
|
/// FIXME: Deal with non-primitive types.
|
|
|
|
void BuildLockset::VisitBinaryOperator(BinaryOperator *BO) {
|
|
|
|
if (!BO->isAssignmentOp())
|
|
|
|
return;
|
2012-01-07 02:36:09 +08:00
|
|
|
|
|
|
|
// adjust the context
|
2012-04-20 00:48:43 +08:00
|
|
|
LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, BO, LVarCtx);
|
2012-01-07 02:36:09 +08:00
|
|
|
|
2012-12-05 08:52:33 +08:00
|
|
|
checkAccess(BO->getLHS(), AK_Written);
|
2011-10-22 02:06:53 +08:00
|
|
|
}
|
|
|
|
|
2013-11-09 03:42:01 +08:00
|
|
|
|
2011-10-22 02:06:53 +08:00
|
|
|
/// Whenever we do an LValue to Rvalue cast, we are reading a variable and
|
|
|
|
/// need to ensure we hold any required mutexes.
|
|
|
|
/// FIXME: Deal with non-primitive types.
|
|
|
|
void BuildLockset::VisitCastExpr(CastExpr *CE) {
|
|
|
|
if (CE->getCastKind() != CK_LValueToRValue)
|
|
|
|
return;
|
2012-12-05 08:52:33 +08:00
|
|
|
checkAccess(CE->getSubExpr(), AK_Read);
|
2011-10-22 02:06:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-29 08:56:48 +08:00
|
|
|
void BuildLockset::VisitCallExpr(CallExpr *Exp) {
|
2013-04-02 01:47:37 +08:00
|
|
|
if (CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(Exp)) {
|
|
|
|
MemberExpr *ME = dyn_cast<MemberExpr>(CE->getCallee());
|
|
|
|
// ME can be null when calling a method pointer
|
|
|
|
CXXMethodDecl *MD = CE->getMethodDecl();
|
|
|
|
|
|
|
|
if (ME && MD) {
|
|
|
|
if (ME->isArrow()) {
|
|
|
|
if (MD->isConst()) {
|
|
|
|
checkPtAccess(CE->getImplicitObjectArgument(), AK_Read);
|
|
|
|
} else { // FIXME -- should be AK_Written
|
|
|
|
checkPtAccess(CE->getImplicitObjectArgument(), AK_Read);
|
2012-12-05 09:20:45 +08:00
|
|
|
}
|
2013-04-02 01:47:37 +08:00
|
|
|
} else {
|
|
|
|
if (MD->isConst())
|
|
|
|
checkAccess(CE->getImplicitObjectArgument(), AK_Read);
|
|
|
|
else // FIXME -- should be AK_Written
|
|
|
|
checkAccess(CE->getImplicitObjectArgument(), AK_Read);
|
2012-12-05 09:20:45 +08:00
|
|
|
}
|
2013-04-02 01:47:37 +08:00
|
|
|
}
|
|
|
|
} else if (CXXOperatorCallExpr *OE = dyn_cast<CXXOperatorCallExpr>(Exp)) {
|
|
|
|
switch (OE->getOperator()) {
|
|
|
|
case OO_Equal: {
|
|
|
|
const Expr *Target = OE->getArg(0);
|
|
|
|
const Expr *Source = OE->getArg(1);
|
|
|
|
checkAccess(Target, AK_Written);
|
|
|
|
checkAccess(Source, AK_Read);
|
|
|
|
break;
|
|
|
|
}
|
2013-11-06 07:09:56 +08:00
|
|
|
case OO_Star:
|
2013-11-09 03:42:01 +08:00
|
|
|
case OO_Arrow:
|
|
|
|
case OO_Subscript: {
|
2014-03-11 07:03:49 +08:00
|
|
|
const Expr *Obj = OE->getArg(0);
|
|
|
|
checkAccess(Obj, AK_Read);
|
|
|
|
checkPtAccess(Obj, AK_Read);
|
2013-11-06 07:09:56 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-04-02 01:47:37 +08:00
|
|
|
default: {
|
2013-11-07 02:40:01 +08:00
|
|
|
const Expr *Obj = OE->getArg(0);
|
|
|
|
checkAccess(Obj, AK_Read);
|
2013-04-02 01:47:37 +08:00
|
|
|
break;
|
2012-12-05 09:20:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-22 02:06:53 +08:00
|
|
|
NamedDecl *D = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
|
|
|
|
if(!D || !D->hasAttrs())
|
|
|
|
return;
|
|
|
|
handleCall(Exp, D);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BuildLockset::VisitCXXConstructExpr(CXXConstructExpr *Exp) {
|
2013-04-02 01:47:37 +08:00
|
|
|
const CXXConstructorDecl *D = Exp->getConstructor();
|
|
|
|
if (D && D->isCopyConstructor()) {
|
|
|
|
const Expr* Source = Exp->getArg(0);
|
|
|
|
checkAccess(Source, AK_Read);
|
2012-12-05 09:20:45 +08:00
|
|
|
}
|
2011-12-09 04:23:06 +08:00
|
|
|
// FIXME -- only handles constructors in DeclStmt below.
|
|
|
|
}
|
|
|
|
|
|
|
|
void BuildLockset::VisitDeclStmt(DeclStmt *S) {
|
2012-01-07 02:36:09 +08:00
|
|
|
// adjust the context
|
2012-04-20 00:48:43 +08:00
|
|
|
LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, S, LVarCtx);
|
2012-01-07 02:36:09 +08:00
|
|
|
|
2014-05-15 04:42:13 +08:00
|
|
|
for (auto *D : S->getDeclGroup()) {
|
2011-12-09 04:23:06 +08:00
|
|
|
if (VarDecl *VD = dyn_cast_or_null<VarDecl>(D)) {
|
|
|
|
Expr *E = VD->getInit();
|
2012-07-04 02:25:56 +08:00
|
|
|
// handle constructors that involve temporaries
|
|
|
|
if (ExprWithCleanups *EWC = dyn_cast_or_null<ExprWithCleanups>(E))
|
|
|
|
E = EWC->getSubExpr();
|
|
|
|
|
2011-12-09 04:23:06 +08:00
|
|
|
if (CXXConstructExpr *CE = dyn_cast_or_null<CXXConstructExpr>(E)) {
|
|
|
|
NamedDecl *CtorD = dyn_cast_or_null<NamedDecl>(CE->getConstructor());
|
|
|
|
if (!CtorD || !CtorD->hasAttrs())
|
|
|
|
return;
|
|
|
|
handleCall(CE, CtorD, VD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-22 02:06:53 +08:00
|
|
|
}
|
|
|
|
|
2011-10-22 00:14:33 +08:00
|
|
|
|
2012-06-23 01:07:28 +08:00
|
|
|
|
2011-09-16 01:25:19 +08:00
|
|
|
/// \brief Compute the intersection of two locksets and issue warnings for any
|
|
|
|
/// locks in the symmetric difference.
|
|
|
|
///
|
|
|
|
/// This function is used at a merge point in the CFG when comparing the lockset
|
|
|
|
/// of each branch being merged. For example, given the following sequence:
|
|
|
|
/// A; if () then B; else C; D; we need to check that the lockset after B and C
|
|
|
|
/// are the same. In the event of a difference, we use the intersection of these
|
|
|
|
/// two locksets at the start of D.
|
2012-06-23 01:07:28 +08:00
|
|
|
///
|
2012-08-23 07:50:41 +08:00
|
|
|
/// \param FSet1 The first lockset.
|
|
|
|
/// \param FSet2 The second lockset.
|
2012-06-23 01:07:28 +08:00
|
|
|
/// \param JoinLoc The location of the join point for error reporting
|
2012-07-03 06:16:54 +08:00
|
|
|
/// \param LEK1 The error message to report if a mutex is missing from LSet1
|
|
|
|
/// \param LEK2 The error message to report if a mutex is missing from Lset2
|
2012-08-11 02:39:05 +08:00
|
|
|
void ThreadSafetyAnalyzer::intersectAndWarn(FactSet &FSet1,
|
|
|
|
const FactSet &FSet2,
|
|
|
|
SourceLocation JoinLoc,
|
|
|
|
LockErrorKind LEK1,
|
|
|
|
LockErrorKind LEK2,
|
|
|
|
bool Modify) {
|
|
|
|
FactSet FSet1Orig = FSet1;
|
|
|
|
|
2013-05-21 01:57:55 +08:00
|
|
|
// Find locks in FSet2 that conflict or are not in FSet1, and warn.
|
2014-05-15 02:32:59 +08:00
|
|
|
for (const auto &Fact : FSet2) {
|
2014-07-28 23:57:27 +08:00
|
|
|
const til::SExpr *FSet2Mutex = FactMan[Fact].MutID;
|
2014-05-15 02:32:59 +08:00
|
|
|
const LockData &LDat2 = FactMan[Fact].LDat;
|
2013-05-21 01:57:55 +08:00
|
|
|
FactSet::iterator I1 = FSet1.findLockIter(FactMan, FSet2Mutex);
|
2012-08-11 02:39:05 +08:00
|
|
|
|
2013-05-21 01:57:55 +08:00
|
|
|
if (I1 != FSet1.end()) {
|
|
|
|
const LockData* LDat1 = &FactMan[*I1].LDat;
|
2012-07-03 06:26:29 +08:00
|
|
|
if (LDat1->LKind != LDat2.LKind) {
|
2014-07-28 23:57:27 +08:00
|
|
|
Handler.handleExclusiveAndShared("mutex", sx::toString(FSet2Mutex),
|
2014-04-02 05:43:23 +08:00
|
|
|
LDat2.AcquireLoc, LDat1->AcquireLoc);
|
2012-08-11 02:39:05 +08:00
|
|
|
if (Modify && LDat1->LKind != LK_Exclusive) {
|
2013-05-21 01:57:55 +08:00
|
|
|
// Take the exclusive lock, which is the one in FSet2.
|
2014-05-15 02:32:59 +08:00
|
|
|
*I1 = Fact;
|
2012-08-11 02:39:05 +08:00
|
|
|
}
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
2013-05-21 01:57:55 +08:00
|
|
|
else if (LDat1->Asserted && !LDat2.Asserted) {
|
|
|
|
// The non-asserted lock in FSet2 is the one we want to track.
|
2014-05-15 02:32:59 +08:00
|
|
|
*I1 = Fact;
|
2013-05-18 07:02:59 +08:00
|
|
|
}
|
2011-09-10 00:11:56 +08:00
|
|
|
} else {
|
2014-07-28 23:57:27 +08:00
|
|
|
if (LDat2.UnderlyingMutex) {
|
2012-08-11 02:39:05 +08:00
|
|
|
if (FSet2.findLock(FactMan, LDat2.UnderlyingMutex)) {
|
2012-07-03 06:26:29 +08:00
|
|
|
// If this is a scoped lock that manages another mutex, and if the
|
|
|
|
// underlying mutex is still held, then warn about the underlying
|
|
|
|
// mutex.
|
2014-04-02 05:43:23 +08:00
|
|
|
Handler.handleMutexHeldEndOfScope("mutex",
|
2014-07-28 23:57:27 +08:00
|
|
|
sx::toString(LDat2.UnderlyingMutex),
|
2014-04-02 05:43:23 +08:00
|
|
|
LDat2.AcquireLoc, JoinLoc, LEK1);
|
2012-07-03 06:26:29 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-28 23:57:27 +08:00
|
|
|
else if (!LDat2.Managed && !sx::isUniversal(FSet2Mutex) &&
|
|
|
|
!LDat2.Asserted)
|
|
|
|
Handler.handleMutexHeldEndOfScope("mutex", sx::toString(FSet2Mutex),
|
2014-04-02 05:43:23 +08:00
|
|
|
LDat2.AcquireLoc, JoinLoc, LEK1);
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 01:57:55 +08:00
|
|
|
// Find locks in FSet1 that are not in FSet2, and remove them.
|
2014-05-15 02:32:59 +08:00
|
|
|
for (const auto &Fact : FSet1Orig) {
|
2014-07-28 23:57:27 +08:00
|
|
|
const til::SExpr *FSet1Mutex = FactMan[Fact].MutID;
|
2014-05-15 02:32:59 +08:00
|
|
|
const LockData &LDat1 = FactMan[Fact].LDat;
|
2012-07-03 06:26:29 +08:00
|
|
|
|
2012-08-11 02:39:05 +08:00
|
|
|
if (!FSet2.findLock(FactMan, FSet1Mutex)) {
|
2014-07-28 23:57:27 +08:00
|
|
|
if (LDat1.UnderlyingMutex) {
|
2012-08-11 02:39:05 +08:00
|
|
|
if (FSet1Orig.findLock(FactMan, LDat1.UnderlyingMutex)) {
|
2012-07-03 06:26:29 +08:00
|
|
|
// If this is a scoped lock that manages another mutex, and if the
|
|
|
|
// underlying mutex is still held, then warn about the underlying
|
|
|
|
// mutex.
|
2014-04-02 05:43:23 +08:00
|
|
|
Handler.handleMutexHeldEndOfScope("mutex",
|
2014-07-28 23:57:27 +08:00
|
|
|
sx::toString(LDat1.UnderlyingMutex),
|
2014-04-02 05:43:23 +08:00
|
|
|
LDat1.AcquireLoc, JoinLoc, LEK1);
|
2012-07-03 06:26:29 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-28 23:57:27 +08:00
|
|
|
else if (!LDat1.Managed && !sx::isUniversal(FSet1Mutex) &&
|
|
|
|
!LDat1.Asserted)
|
|
|
|
Handler.handleMutexHeldEndOfScope("mutex", sx::toString(FSet1Mutex),
|
2014-04-02 05:43:23 +08:00
|
|
|
LDat1.AcquireLoc, JoinLoc, LEK2);
|
2012-08-11 02:39:05 +08:00
|
|
|
if (Modify)
|
|
|
|
FSet1.removeLock(FactMan, FSet1Mutex);
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-16 01:43:08 +08:00
|
|
|
|
2013-01-19 06:15:45 +08:00
|
|
|
// Return true if block B never continues to its successors.
|
|
|
|
inline bool neverReturns(const CFGBlock* B) {
|
|
|
|
if (B->hasNoReturnElement())
|
|
|
|
return true;
|
|
|
|
if (B->empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CFGElement Last = B->back();
|
2013-02-23 08:29:34 +08:00
|
|
|
if (Optional<CFGStmt> S = Last.getAs<CFGStmt>()) {
|
|
|
|
if (isa<CXXThrowExpr>(S->getStmt()))
|
2013-01-19 06:15:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-06 05:16:29 +08:00
|
|
|
|
2011-09-10 00:11:56 +08:00
|
|
|
/// \brief Check a function's CFG for thread-safety violations.
|
|
|
|
///
|
|
|
|
/// We traverse the blocks in the CFG, compute the set of mutexes that are held
|
|
|
|
/// at the end of each block, and issue warnings for thread safety violations.
|
|
|
|
/// Each block in the CFG is traversed exactly once.
|
2011-10-24 09:32:45 +08:00
|
|
|
void ThreadSafetyAnalyzer::runAnalysis(AnalysisDeclContext &AC) {
|
2014-04-08 02:09:54 +08:00
|
|
|
// TODO: this whole function needs be rewritten as a visitor for CFGWalker.
|
|
|
|
// For now, we just use the walker to set things up.
|
|
|
|
threadSafety::CFGWalker walker;
|
|
|
|
if (!walker.init(AC))
|
|
|
|
return;
|
2011-10-18 05:33:35 +08:00
|
|
|
|
2012-06-23 01:07:28 +08:00
|
|
|
// AC.dumpCFG(true);
|
2014-04-08 02:09:54 +08:00
|
|
|
// threadSafety::printSCFG(walker);
|
|
|
|
|
2014-04-18 05:44:08 +08:00
|
|
|
CFG *CFGraph = walker.getGraph();
|
|
|
|
const NamedDecl *D = walker.getDecl();
|
2012-06-23 01:07:28 +08:00
|
|
|
|
2013-12-19 10:39:40 +08:00
|
|
|
if (D->hasAttr<NoThreadSafetyAnalysisAttr>())
|
2011-10-18 05:33:35 +08:00
|
|
|
return;
|
2014-04-08 02:09:54 +08:00
|
|
|
|
2012-02-17 01:13:43 +08:00
|
|
|
// FIXME: Do something a bit more intelligent inside constructor and
|
|
|
|
// destructor code. Constructors and destructors must assume unique access
|
|
|
|
// to 'this', so checks on member variable access is disabled, but we should
|
|
|
|
// still enable checks on other objects.
|
|
|
|
if (isa<CXXConstructorDecl>(D))
|
|
|
|
return; // Don't check inside constructors.
|
|
|
|
if (isa<CXXDestructorDecl>(D))
|
|
|
|
return; // Don't check inside destructors.
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2012-04-20 00:48:43 +08:00
|
|
|
BlockInfo.resize(CFGraph->getNumBlockIDs(),
|
2012-08-11 02:39:05 +08:00
|
|
|
CFGBlockInfo::getEmptyBlockInfo(LocalVarMap));
|
2011-09-10 00:11:56 +08:00
|
|
|
|
|
|
|
// We need to explore the CFG via a "topological" ordering.
|
|
|
|
// That way, we will be guaranteed to have information about required
|
|
|
|
// predecessor locksets when exploring a new block.
|
2014-04-18 05:44:08 +08:00
|
|
|
const PostOrderCFGView *SortedGraph = walker.getSortedGraph();
|
2011-10-22 10:14:27 +08:00
|
|
|
PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph);
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2012-09-22 01:57:00 +08:00
|
|
|
// Mark entry block as reachable
|
|
|
|
BlockInfo[CFGraph->getEntry().getBlockID()].Reachable = true;
|
|
|
|
|
2012-01-07 02:36:09 +08:00
|
|
|
// Compute SSA names for local variables
|
|
|
|
LocalVarMap.traverseCFG(CFGraph, SortedGraph, BlockInfo);
|
|
|
|
|
2012-02-03 12:45:26 +08:00
|
|
|
// Fill in source locations for all CFGBlocks.
|
|
|
|
findBlockLocations(CFGraph, SortedGraph, BlockInfo);
|
|
|
|
|
2013-04-09 04:11:11 +08:00
|
|
|
MutexIDList ExclusiveLocksAcquired;
|
|
|
|
MutexIDList SharedLocksAcquired;
|
|
|
|
MutexIDList LocksReleased;
|
|
|
|
|
2011-10-22 00:14:33 +08:00
|
|
|
// Add locks from exclusive_locks_required and shared_locks_required
|
2012-02-17 01:13:43 +08:00
|
|
|
// to initial lockset. Also turn off checking for lock and unlock functions.
|
|
|
|
// FIXME: is there a more intelligent way to check lock/unlock functions?
|
2011-10-22 10:14:27 +08:00
|
|
|
if (!SortedGraph->empty() && D->hasAttrs()) {
|
|
|
|
const CFGBlock *FirstBlock = *SortedGraph->begin();
|
2012-08-11 02:39:05 +08:00
|
|
|
FactSet &InitialLockset = BlockInfo[FirstBlock->getBlockID()].EntrySet;
|
2011-09-16 01:43:08 +08:00
|
|
|
const AttrVec &ArgAttrs = D->getAttrs();
|
2012-07-06 05:16:29 +08:00
|
|
|
|
|
|
|
MutexIDList ExclusiveLocksToAdd;
|
|
|
|
MutexIDList SharedLocksToAdd;
|
2014-04-02 05:43:23 +08:00
|
|
|
StringRef CapDiagKind = "mutex";
|
2012-07-06 05:16:29 +08:00
|
|
|
|
|
|
|
SourceLocation Loc = D->getLocation();
|
2014-04-18 21:13:15 +08:00
|
|
|
for (const auto *Attr : ArgAttrs) {
|
2012-07-06 05:16:29 +08:00
|
|
|
Loc = Attr->getLocation();
|
2014-04-18 21:13:15 +08:00
|
|
|
if (const auto *A = dyn_cast<RequiresCapabilityAttr>(Attr)) {
|
2014-02-19 01:36:50 +08:00
|
|
|
getMutexIDs(A->isShared() ? SharedLocksToAdd : ExclusiveLocksToAdd, A,
|
2014-05-20 12:30:07 +08:00
|
|
|
nullptr, D);
|
2014-04-02 05:43:23 +08:00
|
|
|
CapDiagKind = ClassifyDiagnostic(A);
|
2014-04-18 21:13:15 +08:00
|
|
|
} else if (const auto *A = dyn_cast<ReleaseCapabilityAttr>(Attr)) {
|
2013-04-09 04:11:11 +08:00
|
|
|
// UNLOCK_FUNCTION() is used to hide the underlying lock implementation.
|
|
|
|
// We must ignore such methods.
|
|
|
|
if (A->args_size() == 0)
|
|
|
|
return;
|
|
|
|
// FIXME -- deal with exclusive vs. shared unlock functions?
|
2014-04-18 21:13:15 +08:00
|
|
|
getMutexIDs(ExclusiveLocksToAdd, A, nullptr, D);
|
|
|
|
getMutexIDs(LocksReleased, A, nullptr, D);
|
2014-04-02 05:43:23 +08:00
|
|
|
CapDiagKind = ClassifyDiagnostic(A);
|
2014-04-18 21:13:15 +08:00
|
|
|
} else if (const auto *A = dyn_cast<AcquireCapabilityAttr>(Attr)) {
|
2013-04-09 04:11:11 +08:00
|
|
|
if (A->args_size() == 0)
|
|
|
|
return;
|
2014-03-21 00:02:49 +08:00
|
|
|
getMutexIDs(A->isShared() ? SharedLocksAcquired
|
|
|
|
: ExclusiveLocksAcquired,
|
|
|
|
A, nullptr, D);
|
2014-04-02 05:43:23 +08:00
|
|
|
CapDiagKind = ClassifyDiagnostic(A);
|
2012-07-03 05:59:24 +08:00
|
|
|
} else if (isa<ExclusiveTrylockFunctionAttr>(Attr)) {
|
|
|
|
// Don't try to check trylock functions for now
|
|
|
|
return;
|
|
|
|
} else if (isa<SharedTrylockFunctionAttr>(Attr)) {
|
|
|
|
// Don't try to check trylock functions for now
|
|
|
|
return;
|
2011-09-16 01:43:08 +08:00
|
|
|
}
|
|
|
|
}
|
2012-07-06 05:16:29 +08:00
|
|
|
|
|
|
|
// FIXME -- Loc can be wrong here.
|
2014-04-02 05:43:23 +08:00
|
|
|
for (const auto &ExclusiveLockToAdd : ExclusiveLocksToAdd)
|
|
|
|
addLock(InitialLockset, ExclusiveLockToAdd, LockData(Loc, LK_Exclusive),
|
|
|
|
CapDiagKind);
|
|
|
|
for (const auto &SharedLockToAdd : SharedLocksToAdd)
|
|
|
|
addLock(InitialLockset, SharedLockToAdd, LockData(Loc, LK_Shared),
|
|
|
|
CapDiagKind);
|
2011-09-16 01:43:08 +08:00
|
|
|
}
|
|
|
|
|
2014-04-18 05:44:08 +08:00
|
|
|
for (const auto *CurrBlock : *SortedGraph) {
|
2011-09-10 00:11:56 +08:00
|
|
|
int CurrBlockID = CurrBlock->getBlockID();
|
2012-01-07 02:36:09 +08:00
|
|
|
CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID];
|
2011-09-10 00:11:56 +08:00
|
|
|
|
|
|
|
// Use the default initial lockset in case there are no predecessors.
|
2012-01-07 02:36:09 +08:00
|
|
|
VisitedBlocks.insert(CurrBlock);
|
2011-09-10 00:11:56 +08:00
|
|
|
|
|
|
|
// Iterate through the predecessor blocks and warn if the lockset for all
|
|
|
|
// predecessors is not the same. We take the entry lockset of the current
|
|
|
|
// block to be the intersection of all previous locksets.
|
|
|
|
// FIXME: By keeping the intersection, we may output more errors in future
|
|
|
|
// for a lock which is not in the intersection, but was in the union. We
|
|
|
|
// may want to also keep the union in future. As an example, let's say
|
|
|
|
// the intersection contains Mutex L, and the union contains L and M.
|
|
|
|
// Later we unlock M. At this point, we would output an error because we
|
|
|
|
// never locked M; although the real error is probably that we forgot to
|
|
|
|
// lock M on all code paths. Conversely, let's say that later we lock M.
|
|
|
|
// In this case, we should compare against the intersection instead of the
|
|
|
|
// union because the real error is probably that we forgot to unlock M on
|
|
|
|
// all code paths.
|
|
|
|
bool LocksetInitialized = false;
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<CFGBlock *, 8> SpecialBlocks;
|
2011-09-10 00:11:56 +08:00
|
|
|
for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(),
|
|
|
|
PE = CurrBlock->pred_end(); PI != PE; ++PI) {
|
|
|
|
|
|
|
|
// if *PI -> CurrBlock is a back edge
|
2014-04-18 21:13:15 +08:00
|
|
|
if (*PI == nullptr || !VisitedBlocks.alreadySet(*PI))
|
2011-09-10 00:11:56 +08:00
|
|
|
continue;
|
|
|
|
|
2012-09-22 01:57:00 +08:00
|
|
|
int PrevBlockID = (*PI)->getBlockID();
|
|
|
|
CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
|
|
|
|
|
2012-03-03 06:02:58 +08:00
|
|
|
// Ignore edges from blocks that can't return.
|
2013-01-19 06:15:45 +08:00
|
|
|
if (neverReturns(*PI) || !PrevBlockInfo->Reachable)
|
2012-03-03 06:02:58 +08:00
|
|
|
continue;
|
|
|
|
|
2012-09-22 01:57:00 +08:00
|
|
|
// Okay, we can reach this block from the entry.
|
|
|
|
CurrBlockInfo->Reachable = true;
|
|
|
|
|
2012-02-03 11:30:07 +08:00
|
|
|
// If the previous block ended in a 'continue' or 'break' statement, then
|
|
|
|
// a difference in locksets is probably due to a bug in that block, rather
|
|
|
|
// than in some other predecessor. In that case, keep the other
|
|
|
|
// predecessor's lockset.
|
|
|
|
if (const Stmt *Terminator = (*PI)->getTerminator()) {
|
|
|
|
if (isa<ContinueStmt>(Terminator) || isa<BreakStmt>(Terminator)) {
|
|
|
|
SpecialBlocks.push_back(*PI);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-11 02:39:05 +08:00
|
|
|
FactSet PrevLockset;
|
|
|
|
getEdgeLockset(PrevLockset, PrevBlockInfo->ExitSet, *PI, CurrBlock);
|
2012-01-07 02:36:09 +08:00
|
|
|
|
2011-09-10 00:11:56 +08:00
|
|
|
if (!LocksetInitialized) {
|
2012-06-23 01:07:28 +08:00
|
|
|
CurrBlockInfo->EntrySet = PrevLockset;
|
2011-09-10 00:11:56 +08:00
|
|
|
LocksetInitialized = true;
|
|
|
|
} else {
|
2012-08-11 02:39:05 +08:00
|
|
|
intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset,
|
|
|
|
CurrBlockInfo->EntryLoc,
|
|
|
|
LEK_LockedSomePredecessors);
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-22 01:57:00 +08:00
|
|
|
// Skip rest of block if it's not reachable.
|
|
|
|
if (!CurrBlockInfo->Reachable)
|
|
|
|
continue;
|
|
|
|
|
2012-02-03 11:30:07 +08:00
|
|
|
// Process continue and break blocks. Assume that the lockset for the
|
|
|
|
// resulting block is unaffected by any discrepancies in them.
|
2014-04-18 21:13:15 +08:00
|
|
|
for (const auto *PrevBlock : SpecialBlocks) {
|
2012-02-03 11:30:07 +08:00
|
|
|
int PrevBlockID = PrevBlock->getBlockID();
|
|
|
|
CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID];
|
|
|
|
|
|
|
|
if (!LocksetInitialized) {
|
|
|
|
CurrBlockInfo->EntrySet = PrevBlockInfo->ExitSet;
|
|
|
|
LocksetInitialized = true;
|
|
|
|
} else {
|
|
|
|
// Determine whether this edge is a loop terminator for diagnostic
|
|
|
|
// purposes. FIXME: A 'break' statement might be a loop terminator, but
|
|
|
|
// it might also be part of a switch. Also, a subsequent destructor
|
|
|
|
// might add to the lockset, in which case the real issue might be a
|
|
|
|
// double lock on the other path.
|
|
|
|
const Stmt *Terminator = PrevBlock->getTerminator();
|
|
|
|
bool IsLoop = Terminator && isa<ContinueStmt>(Terminator);
|
|
|
|
|
2012-08-11 02:39:05 +08:00
|
|
|
FactSet PrevLockset;
|
|
|
|
getEdgeLockset(PrevLockset, PrevBlockInfo->ExitSet,
|
|
|
|
PrevBlock, CurrBlock);
|
2012-06-23 01:07:28 +08:00
|
|
|
|
2012-02-03 11:30:07 +08:00
|
|
|
// Do not update EntrySet.
|
2012-06-23 01:07:28 +08:00
|
|
|
intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset,
|
|
|
|
PrevBlockInfo->ExitLoc,
|
2012-02-03 11:30:07 +08:00
|
|
|
IsLoop ? LEK_LockedSomeLoopIterations
|
2012-08-11 02:39:05 +08:00
|
|
|
: LEK_LockedSomePredecessors,
|
|
|
|
false);
|
2012-02-03 11:30:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-20 00:48:43 +08:00
|
|
|
BuildLockset LocksetBuilder(this, *CurrBlockInfo);
|
|
|
|
|
2012-01-07 02:36:09 +08:00
|
|
|
// Visit all the statements in the basic block.
|
2011-09-10 00:11:56 +08:00
|
|
|
for (CFGBlock::const_iterator BI = CurrBlock->begin(),
|
|
|
|
BE = CurrBlock->end(); BI != BE; ++BI) {
|
2011-10-22 04:51:27 +08:00
|
|
|
switch (BI->getKind()) {
|
|
|
|
case CFGElement::Statement: {
|
2013-02-22 04:58:29 +08:00
|
|
|
CFGStmt CS = BI->castAs<CFGStmt>();
|
|
|
|
LocksetBuilder.Visit(const_cast<Stmt*>(CS.getStmt()));
|
2011-10-22 04:51:27 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Ignore BaseDtor, MemberDtor, and TemporaryDtor for now.
|
|
|
|
case CFGElement::AutomaticObjectDtor: {
|
2013-02-22 04:58:29 +08:00
|
|
|
CFGAutomaticObjDtor AD = BI->castAs<CFGAutomaticObjDtor>();
|
|
|
|
CXXDestructorDecl *DD = const_cast<CXXDestructorDecl *>(
|
|
|
|
AD.getDestructorDecl(AC.getASTContext()));
|
2011-10-22 04:51:27 +08:00
|
|
|
if (!DD->hasAttrs())
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Create a dummy expression,
|
2013-02-22 04:58:29 +08:00
|
|
|
VarDecl *VD = const_cast<VarDecl*>(AD.getVarDecl());
|
2012-03-10 17:33:50 +08:00
|
|
|
DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue,
|
2013-02-22 04:58:29 +08:00
|
|
|
AD.getTriggerStmt()->getLocEnd());
|
2011-10-22 04:51:27 +08:00
|
|
|
LocksetBuilder.handleCall(&DRE, DD);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
2012-08-11 02:39:05 +08:00
|
|
|
CurrBlockInfo->ExitSet = LocksetBuilder.FSet;
|
2011-09-10 00:11:56 +08:00
|
|
|
|
|
|
|
// For every back edge from CurrBlock (the end of the loop) to another block
|
|
|
|
// (FirstLoopBlock) we need to check that the Lockset of Block is equal to
|
|
|
|
// the one held at the beginning of FirstLoopBlock. We can look up the
|
|
|
|
// Lockset held at the beginning of FirstLoopBlock in the EntryLockSets map.
|
|
|
|
for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
|
|
|
|
SE = CurrBlock->succ_end(); SI != SE; ++SI) {
|
|
|
|
|
|
|
|
// if CurrBlock -> *SI is *not* a back edge
|
2014-05-20 12:30:07 +08:00
|
|
|
if (*SI == nullptr || !VisitedBlocks.alreadySet(*SI))
|
2011-09-10 00:11:56 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
CFGBlock *FirstLoopBlock = *SI;
|
2012-06-23 01:07:28 +08:00
|
|
|
CFGBlockInfo *PreLoop = &BlockInfo[FirstLoopBlock->getBlockID()];
|
|
|
|
CFGBlockInfo *LoopEnd = &BlockInfo[CurrBlockID];
|
|
|
|
intersectAndWarn(LoopEnd->ExitSet, PreLoop->EntrySet,
|
|
|
|
PreLoop->EntryLoc,
|
2012-08-11 02:39:05 +08:00
|
|
|
LEK_LockedSomeLoopIterations,
|
|
|
|
false);
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-22 01:57:00 +08:00
|
|
|
CFGBlockInfo *Initial = &BlockInfo[CFGraph->getEntry().getBlockID()];
|
|
|
|
CFGBlockInfo *Final = &BlockInfo[CFGraph->getExit().getBlockID()];
|
2012-09-20 03:49:40 +08:00
|
|
|
|
|
|
|
// Skip the final check if the exit block is unreachable.
|
2012-09-22 01:57:00 +08:00
|
|
|
if (!Final->Reachable)
|
2012-09-20 03:49:40 +08:00
|
|
|
return;
|
|
|
|
|
2013-04-09 04:11:11 +08:00
|
|
|
// By default, we expect all locks held on entry to be held on exit.
|
|
|
|
FactSet ExpectedExitSet = Initial->EntrySet;
|
|
|
|
|
|
|
|
// Adjust the expected exit set by adding or removing locks, as declared
|
|
|
|
// by *-LOCK_FUNCTION and UNLOCK_FUNCTION. The intersect below will then
|
|
|
|
// issue the appropriate warning.
|
|
|
|
// FIXME: the location here is not quite right.
|
2014-04-18 21:13:15 +08:00
|
|
|
for (const auto &Lock : ExclusiveLocksAcquired)
|
|
|
|
ExpectedExitSet.addLock(FactMan, Lock,
|
2013-04-09 04:11:11 +08:00
|
|
|
LockData(D->getLocation(), LK_Exclusive));
|
2014-04-18 21:13:15 +08:00
|
|
|
for (const auto &Lock : SharedLocksAcquired)
|
|
|
|
ExpectedExitSet.addLock(FactMan, Lock,
|
2013-04-09 04:11:11 +08:00
|
|
|
LockData(D->getLocation(), LK_Shared));
|
2014-04-18 21:13:15 +08:00
|
|
|
for (const auto &Lock : LocksReleased)
|
|
|
|
ExpectedExitSet.removeLock(FactMan, Lock);
|
2013-04-09 04:11:11 +08:00
|
|
|
|
2011-09-16 08:35:54 +08:00
|
|
|
// FIXME: Should we call this function for all blocks which exit the function?
|
2013-04-09 04:11:11 +08:00
|
|
|
intersectAndWarn(ExpectedExitSet, Final->ExitSet,
|
2012-06-23 01:07:28 +08:00
|
|
|
Final->ExitLoc,
|
2012-07-03 06:16:54 +08:00
|
|
|
LEK_LockedAtEndOfFunction,
|
2012-08-11 02:39:05 +08:00
|
|
|
LEK_NotLockedAtEndOfFunction,
|
|
|
|
false);
|
2011-10-22 00:14:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// \brief Check a function's CFG for thread-safety violations.
|
|
|
|
///
|
|
|
|
/// We traverse the blocks in the CFG, compute the set of mutexes that are held
|
|
|
|
/// at the end of each block, and issue warnings for thread safety violations.
|
|
|
|
/// Each block in the CFG is traversed exactly once.
|
2011-10-24 09:32:45 +08:00
|
|
|
void runThreadSafetyAnalysis(AnalysisDeclContext &AC,
|
2011-10-22 00:14:33 +08:00
|
|
|
ThreadSafetyHandler &Handler) {
|
|
|
|
ThreadSafetyAnalyzer Analyzer(Handler);
|
|
|
|
Analyzer.runAnalysis(AC);
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Helper function that returns a LockKind required for the given level
|
|
|
|
/// of access.
|
|
|
|
LockKind getLockKindFromAccessKind(AccessKind AK) {
|
|
|
|
switch (AK) {
|
|
|
|
case AK_Read :
|
|
|
|
return LK_Shared;
|
|
|
|
case AK_Written :
|
|
|
|
return LK_Exclusive;
|
|
|
|
}
|
2011-09-11 05:52:04 +08:00
|
|
|
llvm_unreachable("Unknown AccessKind");
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
2011-10-22 00:14:33 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
}} // end namespace clang::threadSafety
|