2018-03-17 05:22:42 +08:00
|
|
|
//===- ThreadSafety.cpp ---------------------------------------------------===//
|
2011-09-10 00:11:56 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-09-10 00:11:56 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/Analysis/Analyses/ThreadSafety.h"
|
2012-12-01 23:09:41 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2018-03-17 05:22:42 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2011-09-10 00:11:56 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2018-03-17 05:22:42 +08:00
|
|
|
#include "clang/AST/DeclGroup.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
2011-09-10 00:11:56 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2018-03-17 05:22:42 +08:00
|
|
|
#include "clang/AST/OperationKinds.h"
|
|
|
|
#include "clang/AST/Stmt.h"
|
2011-09-10 00:11:56 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2018-03-17 05:22:42 +08:00
|
|
|
#include "clang/AST/Type.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Analysis/Analyses/PostOrderCFGView.h"
|
2015-01-14 19:29:14 +08:00
|
|
|
#include "clang/Analysis/Analyses/ThreadSafetyCommon.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"
|
2018-03-17 05:22:42 +08:00
|
|
|
#include "clang/Analysis/Analyses/ThreadSafetyUtil.h"
|
2017-09-07 05:45:03 +08:00
|
|
|
#include "clang/Analysis/AnalysisDeclContext.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Analysis/CFG.h"
|
2018-10-03 19:58:19 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2018-03-17 05:22:42 +08:00
|
|
|
#include "clang/Basic/LLVM.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"
|
2018-03-17 05:22:42 +08:00
|
|
|
#include "clang/Basic/Specifiers.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2011-09-10 00:11:56 +08:00
|
|
|
#include "llvm/ADT/ImmutableMap.h"
|
2018-03-17 05:22:42 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2018-12-16 22:15:30 +08:00
|
|
|
#include "llvm/ADT/PointerIntPair.h"
|
2018-03-17 05:22:42 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2011-09-10 00:11:56 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2018-03-17 05:22:42 +08:00
|
|
|
#include "llvm/Support/Allocator.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.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>
|
2018-03-17 05:22:42 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <functional>
|
|
|
|
#include <iterator>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <type_traits>
|
2012-01-07 02:36:09 +08:00
|
|
|
#include <utility>
|
2011-09-10 00:11:56 +08:00
|
|
|
#include <vector>
|
2018-03-17 05:22:42 +08:00
|
|
|
|
2015-03-09 22:19:54 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace threadSafety;
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2011-09-15 04:05:09 +08:00
|
|
|
// Key method definition
|
2018-03-17 05:22:42 +08:00
|
|
|
ThreadSafetyHandler::~ThreadSafetyHandler() = default;
|
2011-09-15 04:05:09 +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
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
namespace {
|
|
|
|
|
2018-08-23 05:06:04 +08:00
|
|
|
/// A set of CapabilityExpr objects, which are compiled from thread safety
|
|
|
|
/// attributes on a function.
|
2014-08-05 00:10:59 +08:00
|
|
|
class CapExprSet : public SmallVector<CapabilityExpr, 4> {
|
2012-07-06 05:16:29 +08:00
|
|
|
public:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Push M onto list, but discard duplicates.
|
2014-08-05 00:10:59 +08:00
|
|
|
void push_back_nodup(const CapabilityExpr &CapE) {
|
|
|
|
iterator It = std::find_if(begin(), end(),
|
|
|
|
[=](const CapabilityExpr &CapE2) {
|
|
|
|
return CapE.equals(CapE2);
|
2014-07-28 23:57:27 +08:00
|
|
|
});
|
|
|
|
if (It == end())
|
2014-08-05 00:10:59 +08:00
|
|
|
push_back(CapE);
|
2012-07-06 05:16:29 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-03 14:00:11 +08:00
|
|
|
class FactManager;
|
|
|
|
class FactSet;
|
2014-08-05 00:10:59 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// This is a helper class that stores a fact that is known at a
|
2014-08-05 00:10:59 +08:00
|
|
|
/// particular point in program execution. Currently, a fact is a capability,
|
|
|
|
/// along with additional information, such as where it was acquired, whether
|
|
|
|
/// it is exclusive or shared, etc.
|
2011-09-10 00:11:56 +08:00
|
|
|
///
|
2018-07-26 21:03:16 +08:00
|
|
|
/// FIXME: this analysis does not currently support re-entrant locking.
|
2014-08-05 00:10:59 +08:00
|
|
|
class FactEntry : public CapabilityExpr {
|
|
|
|
private:
|
2018-03-17 05:22:42 +08:00
|
|
|
/// Exclusive or shared.
|
|
|
|
LockKind LKind;
|
|
|
|
|
|
|
|
/// Where it was acquired.
|
|
|
|
SourceLocation AcquireLoc;
|
|
|
|
|
|
|
|
/// True if the lock was asserted.
|
|
|
|
bool Asserted;
|
|
|
|
|
|
|
|
/// True if the lock was declared.
|
|
|
|
bool Declared;
|
2011-12-09 04:23:06 +08:00
|
|
|
|
2014-08-05 00:10:59 +08:00
|
|
|
public:
|
|
|
|
FactEntry(const CapabilityExpr &CE, LockKind LK, SourceLocation Loc,
|
2015-02-04 06:11:04 +08:00
|
|
|
bool Asrt, bool Declrd = false)
|
|
|
|
: CapabilityExpr(CE), LKind(LK), AcquireLoc(Loc), Asserted(Asrt),
|
|
|
|
Declared(Declrd) {}
|
2018-03-17 05:22:42 +08:00
|
|
|
virtual ~FactEntry() = default;
|
2011-09-10 00:11:56 +08:00
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
LockKind kind() const { return LKind; }
|
|
|
|
SourceLocation loc() const { return AcquireLoc; }
|
|
|
|
bool asserted() const { return Asserted; }
|
|
|
|
bool declared() const { return Declared; }
|
2015-02-04 06:11:04 +08:00
|
|
|
|
|
|
|
void setDeclared(bool D) { Declared = D; }
|
2014-09-03 14:00:11 +08:00
|
|
|
|
|
|
|
virtual void
|
|
|
|
handleRemovalFromIntersection(const FactSet &FSet, FactManager &FactMan,
|
|
|
|
SourceLocation JoinLoc, LockErrorKind LEK,
|
|
|
|
ThreadSafetyHandler &Handler) const = 0;
|
2018-08-23 06:14:53 +08:00
|
|
|
virtual void handleLock(FactSet &FSet, FactManager &FactMan,
|
|
|
|
const FactEntry &entry, ThreadSafetyHandler &Handler,
|
|
|
|
StringRef DiagKind) const = 0;
|
2014-09-03 14:00:11 +08:00
|
|
|
virtual void handleUnlock(FactSet &FSet, FactManager &FactMan,
|
|
|
|
const CapabilityExpr &Cp, SourceLocation UnlockLoc,
|
|
|
|
bool FullyRemove, ThreadSafetyHandler &Handler,
|
|
|
|
StringRef DiagKind) const = 0;
|
2012-09-08 01:34:53 +08:00
|
|
|
|
2014-08-05 00:10:59 +08:00
|
|
|
// Return true if LKind >= LK, where exclusive > shared
|
2018-09-22 07:08:30 +08:00
|
|
|
bool isAtLeast(LockKind LK) const {
|
2014-08-05 00:10:59 +08:00
|
|
|
return (LKind == LK_Exclusive) || (LK == LK_Shared);
|
2012-09-08 01:34:53 +08:00
|
|
|
}
|
2011-09-10 00:11:56 +08:00
|
|
|
};
|
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
using FactID = unsigned short;
|
2012-08-11 02:39:05 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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:
|
2018-09-22 07:08:30 +08:00
|
|
|
std::vector<std::unique_ptr<const FactEntry>> Facts;
|
2012-08-11 02:39:05 +08:00
|
|
|
|
|
|
|
public:
|
2014-09-03 14:00:11 +08:00
|
|
|
FactID newFact(std::unique_ptr<FactEntry> Entry) {
|
|
|
|
Facts.push_back(std::move(Entry));
|
2012-08-11 02:39:05 +08:00
|
|
|
return static_cast<unsigned short>(Facts.size() - 1);
|
|
|
|
}
|
|
|
|
|
2014-09-03 14:00:11 +08:00
|
|
|
const FactEntry &operator[](FactID F) const { return *Facts[F]; }
|
2012-08-11 02:39:05 +08:00
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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:
|
2018-03-17 05:22:42 +08:00
|
|
|
using FactVec = SmallVector<FactID, 4>;
|
2012-08-11 02:39:05 +08:00
|
|
|
|
|
|
|
FactVec FactIDs;
|
|
|
|
|
|
|
|
public:
|
2018-03-17 05:22:42 +08:00
|
|
|
using iterator = FactVec::iterator;
|
|
|
|
using const_iterator = FactVec::const_iterator;
|
2012-08-11 02:39:05 +08:00
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
iterator begin() { return FactIDs.begin(); }
|
2012-08-11 02:39:05 +08:00
|
|
|
const_iterator begin() const { return FactIDs.begin(); }
|
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
iterator end() { return FactIDs.end(); }
|
2012-08-11 02:39:05 +08:00
|
|
|
const_iterator end() const { return FactIDs.end(); }
|
|
|
|
|
|
|
|
bool isEmpty() const { return FactIDs.size() == 0; }
|
|
|
|
|
2014-08-05 06:13:06 +08:00
|
|
|
// Return true if the set contains only negative facts
|
|
|
|
bool isEmpty(FactManager &FactMan) const {
|
2018-03-17 05:22:42 +08:00
|
|
|
for (const auto FID : *this) {
|
2014-08-05 06:13:06 +08:00
|
|
|
if (!FactMan[FID].negative())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addLockByID(FactID ID) { FactIDs.push_back(ID); }
|
|
|
|
|
2014-09-03 14:00:11 +08:00
|
|
|
FactID addLock(FactManager &FM, std::unique_ptr<FactEntry> Entry) {
|
|
|
|
FactID F = FM.newFact(std::move(Entry));
|
2012-08-11 02:39:05 +08:00
|
|
|
FactIDs.push_back(F);
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
2014-08-05 00:10:59 +08:00
|
|
|
bool removeLock(FactManager& FM, const CapabilityExpr &CapE) {
|
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-08-05 00:10:59 +08:00
|
|
|
if (FM[FactIDs[i]].matches(CapE)) {
|
2012-08-11 02:39:05 +08:00
|
|
|
FactIDs[i] = FactIDs[n-1];
|
|
|
|
FactIDs.pop_back();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 00:10:59 +08:00
|
|
|
if (FM[FactIDs[n-1]].matches(CapE)) {
|
2012-08-11 02:39:05 +08:00
|
|
|
FactIDs.pop_back();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-05 00:10:59 +08:00
|
|
|
iterator findLockIter(FactManager &FM, const CapabilityExpr &CapE) {
|
2014-05-15 02:32:59 +08:00
|
|
|
return std::find_if(begin(), end(), [&](FactID ID) {
|
2014-08-05 00:10:59 +08:00
|
|
|
return FM[ID].matches(CapE);
|
2014-05-14 23:01:43 +08:00
|
|
|
});
|
2013-05-21 01:57:55 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 07:08:30 +08:00
|
|
|
const FactEntry *findLock(FactManager &FM, const CapabilityExpr &CapE) const {
|
2014-05-15 02:32:59 +08:00
|
|
|
auto I = std::find_if(begin(), end(), [&](FactID ID) {
|
2014-08-05 00:10:59 +08:00
|
|
|
return FM[ID].matches(CapE);
|
2014-05-14 23:01:43 +08:00
|
|
|
});
|
2014-08-05 00:10:59 +08:00
|
|
|
return I != end() ? &FM[*I] : nullptr;
|
2012-08-11 02:39:05 +08:00
|
|
|
}
|
2012-09-11 03:58:23 +08:00
|
|
|
|
2018-09-22 07:08:30 +08:00
|
|
|
const FactEntry *findLockUniv(FactManager &FM,
|
|
|
|
const CapabilityExpr &CapE) const {
|
2014-05-15 02:32:59 +08:00
|
|
|
auto I = std::find_if(begin(), end(), [&](FactID ID) -> bool {
|
2014-08-05 00:10:59 +08:00
|
|
|
return FM[ID].matchesUniv(CapE);
|
2014-05-14 23:01:43 +08:00
|
|
|
});
|
2014-08-05 00:10:59 +08:00
|
|
|
return I != end() ? &FM[*I] : nullptr;
|
2012-09-11 03:58:23 +08:00
|
|
|
}
|
2012-08-11 02:39:05 +08:00
|
|
|
|
2018-09-22 07:08:30 +08:00
|
|
|
const FactEntry *findPartialMatch(FactManager &FM,
|
|
|
|
const CapabilityExpr &CapE) const {
|
2015-02-04 06:11:04 +08:00
|
|
|
auto I = std::find_if(begin(), end(), [&](FactID ID) -> bool {
|
2014-08-05 00:10:59 +08:00
|
|
|
return FM[ID].partiallyMatches(CapE);
|
2014-05-14 23:01:43 +08:00
|
|
|
});
|
|
|
|
return I != end() ? &FM[*I] : nullptr;
|
|
|
|
}
|
2015-02-04 06:11:04 +08:00
|
|
|
|
|
|
|
bool containsMutexDecl(FactManager &FM, const ValueDecl* Vd) const {
|
|
|
|
auto I = std::find_if(begin(), end(), [&](FactID ID) -> bool {
|
|
|
|
return FM[ID].valueDecl() == Vd;
|
|
|
|
});
|
|
|
|
return I != end();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadSafetyAnalyzer;
|
2018-03-17 05:22:42 +08:00
|
|
|
|
2015-03-09 22:19:54 +08:00
|
|
|
} // namespace
|
2015-02-04 06:11:04 +08:00
|
|
|
|
2015-03-09 22:19:54 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace threadSafety {
|
2018-03-17 05:22:42 +08:00
|
|
|
|
2015-02-04 06:11:04 +08:00
|
|
|
class BeforeSet {
|
|
|
|
private:
|
2018-03-17 05:22:42 +08:00
|
|
|
using BeforeVect = SmallVector<const ValueDecl *, 4>;
|
2015-02-04 06:11:04 +08:00
|
|
|
|
|
|
|
struct BeforeInfo {
|
2015-11-21 03:08:30 +08:00
|
|
|
BeforeVect Vect;
|
2018-03-17 05:22:42 +08:00
|
|
|
int Visited = 0;
|
|
|
|
|
|
|
|
BeforeInfo() = default;
|
|
|
|
BeforeInfo(BeforeInfo &&) = default;
|
2015-02-04 06:11:04 +08:00
|
|
|
};
|
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
using BeforeMap =
|
|
|
|
llvm::DenseMap<const ValueDecl *, std::unique_ptr<BeforeInfo>>;
|
|
|
|
using CycleMap = llvm::DenseMap<const ValueDecl *, bool>;
|
2015-02-04 06:11:04 +08:00
|
|
|
|
|
|
|
public:
|
2018-03-17 05:22:42 +08:00
|
|
|
BeforeSet() = default;
|
2015-02-04 06:11:04 +08:00
|
|
|
|
|
|
|
BeforeInfo* insertAttrExprs(const ValueDecl* Vd,
|
|
|
|
ThreadSafetyAnalyzer& Analyzer);
|
|
|
|
|
2015-11-21 03:08:30 +08:00
|
|
|
BeforeInfo *getBeforeInfoForDecl(const ValueDecl *Vd,
|
|
|
|
ThreadSafetyAnalyzer &Analyzer);
|
|
|
|
|
2015-02-04 06:11:04 +08:00
|
|
|
void checkBeforeAfter(const ValueDecl* Vd,
|
|
|
|
const FactSet& FSet,
|
|
|
|
ThreadSafetyAnalyzer& Analyzer,
|
|
|
|
SourceLocation Loc, StringRef CapKind);
|
|
|
|
|
|
|
|
private:
|
|
|
|
BeforeMap BMap;
|
2018-03-17 05:22:42 +08:00
|
|
|
CycleMap CycMap;
|
2015-02-04 02:17:48 +08:00
|
|
|
};
|
2018-03-17 05:22:42 +08:00
|
|
|
|
|
|
|
} // namespace threadSafety
|
|
|
|
} // namespace clang
|
2015-02-04 02:17:48 +08:00
|
|
|
|
2015-03-09 22:19:54 +08:00
|
|
|
namespace {
|
2018-03-17 05:22:42 +08:00
|
|
|
|
2012-01-07 02:36:09 +08:00
|
|
|
class LocalVariableMap;
|
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
using LocalVarContext = llvm::ImmutableMap<const NamedDecl *, unsigned>;
|
|
|
|
|
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 {
|
2018-03-17 05:22:42 +08:00
|
|
|
// Lockset held at entry to block
|
|
|
|
FactSet EntrySet;
|
|
|
|
|
|
|
|
// Lockset held at exit from block
|
|
|
|
FactSet ExitSet;
|
|
|
|
|
|
|
|
// Context held at entry to block
|
|
|
|
LocalVarContext EntryContext;
|
|
|
|
|
|
|
|
// Context held at exit from block
|
|
|
|
LocalVarContext ExitContext;
|
|
|
|
|
|
|
|
// Location of first statement in block
|
|
|
|
SourceLocation EntryLoc;
|
|
|
|
|
|
|
|
// Location of last statement in block.
|
|
|
|
SourceLocation ExitLoc;
|
|
|
|
|
|
|
|
// Used to replay contexts later
|
|
|
|
unsigned EntryIndex;
|
|
|
|
|
|
|
|
// Is this block reachable?
|
|
|
|
bool Reachable = false;
|
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;
|
|
|
|
}
|
2018-03-17 05:22:42 +08:00
|
|
|
|
2012-02-03 12:45:26 +08:00
|
|
|
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)
|
2018-03-17 05:22:42 +08:00
|
|
|
: EntryContext(EmptyCtx), ExitContext(EmptyCtx) {}
|
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:
|
2018-03-17 05:22:42 +08:00
|
|
|
using Context = LocalVarContext;
|
2012-01-07 02:36:09 +08:00
|
|
|
|
|
|
|
/// 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;
|
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
// The original declaration for this variable.
|
|
|
|
const NamedDecl *Dec;
|
|
|
|
|
|
|
|
// The expression for this variable, OR
|
|
|
|
const Expr *Exp = nullptr;
|
|
|
|
|
|
|
|
// Reference to another VarDefinition
|
|
|
|
unsigned Ref = 0;
|
|
|
|
|
|
|
|
// The map with which Exp should be interpreted.
|
|
|
|
Context Ctx;
|
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)
|
2018-03-17 05:22:42 +08:00
|
|
|
: Dec(D), Exp(E), Ctx(C) {}
|
2012-01-07 02:36:09 +08:00
|
|
|
|
|
|
|
// Create reference to previous definition
|
2012-04-20 00:48:43 +08:00
|
|
|
VarDefinition(const NamedDecl *D, unsigned R, Context C)
|
2018-03-17 05:22:42 +08:00
|
|
|
: Dec(D), Ref(R), Ctx(C) {}
|
2012-01-07 02:36:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
Context::Factory ContextFactory;
|
|
|
|
std::vector<VarDefinition> VarDefinitions;
|
|
|
|
std::vector<unsigned> CtxIndices;
|
2018-08-24 05:53:04 +08:00
|
|
|
std::vector<std::pair<const Stmt *, Context>> SavedContexts;
|
2012-01-07 02:36:09 +08:00
|
|
|
|
|
|
|
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.
|
2018-08-24 05:53:04 +08:00
|
|
|
Context getNextContext(unsigned &CtxIndex, const Stmt *S, Context C) {
|
2012-01-07 02:36:09 +08:00
|
|
|
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:
|
2018-03-17 05:22:42 +08:00
|
|
|
friend class VarMapBuilder;
|
|
|
|
|
2012-01-07 02:36:09 +08:00
|
|
|
// Get the current context index
|
|
|
|
unsigned getContextIndex() { return SavedContexts.size()-1; }
|
|
|
|
|
|
|
|
// Save the current context for later replay
|
2018-08-24 05:53:04 +08:00
|
|
|
void saveContext(const Stmt *S, Context C) {
|
2018-03-17 05:22:42 +08:00
|
|
|
SavedContexts.push_back(std::make_pair(S, C));
|
2012-01-07 02:36:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
};
|
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
} // namespace
|
2012-01-07 02:36:09 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
namespace {
|
2012-01-07 02:36:09 +08:00
|
|
|
|
|
|
|
/// Visitor which builds a LocalVariableMap
|
2018-08-24 05:53:04 +08:00
|
|
|
class VarMapBuilder : public ConstStmtVisitor<VarMapBuilder> {
|
2012-01-07 02:36:09 +08:00
|
|
|
public:
|
|
|
|
LocalVariableMap* VMap;
|
|
|
|
LocalVariableMap::Context Ctx;
|
|
|
|
|
|
|
|
VarMapBuilder(LocalVariableMap *VM, LocalVariableMap::Context C)
|
2018-03-17 05:22:42 +08:00
|
|
|
: VMap(VM), Ctx(C) {}
|
2012-01-07 02:36:09 +08:00
|
|
|
|
2018-08-24 05:53:04 +08:00
|
|
|
void VisitDeclStmt(const DeclStmt *S);
|
|
|
|
void VisitBinaryOperator(const BinaryOperator *BO);
|
2012-01-07 02:36:09 +08:00
|
|
|
};
|
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
} // namespace
|
2012-01-07 02:36:09 +08:00
|
|
|
|
|
|
|
// Add new local variables to the variable map
|
2018-08-24 05:53:04 +08:00
|
|
|
void VarMapBuilder::VisitDeclStmt(const DeclStmt *S) {
|
2012-01-07 02:36:09 +08:00
|
|
|
bool modifiedCtx = false;
|
2018-08-24 05:53:04 +08:00
|
|
|
const 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
|
2018-08-24 05:53:04 +08:00
|
|
|
void VarMapBuilder::VisitBinaryOperator(const BinaryOperator *BO) {
|
2012-01-07 02:36:09 +08:00
|
|
|
if (!BO->isAssignmentOp())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Expr *LHSExp = BO->getLHS()->IgnoreParenCasts();
|
|
|
|
|
|
|
|
// Update the variable map and current context.
|
2018-03-17 05:22:42 +08:00
|
|
|
if (const auto *DRE = dyn_cast<DeclRefExpr>(LHSExp)) {
|
|
|
|
const ValueDecl *VDec = DRE->getDecl();
|
2012-01-07 02:36:09 +08:00
|
|
|
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) {
|
2018-09-23 05:56:16 +08:00
|
|
|
unsigned CurrBlockID = CurrBlock->getBlockID();
|
2012-01-07 02:36:09 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-23 05:56:16 +08:00
|
|
|
unsigned PrevBlockID = (*PI)->getBlockID();
|
2012-01-07 02:36:09 +08:00
|
|
|
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);
|
2018-03-17 05:22:42 +08:00
|
|
|
for (const auto &BI : *CurrBlock) {
|
|
|
|
switch (BI.getKind()) {
|
2012-01-07 02:36:09 +08:00
|
|
|
case CFGElement::Statement: {
|
2018-03-17 05:22:42 +08:00
|
|
|
CFGStmt CS = BI.castAs<CFGStmt>();
|
2018-08-24 05:53:04 +08:00
|
|
|
VMapBuilder.Visit(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.
|
2019-05-24 09:34:22 +08:00
|
|
|
if (const Stmt *S = CurrBlock->getTerminatorStmt()) {
|
2018-08-10 05:08:08 +08:00
|
|
|
CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc = S->getBeginLoc();
|
2012-02-03 12:45:26 +08:00
|
|
|
} 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>()) {
|
2018-08-10 05:08:08 +08:00
|
|
|
CurrBlockInfo->ExitLoc = CS->getStmt()->getBeginLoc();
|
2012-02-03 12:45:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-03 13:15:57 +08:00
|
|
|
if (CurrBlockInfo->ExitLoc.isValid()) {
|
2012-02-03 12:45:26 +08:00
|
|
|
// This block contains at least one statement. Find the source location
|
|
|
|
// of the first statement in the block.
|
2018-03-17 05:22:42 +08:00
|
|
|
for (const auto &BI : *CurrBlock) {
|
2012-02-03 12:45:26 +08:00
|
|
|
// FIXME: Handle other CFGElement kinds.
|
2018-03-17 05:22:42 +08:00
|
|
|
if (Optional<CFGStmt> CS = BI.getAs<CFGStmt>()) {
|
2018-08-10 05:08:08 +08:00
|
|
|
CurrBlockInfo->EntryLoc = CS->getStmt()->getBeginLoc();
|
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
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
namespace {
|
|
|
|
|
2014-09-03 14:00:11 +08:00
|
|
|
class LockableFactEntry : public FactEntry {
|
|
|
|
private:
|
2018-03-17 05:22:42 +08:00
|
|
|
/// managed by ScopedLockable object
|
|
|
|
bool Managed;
|
2014-09-03 14:00:11 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
LockableFactEntry(const CapabilityExpr &CE, LockKind LK, SourceLocation Loc,
|
|
|
|
bool Mng = false, bool Asrt = false)
|
|
|
|
: FactEntry(CE, LK, Loc, Asrt), Managed(Mng) {}
|
|
|
|
|
|
|
|
void
|
|
|
|
handleRemovalFromIntersection(const FactSet &FSet, FactManager &FactMan,
|
|
|
|
SourceLocation JoinLoc, LockErrorKind LEK,
|
|
|
|
ThreadSafetyHandler &Handler) const override {
|
|
|
|
if (!Managed && !asserted() && !negative() && !isUniversal()) {
|
|
|
|
Handler.handleMutexHeldEndOfScope("mutex", toString(), loc(), JoinLoc,
|
|
|
|
LEK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 06:14:53 +08:00
|
|
|
void handleLock(FactSet &FSet, FactManager &FactMan, const FactEntry &entry,
|
|
|
|
ThreadSafetyHandler &Handler,
|
|
|
|
StringRef DiagKind) const override {
|
2019-01-30 06:11:42 +08:00
|
|
|
Handler.handleDoubleLock(DiagKind, entry.toString(), loc(), entry.loc());
|
2018-08-23 06:14:53 +08:00
|
|
|
}
|
|
|
|
|
2014-09-03 14:00:11 +08:00
|
|
|
void handleUnlock(FactSet &FSet, FactManager &FactMan,
|
|
|
|
const CapabilityExpr &Cp, SourceLocation UnlockLoc,
|
|
|
|
bool FullyRemove, ThreadSafetyHandler &Handler,
|
|
|
|
StringRef DiagKind) const override {
|
|
|
|
FSet.removeLock(FactMan, Cp);
|
|
|
|
if (!Cp.negative()) {
|
|
|
|
FSet.addLock(FactMan, llvm::make_unique<LockableFactEntry>(
|
|
|
|
!Cp, LK_Exclusive, UnlockLoc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ScopedLockableFactEntry : public FactEntry {
|
|
|
|
private:
|
2018-12-16 22:15:30 +08:00
|
|
|
enum UnderlyingCapabilityKind {
|
|
|
|
UCK_Acquired, ///< Any kind of acquired capability.
|
|
|
|
UCK_ReleasedShared, ///< Shared capability that was released.
|
|
|
|
UCK_ReleasedExclusive, ///< Exclusive capability that was released.
|
|
|
|
};
|
|
|
|
|
|
|
|
using UnderlyingCapability =
|
|
|
|
llvm::PointerIntPair<const til::SExpr *, 2, UnderlyingCapabilityKind>;
|
|
|
|
|
|
|
|
SmallVector<UnderlyingCapability, 4> UnderlyingMutexes;
|
2014-09-03 14:00:11 +08:00
|
|
|
|
|
|
|
public:
|
2018-12-17 00:19:11 +08:00
|
|
|
ScopedLockableFactEntry(const CapabilityExpr &CE, SourceLocation Loc)
|
|
|
|
: FactEntry(CE, LK_Exclusive, Loc, false) {}
|
|
|
|
|
|
|
|
void addExclusiveLock(const CapabilityExpr &M) {
|
|
|
|
UnderlyingMutexes.emplace_back(M.sexpr(), UCK_Acquired);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addSharedLock(const CapabilityExpr &M) {
|
|
|
|
UnderlyingMutexes.emplace_back(M.sexpr(), UCK_Acquired);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addExclusiveUnlock(const CapabilityExpr &M) {
|
|
|
|
UnderlyingMutexes.emplace_back(M.sexpr(), UCK_ReleasedExclusive);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addSharedUnlock(const CapabilityExpr &M) {
|
|
|
|
UnderlyingMutexes.emplace_back(M.sexpr(), UCK_ReleasedShared);
|
2014-09-03 14:00:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
handleRemovalFromIntersection(const FactSet &FSet, FactManager &FactMan,
|
|
|
|
SourceLocation JoinLoc, LockErrorKind LEK,
|
|
|
|
ThreadSafetyHandler &Handler) const override {
|
2018-12-16 22:15:30 +08:00
|
|
|
for (const auto &UnderlyingMutex : UnderlyingMutexes) {
|
|
|
|
const auto *Entry = FSet.findLock(
|
|
|
|
FactMan, CapabilityExpr(UnderlyingMutex.getPointer(), false));
|
|
|
|
if ((UnderlyingMutex.getInt() == UCK_Acquired && Entry) ||
|
|
|
|
(UnderlyingMutex.getInt() != UCK_Acquired && !Entry)) {
|
2014-09-03 14:00:11 +08:00
|
|
|
// If this scoped lock manages another mutex, and if the underlying
|
2018-12-16 22:15:30 +08:00
|
|
|
// mutex is still/not held, then warn about the underlying mutex.
|
2014-09-03 14:00:11 +08:00
|
|
|
Handler.handleMutexHeldEndOfScope(
|
2018-12-16 22:15:30 +08:00
|
|
|
"mutex", sx::toString(UnderlyingMutex.getPointer()), loc(), JoinLoc,
|
|
|
|
LEK);
|
2014-09-03 14:00:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 06:14:53 +08:00
|
|
|
void handleLock(FactSet &FSet, FactManager &FactMan, const FactEntry &entry,
|
|
|
|
ThreadSafetyHandler &Handler,
|
|
|
|
StringRef DiagKind) const override {
|
2018-12-16 22:15:30 +08:00
|
|
|
for (const auto &UnderlyingMutex : UnderlyingMutexes) {
|
|
|
|
CapabilityExpr UnderCp(UnderlyingMutex.getPointer(), false);
|
2018-08-23 06:14:53 +08:00
|
|
|
|
2018-12-16 22:15:30 +08:00
|
|
|
if (UnderlyingMutex.getInt() == UCK_Acquired)
|
|
|
|
lock(FSet, FactMan, UnderCp, entry.kind(), entry.loc(), &Handler,
|
|
|
|
DiagKind);
|
|
|
|
else
|
|
|
|
unlock(FSet, FactMan, UnderCp, entry.loc(), &Handler, DiagKind);
|
2018-08-23 06:14:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-03 14:00:11 +08:00
|
|
|
void handleUnlock(FactSet &FSet, FactManager &FactMan,
|
|
|
|
const CapabilityExpr &Cp, SourceLocation UnlockLoc,
|
|
|
|
bool FullyRemove, ThreadSafetyHandler &Handler,
|
|
|
|
StringRef DiagKind) const override {
|
|
|
|
assert(!Cp.negative() && "Managing object cannot be negative.");
|
2018-12-16 22:15:30 +08:00
|
|
|
for (const auto &UnderlyingMutex : UnderlyingMutexes) {
|
|
|
|
CapabilityExpr UnderCp(UnderlyingMutex.getPointer(), false);
|
|
|
|
|
|
|
|
// Remove/lock the underlying mutex if it exists/is still unlocked; warn
|
|
|
|
// on double unlocking/locking if we're not destroying the scoped object.
|
|
|
|
ThreadSafetyHandler *TSHandler = FullyRemove ? nullptr : &Handler;
|
|
|
|
if (UnderlyingMutex.getInt() == UCK_Acquired) {
|
|
|
|
unlock(FSet, FactMan, UnderCp, UnlockLoc, TSHandler, DiagKind);
|
2014-09-03 14:00:11 +08:00
|
|
|
} else {
|
2018-12-16 22:15:30 +08:00
|
|
|
LockKind kind = UnderlyingMutex.getInt() == UCK_ReleasedShared
|
|
|
|
? LK_Shared
|
|
|
|
: LK_Exclusive;
|
|
|
|
lock(FSet, FactMan, UnderCp, kind, UnlockLoc, TSHandler, DiagKind);
|
2014-09-03 14:00:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (FullyRemove)
|
|
|
|
FSet.removeLock(FactMan, Cp);
|
|
|
|
}
|
2018-12-16 22:15:30 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void lock(FactSet &FSet, FactManager &FactMan, const CapabilityExpr &Cp,
|
|
|
|
LockKind kind, SourceLocation loc, ThreadSafetyHandler *Handler,
|
|
|
|
StringRef DiagKind) const {
|
2019-01-30 06:11:42 +08:00
|
|
|
if (const FactEntry *Fact = FSet.findLock(FactMan, Cp)) {
|
|
|
|
if (Handler)
|
|
|
|
Handler->handleDoubleLock(DiagKind, Cp.toString(), Fact->loc(), loc);
|
|
|
|
} else {
|
2018-12-16 22:15:30 +08:00
|
|
|
FSet.removeLock(FactMan, !Cp);
|
|
|
|
FSet.addLock(FactMan,
|
|
|
|
llvm::make_unique<LockableFactEntry>(Cp, kind, loc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock(FactSet &FSet, FactManager &FactMan, const CapabilityExpr &Cp,
|
|
|
|
SourceLocation loc, ThreadSafetyHandler *Handler,
|
|
|
|
StringRef DiagKind) const {
|
|
|
|
if (FSet.findLock(FactMan, Cp)) {
|
|
|
|
FSet.removeLock(FactMan, Cp);
|
|
|
|
FSet.addLock(FactMan, llvm::make_unique<LockableFactEntry>(
|
|
|
|
!Cp, LK_Exclusive, loc));
|
|
|
|
} else if (Handler) {
|
|
|
|
Handler->handleUnmatchedUnlock(DiagKind, Cp.toString(), loc);
|
|
|
|
}
|
|
|
|
}
|
2014-09-03 14:00:11 +08:00
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Class which implements the core thread safety analysis routines.
|
2012-01-07 02:36:09 +08:00
|
|
|
class ThreadSafetyAnalyzer {
|
|
|
|
friend class BuildLockset;
|
2015-03-09 22:19:54 +08:00
|
|
|
friend class threadSafety::BeforeSet;
|
2012-01-07 02:36:09 +08:00
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
llvm::BumpPtrAllocator Bpa;
|
|
|
|
threadSafety::til::MemRegionRef Arena;
|
|
|
|
threadSafety::SExprBuilder SxBuilder;
|
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
ThreadSafetyHandler &Handler;
|
|
|
|
const CXXMethodDecl *CurrentMethod;
|
|
|
|
LocalVariableMap LocalVarMap;
|
|
|
|
FactManager FactMan;
|
2012-04-20 00:48:43 +08:00
|
|
|
std::vector<CFGBlockInfo> BlockInfo;
|
2012-01-07 02:36:09 +08:00
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
BeforeSet *GlobalBeforeSet;
|
2015-02-04 06:11:04 +08:00
|
|
|
|
2012-01-07 02:36:09 +08:00
|
|
|
public:
|
2015-02-04 06:11:04 +08:00
|
|
|
ThreadSafetyAnalyzer(ThreadSafetyHandler &H, BeforeSet* Bset)
|
2018-03-17 05:22:42 +08:00
|
|
|
: Arena(&Bpa), SxBuilder(Arena), Handler(H), GlobalBeforeSet(Bset) {}
|
2012-01-07 02:36:09 +08:00
|
|
|
|
2014-08-05 06:13:06 +08:00
|
|
|
bool inCurrentScope(const CapabilityExpr &CapE);
|
|
|
|
|
2014-09-03 14:00:11 +08:00
|
|
|
void addLock(FactSet &FSet, std::unique_ptr<FactEntry> Entry,
|
|
|
|
StringRef DiagKind, bool ReqAttr = false);
|
2014-08-05 00:10:59 +08:00
|
|
|
void removeLock(FactSet &FSet, const CapabilityExpr &CapE,
|
2014-07-28 23:57:27 +08:00
|
|
|
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>
|
2018-08-24 05:13:32 +08:00
|
|
|
void getMutexIDs(CapExprSet &Mtxs, AttrType *Attr, const 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>
|
2018-08-24 05:13:32 +08:00
|
|
|
void getMutexIDs(CapExprSet &Mtxs, AttrType *Attr, const Expr *Exp,
|
2012-07-06 05:16:29 +08:00
|
|
|
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
|
|
|
};
|
2018-03-17 05:22:42 +08:00
|
|
|
|
2015-03-09 22:19:54 +08:00
|
|
|
} // namespace
|
2015-02-04 06:11:04 +08:00
|
|
|
|
|
|
|
/// Process acquired_before and acquired_after attributes on Vd.
|
|
|
|
BeforeSet::BeforeInfo* BeforeSet::insertAttrExprs(const ValueDecl* Vd,
|
|
|
|
ThreadSafetyAnalyzer& Analyzer) {
|
|
|
|
// Create a new entry for Vd.
|
2015-11-21 03:08:30 +08:00
|
|
|
BeforeInfo *Info = nullptr;
|
|
|
|
{
|
|
|
|
// Keep InfoPtr in its own scope in case BMap is modified later and the
|
|
|
|
// reference becomes invalid.
|
|
|
|
std::unique_ptr<BeforeInfo> &InfoPtr = BMap[Vd];
|
|
|
|
if (!InfoPtr)
|
|
|
|
InfoPtr.reset(new BeforeInfo());
|
|
|
|
Info = InfoPtr.get();
|
|
|
|
}
|
2015-02-04 06:11:04 +08:00
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
for (const auto *At : Vd->attrs()) {
|
2015-02-04 06:11:04 +08:00
|
|
|
switch (At->getKind()) {
|
|
|
|
case attr::AcquiredBefore: {
|
2018-03-17 05:22:42 +08:00
|
|
|
const auto *A = cast<AcquiredBeforeAttr>(At);
|
2015-02-04 06:11:04 +08:00
|
|
|
|
|
|
|
// Read exprs from the attribute, and add them to BeforeVect.
|
|
|
|
for (const auto *Arg : A->args()) {
|
|
|
|
CapabilityExpr Cp =
|
|
|
|
Analyzer.SxBuilder.translateAttrExpr(Arg, nullptr);
|
|
|
|
if (const ValueDecl *Cpvd = Cp.valueDecl()) {
|
2015-11-21 03:08:30 +08:00
|
|
|
Info->Vect.push_back(Cpvd);
|
2018-03-17 05:22:42 +08:00
|
|
|
const auto It = BMap.find(Cpvd);
|
2015-02-04 06:11:04 +08:00
|
|
|
if (It == BMap.end())
|
|
|
|
insertAttrExprs(Cpvd, Analyzer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case attr::AcquiredAfter: {
|
2018-03-17 05:22:42 +08:00
|
|
|
const auto *A = cast<AcquiredAfterAttr>(At);
|
2015-02-04 06:11:04 +08:00
|
|
|
|
|
|
|
// Read exprs from the attribute, and add them to BeforeVect.
|
|
|
|
for (const auto *Arg : A->args()) {
|
|
|
|
CapabilityExpr Cp =
|
|
|
|
Analyzer.SxBuilder.translateAttrExpr(Arg, nullptr);
|
|
|
|
if (const ValueDecl *ArgVd = Cp.valueDecl()) {
|
|
|
|
// Get entry for mutex listed in attribute
|
2015-11-21 03:08:30 +08:00
|
|
|
BeforeInfo *ArgInfo = getBeforeInfoForDecl(ArgVd, Analyzer);
|
|
|
|
ArgInfo->Vect.push_back(Vd);
|
2015-02-04 06:11:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Info;
|
|
|
|
}
|
|
|
|
|
2015-11-21 03:08:30 +08:00
|
|
|
BeforeSet::BeforeInfo *
|
|
|
|
BeforeSet::getBeforeInfoForDecl(const ValueDecl *Vd,
|
|
|
|
ThreadSafetyAnalyzer &Analyzer) {
|
|
|
|
auto It = BMap.find(Vd);
|
|
|
|
BeforeInfo *Info = nullptr;
|
|
|
|
if (It == BMap.end())
|
|
|
|
Info = insertAttrExprs(Vd, Analyzer);
|
|
|
|
else
|
|
|
|
Info = It->second.get();
|
|
|
|
assert(Info && "BMap contained nullptr?");
|
|
|
|
return Info;
|
|
|
|
}
|
2015-02-04 06:11:04 +08:00
|
|
|
|
|
|
|
/// Return true if any mutexes in FSet are in the acquired_before set of Vd.
|
|
|
|
void BeforeSet::checkBeforeAfter(const ValueDecl* StartVd,
|
|
|
|
const FactSet& FSet,
|
|
|
|
ThreadSafetyAnalyzer& Analyzer,
|
|
|
|
SourceLocation Loc, StringRef CapKind) {
|
|
|
|
SmallVector<BeforeInfo*, 8> InfoVect;
|
|
|
|
|
|
|
|
// Do a depth-first traversal of Vd.
|
|
|
|
// Return true if there are cycles.
|
|
|
|
std::function<bool (const ValueDecl*)> traverse = [&](const ValueDecl* Vd) {
|
|
|
|
if (!Vd)
|
|
|
|
return false;
|
|
|
|
|
2015-11-21 03:08:30 +08:00
|
|
|
BeforeSet::BeforeInfo *Info = getBeforeInfoForDecl(Vd, Analyzer);
|
2015-02-04 06:11:04 +08:00
|
|
|
|
|
|
|
if (Info->Visited == 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Info->Visited == 2)
|
|
|
|
return false;
|
|
|
|
|
2015-11-21 03:08:30 +08:00
|
|
|
if (Info->Vect.empty())
|
2015-02-04 06:11:04 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
InfoVect.push_back(Info);
|
|
|
|
Info->Visited = 1;
|
2018-03-17 05:22:42 +08:00
|
|
|
for (const auto *Vdb : Info->Vect) {
|
2015-02-04 06:11:04 +08:00
|
|
|
// Exclude mutexes in our immediate before set.
|
|
|
|
if (FSet.containsMutexDecl(Analyzer.FactMan, Vdb)) {
|
|
|
|
StringRef L1 = StartVd->getName();
|
|
|
|
StringRef L2 = Vdb->getName();
|
|
|
|
Analyzer.Handler.handleLockAcquiredBefore(CapKind, L1, L2, Loc);
|
|
|
|
}
|
|
|
|
// Transitively search other before sets, and warn on cycles.
|
|
|
|
if (traverse(Vdb)) {
|
|
|
|
if (CycMap.find(Vd) == CycMap.end()) {
|
|
|
|
CycMap.insert(std::make_pair(Vd, true));
|
|
|
|
StringRef L1 = Vd->getName();
|
|
|
|
Analyzer.Handler.handleBeforeAfterCycle(L1, Vd->getLocation());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Info->Visited = 2;
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
traverse(StartVd);
|
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
for (auto *Info : InfoVect)
|
2015-02-04 06:11:04 +08:00
|
|
|
Info->Visited = 0;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Gets the value decl pointer from DeclRefExprs or MemberExprs.
|
2014-04-02 05:43:23 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-03-09 22:19:54 +08:00
|
|
|
namespace {
|
2018-03-17 05:22:42 +08:00
|
|
|
|
2014-04-02 05:43:23 +08:00
|
|
|
template <typename Ty>
|
2014-05-02 21:35:42 +08:00
|
|
|
class has_arg_iterator_range {
|
2018-03-17 05:22:42 +08:00
|
|
|
using yes = char[1];
|
|
|
|
using no = char[2];
|
2014-04-02 05:43:23 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
2018-03-17 05:22:42 +08:00
|
|
|
|
2015-03-09 22:19:54 +08:00
|
|
|
} // namespace
|
2014-04-02 05:43:23 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
bool ThreadSafetyAnalyzer::inCurrentScope(const CapabilityExpr &CapE) {
|
2014-08-05 06:13:06 +08:00
|
|
|
if (!CurrentMethod)
|
|
|
|
return false;
|
2018-03-17 05:22:42 +08:00
|
|
|
if (const auto *P = dyn_cast_or_null<til::Project>(CapE.sexpr())) {
|
|
|
|
const auto *VD = P->clangDecl();
|
2014-08-05 06:13:06 +08:00
|
|
|
if (VD)
|
|
|
|
return VD->getDeclContext() == CurrentMethod->getDeclContext();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Add a new lock to the lockset, warning if the lock is already there.
|
2014-08-05 06:13:06 +08:00
|
|
|
/// \param ReqAttr -- true if this is part of an initial Requires attribute.
|
2014-09-03 14:00:11 +08:00
|
|
|
void ThreadSafetyAnalyzer::addLock(FactSet &FSet,
|
|
|
|
std::unique_ptr<FactEntry> Entry,
|
2014-08-05 06:13:06 +08:00
|
|
|
StringRef DiagKind, bool ReqAttr) {
|
2014-09-03 14:00:11 +08:00
|
|
|
if (Entry->shouldIgnore())
|
2012-09-01 05:57:32 +08:00
|
|
|
return;
|
|
|
|
|
2014-09-03 14:00:11 +08:00
|
|
|
if (!ReqAttr && !Entry->negative()) {
|
2014-08-05 06:13:06 +08:00
|
|
|
// look for the negative capability, and remove it from the fact set.
|
2014-09-03 14:00:11 +08:00
|
|
|
CapabilityExpr NegC = !*Entry;
|
2018-09-22 07:08:30 +08:00
|
|
|
const FactEntry *Nen = FSet.findLock(FactMan, NegC);
|
2014-08-05 06:13:06 +08:00
|
|
|
if (Nen) {
|
|
|
|
FSet.removeLock(FactMan, NegC);
|
|
|
|
}
|
|
|
|
else {
|
2014-09-03 14:00:11 +08:00
|
|
|
if (inCurrentScope(*Entry) && !Entry->asserted())
|
|
|
|
Handler.handleNegativeNotHeld(DiagKind, Entry->toString(),
|
|
|
|
NegC.toString(), Entry->loc());
|
2014-08-05 06:13:06 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-05 00:10:59 +08:00
|
|
|
|
2015-02-04 06:11:04 +08:00
|
|
|
// Check before/after constraints
|
|
|
|
if (Handler.issueBetaWarnings() &&
|
|
|
|
!Entry->asserted() && !Entry->declared()) {
|
|
|
|
GlobalBeforeSet->checkBeforeAfter(Entry->valueDecl(), FSet, *this,
|
|
|
|
Entry->loc(), DiagKind);
|
|
|
|
}
|
|
|
|
|
2014-08-05 00:10:59 +08:00
|
|
|
// FIXME: Don't always warn when we have support for reentrant locks.
|
2018-09-22 07:08:30 +08:00
|
|
|
if (const FactEntry *Cp = FSet.findLock(FactMan, *Entry)) {
|
2014-09-03 14:00:11 +08:00
|
|
|
if (!Entry->asserted())
|
2018-08-23 06:14:53 +08:00
|
|
|
Cp->handleLock(FSet, FactMan, *Entry, Handler, DiagKind);
|
2012-04-20 00:48:43 +08:00
|
|
|
} else {
|
2014-09-03 14:00:11 +08:00
|
|
|
FSet.addLock(FactMan, std::move(Entry));
|
2012-04-20 00:48:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Remove a lock from the lockset, warning if the lock is not there.
|
2011-09-10 00:11:56 +08:00
|
|
|
/// \param UnlockLoc The source location of the unlock (only used in error msg)
|
2014-08-05 00:10:59 +08:00
|
|
|
void ThreadSafetyAnalyzer::removeLock(FactSet &FSet, const CapabilityExpr &Cp,
|
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-08-05 00:10:59 +08:00
|
|
|
if (Cp.shouldIgnore())
|
2012-09-01 05:57:32 +08:00
|
|
|
return;
|
|
|
|
|
2014-08-05 00:10:59 +08:00
|
|
|
const FactEntry *LDat = FSet.findLock(FactMan, Cp);
|
2012-04-20 00:48:43 +08:00
|
|
|
if (!LDat) {
|
2014-08-05 00:10:59 +08:00
|
|
|
Handler.handleUnmatchedUnlock(DiagKind, Cp.toString(), 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.
|
2014-08-05 00:10:59 +08:00
|
|
|
if (ReceivedKind != LK_Generic && LDat->kind() != ReceivedKind) {
|
2019-03-19 07:26:54 +08:00
|
|
|
Handler.handleIncorrectUnlockKind(DiagKind, Cp.toString(), LDat->kind(),
|
|
|
|
ReceivedKind, LDat->loc(), UnlockLoc);
|
2014-03-21 22:48:48 +08:00
|
|
|
}
|
|
|
|
|
2014-09-03 14:00:11 +08:00
|
|
|
LDat->handleUnlock(FSet, FactMan, Cp, UnlockLoc, FullyRemove, Handler,
|
|
|
|
DiagKind);
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Extract the list of mutexIDs from the attribute on an expression,
|
2012-07-06 05:16:29 +08:00
|
|
|
/// and push them onto Mtxs, discarding any duplicates.
|
2011-10-22 00:14:33 +08:00
|
|
|
template <typename AttrType>
|
2014-08-05 00:10:59 +08:00
|
|
|
void ThreadSafetyAnalyzer::getMutexIDs(CapExprSet &Mtxs, AttrType *Attr,
|
2018-08-24 05:13:32 +08:00
|
|
|
const Expr *Exp, const NamedDecl *D,
|
2012-10-06 06:38:19 +08:00
|
|
|
VarDecl *SelfDecl) {
|
2011-10-22 00:14:33 +08:00
|
|
|
if (Attr->args_size() == 0) {
|
|
|
|
// The mutex held is the "this" object.
|
2014-08-05 00:10:59 +08:00
|
|
|
CapabilityExpr Cp = SxBuilder.translateAttrExpr(nullptr, D, Exp, SelfDecl);
|
|
|
|
if (Cp.isInvalid()) {
|
2014-07-28 23:57:27 +08:00
|
|
|
warnInvalidLock(Handler, nullptr, D, Exp, ClassifyDiagnostic(Attr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//else
|
2014-08-05 00:10:59 +08:00
|
|
|
if (!Cp.shouldIgnore())
|
|
|
|
Mtxs.push_back_nodup(Cp);
|
2012-07-06 05:16:29 +08:00
|
|
|
return;
|
2011-10-22 00:14:33 +08:00
|
|
|
}
|
|
|
|
|
2014-05-02 21:35:42 +08:00
|
|
|
for (const auto *Arg : Attr->args()) {
|
2014-08-05 00:10:59 +08:00
|
|
|
CapabilityExpr Cp = SxBuilder.translateAttrExpr(Arg, D, Exp, SelfDecl);
|
|
|
|
if (Cp.isInvalid()) {
|
2014-07-28 23:57:27 +08:00
|
|
|
warnInvalidLock(Handler, nullptr, D, Exp, ClassifyDiagnostic(Attr));
|
2014-08-05 00:10:59 +08:00
|
|
|
continue;
|
2014-07-28 23:57:27 +08:00
|
|
|
}
|
|
|
|
//else
|
2014-08-05 00:10:59 +08:00
|
|
|
if (!Cp.shouldIgnore())
|
|
|
|
Mtxs.push_back_nodup(Cp);
|
2012-04-20 00:48:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Extract the list of mutexIDs from a trylock attribute. If the
|
2012-07-06 05:16:29 +08:00
|
|
|
/// 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>
|
2014-08-05 00:10:59 +08:00
|
|
|
void ThreadSafetyAnalyzer::getMutexIDs(CapExprSet &Mtxs, AttrType *Attr,
|
2018-08-24 05:13:32 +08:00
|
|
|
const Expr *Exp, const NamedDecl *D,
|
2012-07-06 05:16:29 +08:00
|
|
|
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;
|
2018-03-17 05:22:42 +08:00
|
|
|
if (const auto *BLE = dyn_cast_or_null<CXXBoolLiteralExpr>(BrE))
|
2012-04-20 00:48:43 +08:00
|
|
|
branch = BLE->getValue();
|
2018-03-17 05:22:42 +08:00
|
|
|
else if (const auto *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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 22:19:54 +08:00
|
|
|
static bool getStaticBooleanValue(Expr *E, bool &TCond) {
|
2012-07-11 05:47:55 +08:00
|
|
|
if (isa<CXXNullPtrLiteralExpr>(E) || isa<GNUNullExpr>(E)) {
|
|
|
|
TCond = false;
|
|
|
|
return true;
|
2018-03-17 05:22:42 +08:00
|
|
|
} else if (const auto *BLE = dyn_cast<CXXBoolLiteralExpr>(E)) {
|
2012-07-11 05:47:55 +08:00
|
|
|
TCond = BLE->getValue();
|
|
|
|
return true;
|
2018-03-17 05:22:42 +08:00
|
|
|
} else if (const auto *ILE = dyn_cast<IntegerLiteral>(E)) {
|
2012-07-11 05:47:55 +08:00
|
|
|
TCond = ILE->getValue().getBoolValue();
|
|
|
|
return true;
|
2018-03-17 05:22:42 +08:00
|
|
|
} else if (auto *CE = dyn_cast<ImplicitCastExpr>(E))
|
2012-07-11 05:47:55 +08:00
|
|
|
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
|
|
|
|
2018-10-03 19:58:19 +08:00
|
|
|
if (const auto *CallExp = dyn_cast<CallExpr>(Cond)) {
|
|
|
|
if (CallExp->getBuiltinCallee() == Builtin::BI__builtin_expect)
|
|
|
|
return getTrylockCallExpr(CallExp->getArg(0), C, Negate);
|
2012-04-20 00:48:43 +08:00
|
|
|
return CallExp;
|
2018-10-03 19:58:19 +08:00
|
|
|
}
|
2018-03-17 05:22:42 +08:00
|
|
|
else if (const auto *PE = dyn_cast<ParenExpr>(Cond))
|
2012-07-11 05:47:55 +08:00
|
|
|
return getTrylockCallExpr(PE->getSubExpr(), C, Negate);
|
2018-03-17 05:22:42 +08:00
|
|
|
else if (const auto *CE = dyn_cast<ImplicitCastExpr>(Cond))
|
2012-04-20 00:48:43 +08:00
|
|
|
return getTrylockCallExpr(CE->getSubExpr(), C, Negate);
|
2018-10-31 11:48:47 +08:00
|
|
|
else if (const auto *FE = dyn_cast<FullExpr>(Cond))
|
|
|
|
return getTrylockCallExpr(FE->getSubExpr(), C, Negate);
|
2018-03-17 05:22:42 +08:00
|
|
|
else if (const auto *DRE = dyn_cast<DeclRefExpr>(Cond)) {
|
2012-04-20 00:48:43 +08:00
|
|
|
const Expr *E = LocalVarMap.lookupExpr(DRE->getDecl(), C);
|
|
|
|
return getTrylockCallExpr(E, C, Negate);
|
|
|
|
}
|
2018-03-17 05:22:42 +08:00
|
|
|
else if (const auto *UOP = dyn_cast<UnaryOperator>(Cond)) {
|
2012-04-20 00:48:43 +08:00
|
|
|
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
|
|
|
}
|
2018-03-17 05:22:42 +08:00
|
|
|
else if (const auto *BOP = dyn_cast<BinaryOperator>(Cond)) {
|
2012-07-11 05:47:55 +08:00
|
|
|
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);
|
|
|
|
}
|
2018-03-17 05:22:42 +08:00
|
|
|
if (BOP->getOpcode() == BO_LOr)
|
2013-08-16 07:06:33 +08:00
|
|
|
return getTrylockCallExpr(BOP->getRHS(), C, Negate);
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2018-10-06 09:09:28 +08:00
|
|
|
} else if (const auto *COP = dyn_cast<ConditionalOperator>(Cond)) {
|
|
|
|
bool TCond, FCond;
|
|
|
|
if (getStaticBooleanValue(COP->getTrueExpr(), TCond) &&
|
|
|
|
getStaticBooleanValue(COP->getFalseExpr(), FCond)) {
|
|
|
|
if (TCond && !FCond)
|
|
|
|
return getTrylockCallExpr(COP->getCond(), C, Negate);
|
|
|
|
if (!TCond && FCond) {
|
|
|
|
Negate = !Negate;
|
|
|
|
return getTrylockCallExpr(COP->getCond(), C, Negate);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Find the lockset that holds on the edge between PredBlock
|
2012-06-23 01:07:28 +08:00
|
|
|
/// 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();
|
2018-10-06 09:09:28 +08:00
|
|
|
// We don't acquire try-locks on ?: branches, only when its result is used.
|
2019-05-24 09:34:22 +08:00
|
|
|
if (!Cond || isa<ConditionalOperator>(PredBlock->getTerminatorStmt()))
|
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
|
|
|
|
2018-08-24 05:13:32 +08:00
|
|
|
const auto *Exp = 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
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
auto *FunDecl = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
|
2012-04-20 00:48:43 +08:00
|
|
|
if(!FunDecl || !FunDecl->hasAttrs())
|
2012-08-11 02:39:05 +08:00
|
|
|
return;
|
2012-04-20 00:48:43 +08:00
|
|
|
|
2014-08-05 00:10:59 +08:00
|
|
|
CapExprSet ExclusiveLocksToAdd;
|
|
|
|
CapExprSet SharedLocksToAdd;
|
2012-04-20 00:48:43 +08:00
|
|
|
|
|
|
|
// If the condition is a call to a Trylock function, then grab the attributes
|
2018-03-17 05:22:42 +08:00
|
|
|
for (const auto *Attr : FunDecl->attrs()) {
|
2012-04-20 00:48:43 +08:00
|
|
|
switch (Attr->getKind()) {
|
2018-04-13 01:53:21 +08:00
|
|
|
case attr::TryAcquireCapability: {
|
|
|
|
auto *A = cast<TryAcquireCapabilityAttr>(Attr);
|
|
|
|
getMutexIDs(A->isShared() ? SharedLocksToAdd : ExclusiveLocksToAdd, A,
|
|
|
|
Exp, FunDecl, PredBlock, CurrBlock, A->getSuccessValue(),
|
|
|
|
Negate);
|
|
|
|
CapDiagKind = ClassifyDiagnostic(A);
|
|
|
|
break;
|
|
|
|
};
|
2012-04-20 00:48:43 +08:00
|
|
|
case attr::ExclusiveTrylockFunction: {
|
2018-03-17 05:22:42 +08:00
|
|
|
const auto *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: {
|
2018-03-17 05:22:42 +08:00
|
|
|
const auto *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)
|
2014-09-03 14:00:11 +08:00
|
|
|
addLock(Result, llvm::make_unique<LockableFactEntry>(ExclusiveLockToAdd,
|
|
|
|
LK_Exclusive, Loc),
|
2014-04-02 05:43:23 +08:00
|
|
|
CapDiagKind);
|
|
|
|
for (const auto &SharedLockToAdd : SharedLocksToAdd)
|
2014-09-03 14:00:11 +08:00
|
|
|
addLock(Result, llvm::make_unique<LockableFactEntry>(SharedLockToAdd,
|
|
|
|
LK_Shared, Loc),
|
2014-08-05 00:10:59 +08:00
|
|
|
CapDiagKind);
|
2011-10-22 02:10:14 +08:00
|
|
|
}
|
|
|
|
|
2015-03-09 22:19:54 +08:00
|
|
|
namespace {
|
2018-03-17 05:22:42 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// We use this class to visit different types of expressions in
|
2012-04-20 00:48:43 +08:00
|
|
|
/// 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.
|
2018-08-24 05:53:04 +08:00
|
|
|
class BuildLockset : public ConstStmtVisitor<BuildLockset> {
|
2012-04-20 00:48:43 +08:00
|
|
|
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;
|
|
|
|
|
2014-08-05 06:13:06 +08:00
|
|
|
// 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,
|
2014-08-15 03:17:06 +08:00
|
|
|
StringRef DiagKind, SourceLocation Loc);
|
2014-04-02 05:43:23 +08:00
|
|
|
void warnIfMutexHeld(const NamedDecl *D, const Expr *Exp, Expr *MutexExp,
|
|
|
|
StringRef DiagKind);
|
2012-12-05 08:52:33 +08:00
|
|
|
|
2014-09-19 07:02:26 +08:00
|
|
|
void checkAccess(const Expr *Exp, AccessKind AK,
|
|
|
|
ProtectedOperationKind POK = POK_VarAccess);
|
|
|
|
void checkPtAccess(const Expr *Exp, AccessKind AK,
|
|
|
|
ProtectedOperationKind POK = POK_VarAccess);
|
2012-04-20 00:48:43 +08:00
|
|
|
|
2018-08-24 05:53:04 +08:00
|
|
|
void handleCall(const Expr *Exp, const NamedDecl *D, VarDecl *VD = nullptr);
|
2018-10-05 07:51:14 +08:00
|
|
|
void examineArguments(const FunctionDecl *FD,
|
|
|
|
CallExpr::const_arg_iterator ArgBegin,
|
|
|
|
CallExpr::const_arg_iterator ArgEnd,
|
|
|
|
bool SkipFirstParam = false);
|
2012-04-20 00:48:43 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
BuildLockset(ThreadSafetyAnalyzer *Anlzr, CFGBlockInfo &Info)
|
2018-08-24 05:53:04 +08:00
|
|
|
: ConstStmtVisitor<BuildLockset>(), Analyzer(Anlzr), FSet(Info.EntrySet),
|
2018-03-17 05:22:42 +08:00
|
|
|
LVarCtx(Info.EntryContext), CtxIndex(Info.EntryIndex) {}
|
2012-04-20 00:48:43 +08:00
|
|
|
|
2018-08-24 05:53:04 +08:00
|
|
|
void VisitUnaryOperator(const UnaryOperator *UO);
|
|
|
|
void VisitBinaryOperator(const BinaryOperator *BO);
|
|
|
|
void VisitCastExpr(const CastExpr *CE);
|
|
|
|
void VisitCallExpr(const CallExpr *Exp);
|
|
|
|
void VisitCXXConstructExpr(const CXXConstructExpr *Exp);
|
|
|
|
void VisitDeclStmt(const DeclStmt *S);
|
2012-04-20 00:48:43 +08:00
|
|
|
};
|
2018-03-17 05:22:42 +08:00
|
|
|
|
2015-03-09 22:19:54 +08:00
|
|
|
} // namespace
|
2014-08-05 00:10:59 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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,
|
2014-08-15 03:17:06 +08:00
|
|
|
StringRef DiagKind, SourceLocation Loc) {
|
2011-09-10 00:11:56 +08:00
|
|
|
LockKind LK = getLockKindFromAccessKind(AK);
|
2011-10-18 05:33:35 +08:00
|
|
|
|
2014-08-05 00:10:59 +08:00
|
|
|
CapabilityExpr Cp = Analyzer->SxBuilder.translateAttrExpr(MutexExp, D, Exp);
|
|
|
|
if (Cp.isInvalid()) {
|
|
|
|
warnInvalidLock(Analyzer->Handler, MutexExp, D, Exp, DiagKind);
|
2012-09-08 01:34:53 +08:00
|
|
|
return;
|
2014-08-05 00:10:59 +08:00
|
|
|
} else if (Cp.shouldIgnore()) {
|
2012-09-08 01:34:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-05 00:10:59 +08:00
|
|
|
if (Cp.negative()) {
|
|
|
|
// Negative capabilities act like locks excluded
|
2018-09-22 07:08:30 +08:00
|
|
|
const FactEntry *LDat = FSet.findLock(Analyzer->FactMan, !Cp);
|
2014-08-05 00:10:59 +08:00
|
|
|
if (LDat) {
|
|
|
|
Analyzer->Handler.handleFunExcludesLock(
|
2014-08-15 03:17:06 +08:00
|
|
|
DiagKind, D->getNameAsString(), (!Cp).toString(), Loc);
|
2014-08-05 00:10:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this does not refer to a negative capability in the same class,
|
|
|
|
// then stop here.
|
2014-08-05 06:13:06 +08:00
|
|
|
if (!Analyzer->inCurrentScope(Cp))
|
2014-08-05 00:10:59 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Otherwise the negative requirement must be propagated to the caller.
|
|
|
|
LDat = FSet.findLock(Analyzer->FactMan, Cp);
|
|
|
|
if (!LDat) {
|
|
|
|
Analyzer->Handler.handleMutexNotHeld("", D, POK, Cp.toString(),
|
2014-08-15 03:17:06 +08:00
|
|
|
LK_Shared, Loc);
|
2014-08-05 00:10:59 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-22 07:08:30 +08:00
|
|
|
const FactEntry *LDat = FSet.findLockUniv(Analyzer->FactMan, Cp);
|
2012-09-11 03:58:23 +08:00
|
|
|
bool NoError = true;
|
|
|
|
if (!LDat) {
|
|
|
|
// No exact match found. Look for a partial match.
|
2014-08-05 00:10:59 +08:00
|
|
|
LDat = FSet.findPartialMatch(Analyzer->FactMan, Cp);
|
|
|
|
if (LDat) {
|
2012-09-11 03:58:23 +08:00
|
|
|
// Warn that there's no precise match.
|
2014-08-05 00:10:59 +08:00
|
|
|
std::string PartMatchStr = LDat->toString();
|
2012-09-11 03:58:23 +08:00
|
|
|
StringRef PartMatchName(PartMatchStr);
|
2014-08-15 03:17:06 +08:00
|
|
|
Analyzer->Handler.handleMutexNotHeld(DiagKind, D, POK, Cp.toString(),
|
|
|
|
LK, Loc, &PartMatchName);
|
2012-09-11 03:58:23 +08:00
|
|
|
} else {
|
|
|
|
// Warn that there's no match at all.
|
2014-08-15 03:17:06 +08:00
|
|
|
Analyzer->Handler.handleMutexNotHeld(DiagKind, D, POK, Cp.toString(),
|
|
|
|
LK, Loc);
|
2012-09-11 03:58:23 +08:00
|
|
|
}
|
|
|
|
NoError = false;
|
|
|
|
}
|
|
|
|
// Make sure the mutex we found is the right kind.
|
2014-08-05 00:10:59 +08:00
|
|
|
if (NoError && LDat && !LDat->isAtLeast(LK)) {
|
2014-08-15 03:17:06 +08:00
|
|
|
Analyzer->Handler.handleMutexNotHeld(DiagKind, D, POK, Cp.toString(),
|
|
|
|
LK, Loc);
|
2014-08-05 00:10:59 +08:00
|
|
|
}
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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,
|
2014-08-15 03:17:06 +08:00
|
|
|
Expr *MutexExp, StringRef DiagKind) {
|
2014-08-05 00:10:59 +08:00
|
|
|
CapabilityExpr Cp = Analyzer->SxBuilder.translateAttrExpr(MutexExp, D, Exp);
|
|
|
|
if (Cp.isInvalid()) {
|
|
|
|
warnInvalidLock(Analyzer->Handler, MutexExp, D, Exp, DiagKind);
|
|
|
|
return;
|
|
|
|
} else if (Cp.shouldIgnore()) {
|
2012-09-08 01:34:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-22 07:08:30 +08:00
|
|
|
const FactEntry *LDat = FSet.findLock(Analyzer->FactMan, Cp);
|
2014-08-05 00:10:59 +08:00
|
|
|
if (LDat) {
|
2014-04-02 05:43:23 +08:00
|
|
|
Analyzer->Handler.handleFunExcludesLock(
|
2014-08-05 00:10:59 +08:00
|
|
|
DiagKind, D->getNameAsString(), Cp.toString(), Exp->getExprLoc());
|
|
|
|
}
|
2012-09-08 01:34:53 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Checks guarded_by and pt_guarded_by attributes.
|
2012-12-05 08:52:33 +08:00
|
|
|
/// 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.
|
2014-09-19 07:02:26 +08:00
|
|
|
void BuildLockset::checkAccess(const Expr *Exp, AccessKind AK,
|
|
|
|
ProtectedOperationKind POK) {
|
2016-12-03 09:14:32 +08:00
|
|
|
Exp = Exp->IgnoreImplicit()->IgnoreParenCasts();
|
2012-12-05 08:52:33 +08:00
|
|
|
|
2014-08-15 03:17:06 +08:00
|
|
|
SourceLocation Loc = Exp->getExprLoc();
|
|
|
|
|
2014-11-06 07:09:28 +08:00
|
|
|
// Local variables of reference type cannot be re-assigned;
|
|
|
|
// map them to their initializer.
|
|
|
|
while (const auto *DRE = dyn_cast<DeclRefExpr>(Exp)) {
|
|
|
|
const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()->getCanonicalDecl());
|
|
|
|
if (VD && VD->isLocalVarDecl() && VD->getType()->isReferenceType()) {
|
|
|
|
if (const auto *E = VD->getInit()) {
|
2018-08-25 02:48:35 +08:00
|
|
|
// Guard against self-initialization. e.g., int &i = i;
|
|
|
|
if (E == Exp)
|
|
|
|
break;
|
2014-11-06 07:09:28 +08:00
|
|
|
Exp = E;
|
|
|
|
continue;
|
2014-08-15 03:17:06 +08:00
|
|
|
}
|
|
|
|
}
|
2014-11-06 07:09:28 +08:00
|
|
|
break;
|
2014-08-15 03:17:06 +08:00
|
|
|
}
|
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
if (const auto *UO = dyn_cast<UnaryOperator>(Exp)) {
|
2012-12-05 08:52:33 +08:00
|
|
|
// For dereferences
|
2018-03-17 05:22:42 +08:00
|
|
|
if (UO->getOpcode() == UO_Deref)
|
2014-09-19 07:02:26 +08:00
|
|
|
checkPtAccess(UO->getSubExpr(), AK, POK);
|
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
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
if (const auto *AE = dyn_cast<ArraySubscriptExpr>(Exp)) {
|
2014-09-19 07:02:26 +08:00
|
|
|
checkPtAccess(AE->getLHS(), AK, POK);
|
2014-03-11 07:03:49 +08:00
|
|
|
return;
|
2013-11-09 03:42:01 +08:00
|
|
|
}
|
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
if (const auto *ME = dyn_cast<MemberExpr>(Exp)) {
|
2013-04-02 01:47:37 +08:00
|
|
|
if (ME->isArrow())
|
2014-09-19 07:02:26 +08:00
|
|
|
checkPtAccess(ME->getBase(), AK, POK);
|
2013-04-02 01:47:37 +08:00
|
|
|
else
|
2014-09-19 07:02:26 +08:00
|
|
|
checkAccess(ME->getBase(), AK, POK);
|
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;
|
|
|
|
|
2014-08-05 06:13:06 +08:00
|
|
|
if (D->hasAttr<GuardedVarAttr>() && FSet.isEmpty(Analyzer->FactMan)) {
|
2014-09-19 07:02:26 +08:00
|
|
|
Analyzer->Handler.handleNoMutexHeld("mutex", D, POK, AK, Loc);
|
2014-08-05 06:13:06 +08:00
|
|
|
}
|
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-09-19 07:02:26 +08:00
|
|
|
warnIfMutexNotHeld(D, Exp, AK, I->getArg(), POK,
|
2014-08-15 03:17:06 +08:00
|
|
|
ClassifyDiagnostic(I), Loc);
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Checks pt_guarded_by and pt_guarded_var attributes.
|
2014-09-19 07:02:26 +08:00
|
|
|
/// POK is the same operationKind that was passed to checkAccess.
|
|
|
|
void BuildLockset::checkPtAccess(const Expr *Exp, AccessKind AK,
|
|
|
|
ProtectedOperationKind POK) {
|
2014-03-11 07:03:49 +08:00
|
|
|
while (true) {
|
2018-03-17 05:22:42 +08:00
|
|
|
if (const auto *PE = dyn_cast<ParenExpr>(Exp)) {
|
2014-03-11 07:03:49 +08:00
|
|
|
Exp = PE->getSubExpr();
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-17 05:22:42 +08:00
|
|
|
if (const auto *CE = dyn_cast<CastExpr>(Exp)) {
|
2014-03-11 07:03:49 +08:00
|
|
|
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;
|
2014-09-19 07:02:26 +08:00
|
|
|
checkAccess(CE->getSubExpr(), AK, POK);
|
2014-03-11 07:03:49 +08:00
|
|
|
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
|
|
|
|
2014-09-19 07:02:26 +08:00
|
|
|
// Pass by reference warnings are under a different flag.
|
|
|
|
ProtectedOperationKind PtPOK = POK_VarDereference;
|
|
|
|
if (POK == POK_PassByRef) PtPOK = POK_PtPassByRef;
|
|
|
|
|
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;
|
|
|
|
|
2014-08-05 06:13:06 +08:00
|
|
|
if (D->hasAttr<PtGuardedVarAttr>() && FSet.isEmpty(Analyzer->FactMan))
|
2014-09-19 07:02:26 +08:00
|
|
|
Analyzer->Handler.handleNoMutexHeld("mutex", D, PtPOK, 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-09-19 07:02:26 +08:00
|
|
|
warnIfMutexNotHeld(D, Exp, AK, I->getArg(), PtPOK,
|
2014-08-15 03:17:06 +08:00
|
|
|
ClassifyDiagnostic(I), Exp->getExprLoc());
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Process a function call, method call, constructor call,
|
2011-10-22 02:06:53 +08:00
|
|
|
/// 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.
|
|
|
|
///
|
2018-08-24 05:53:04 +08:00
|
|
|
void BuildLockset::handleCall(const Expr *Exp, const NamedDecl *D,
|
|
|
|
VarDecl *VD) {
|
2013-05-18 07:02:59 +08:00
|
|
|
SourceLocation Loc = Exp->getExprLoc();
|
2014-08-05 00:10:59 +08:00
|
|
|
CapExprSet ExclusiveLocksToAdd, SharedLocksToAdd;
|
|
|
|
CapExprSet ExclusiveLocksToRemove, SharedLocksToRemove, GenericLocksToRemove;
|
2015-02-05 05:16:17 +08:00
|
|
|
CapExprSet ScopedExclusiveReqs, ScopedSharedReqs;
|
2014-04-02 05:43:23 +08:00
|
|
|
StringRef CapDiagKind = "mutex";
|
2012-07-06 05:16:29 +08:00
|
|
|
|
2018-01-12 06:13:57 +08:00
|
|
|
// Figure out if we're constructing an object of scoped lockable class
|
2018-08-13 20:50:30 +08:00
|
|
|
bool isScopedVar = false;
|
2015-02-05 05:16:17 +08:00
|
|
|
if (VD) {
|
2018-03-17 05:22:42 +08:00
|
|
|
if (const auto *CD = dyn_cast<const CXXConstructorDecl>(D)) {
|
2015-02-05 05:16:17 +08:00
|
|
|
const CXXRecordDecl* PD = CD->getParent();
|
|
|
|
if (PD && PD->hasAttr<ScopedLockableAttr>())
|
2018-08-13 20:50:30 +08:00
|
|
|
isScopedVar = true;
|
2015-02-05 05:16:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 21:03:16 +08:00
|
|
|
for(const Attr *At : D->attrs()) {
|
2012-07-06 05:16:29 +08:00
|
|
|
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: {
|
2018-03-17 05:22:42 +08:00
|
|
|
const auto *A = cast<AcquireCapabilityAttr>(At);
|
2014-03-21 00:02:49 +08:00
|
|
|
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: {
|
2018-03-17 05:22:42 +08:00
|
|
|
const auto *A = cast<AssertExclusiveLockAttr>(At);
|
2013-05-18 07:02:59 +08:00
|
|
|
|
2014-08-05 00:10:59 +08:00
|
|
|
CapExprSet AssertLocks;
|
2013-05-18 07:02:59 +08:00
|
|
|
Analyzer->getMutexIDs(AssertLocks, A, Exp, D, VD);
|
2014-04-02 05:43:23 +08:00
|
|
|
for (const auto &AssertLock : AssertLocks)
|
2014-09-03 14:00:11 +08:00
|
|
|
Analyzer->addLock(FSet,
|
|
|
|
llvm::make_unique<LockableFactEntry>(
|
|
|
|
AssertLock, LK_Exclusive, Loc, false, true),
|
2014-04-02 05:43:23 +08:00
|
|
|
ClassifyDiagnostic(A));
|
2013-05-18 07:02:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case attr::AssertSharedLock: {
|
2018-03-17 05:22:42 +08:00
|
|
|
const auto *A = cast<AssertSharedLockAttr>(At);
|
2013-05-18 07:02:59 +08:00
|
|
|
|
2014-08-05 00:10:59 +08:00
|
|
|
CapExprSet AssertLocks;
|
2013-05-18 07:02:59 +08:00
|
|
|
Analyzer->getMutexIDs(AssertLocks, A, Exp, D, VD);
|
2014-04-02 05:43:23 +08:00
|
|
|
for (const auto &AssertLock : AssertLocks)
|
2017-08-09 03:44:34 +08:00
|
|
|
Analyzer->addLock(FSet,
|
|
|
|
llvm::make_unique<LockableFactEntry>(
|
|
|
|
AssertLock, LK_Shared, Loc, false, true),
|
|
|
|
ClassifyDiagnostic(A));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case attr::AssertCapability: {
|
2018-03-17 05:22:42 +08:00
|
|
|
const auto *A = cast<AssertCapabilityAttr>(At);
|
2017-08-09 03:44:34 +08:00
|
|
|
CapExprSet AssertLocks;
|
|
|
|
Analyzer->getMutexIDs(AssertLocks, A, Exp, D, VD);
|
|
|
|
for (const auto &AssertLock : AssertLocks)
|
|
|
|
Analyzer->addLock(FSet,
|
|
|
|
llvm::make_unique<LockableFactEntry>(
|
|
|
|
AssertLock,
|
|
|
|
A->isShared() ? LK_Shared : LK_Exclusive, Loc,
|
|
|
|
false, true),
|
2014-04-02 05:43:23 +08:00
|
|
|
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: {
|
2018-03-17 05:22:42 +08:00
|
|
|
const 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: {
|
2018-03-17 05:22:42 +08:00
|
|
|
const auto *A = cast<RequiresCapabilityAttr>(At);
|
2015-02-05 05:16:17 +08:00
|
|
|
for (auto *Arg : A->args()) {
|
2014-05-02 21:35:42 +08:00
|
|
|
warnIfMutexNotHeld(D, Exp, A->isShared() ? AK_Read : AK_Written, Arg,
|
2014-08-15 03:17:06 +08:00
|
|
|
POK_FunctionCall, ClassifyDiagnostic(A),
|
|
|
|
Exp->getExprLoc());
|
2015-02-05 05:16:17 +08:00
|
|
|
// use for adopting a lock
|
2018-08-13 20:50:30 +08:00
|
|
|
if (isScopedVar) {
|
2015-02-05 05:16:17 +08:00
|
|
|
Analyzer->getMutexIDs(A->isShared() ? ScopedSharedReqs
|
|
|
|
: ScopedExclusiveReqs,
|
|
|
|
A, Exp, D, VD);
|
|
|
|
}
|
|
|
|
}
|
2011-09-10 00:11:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case attr::LocksExcluded: {
|
2018-03-17 05:22:42 +08:00
|
|
|
const auto *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
|
|
|
|
2018-07-26 21:03:16 +08:00
|
|
|
// Remove locks first to allow lock upgrading/downgrading.
|
|
|
|
// FIXME -- should only fully remove if the attribute refers to 'this'.
|
|
|
|
bool Dtor = isa<CXXDestructorDecl>(D);
|
|
|
|
for (const auto &M : ExclusiveLocksToRemove)
|
|
|
|
Analyzer->removeLock(FSet, M, Loc, Dtor, LK_Exclusive, CapDiagKind);
|
|
|
|
for (const auto &M : SharedLocksToRemove)
|
|
|
|
Analyzer->removeLock(FSet, M, Loc, Dtor, LK_Shared, CapDiagKind);
|
|
|
|
for (const auto &M : GenericLocksToRemove)
|
|
|
|
Analyzer->removeLock(FSet, M, Loc, Dtor, LK_Generic, CapDiagKind);
|
|
|
|
|
2012-07-06 05:16:29 +08:00
|
|
|
// Add locks.
|
2018-08-13 20:50:30 +08:00
|
|
|
for (const auto &M : ExclusiveLocksToAdd)
|
|
|
|
Analyzer->addLock(FSet, llvm::make_unique<LockableFactEntry>(
|
|
|
|
M, LK_Exclusive, Loc, isScopedVar),
|
|
|
|
CapDiagKind);
|
|
|
|
for (const auto &M : SharedLocksToAdd)
|
|
|
|
Analyzer->addLock(FSet, llvm::make_unique<LockableFactEntry>(
|
|
|
|
M, LK_Shared, Loc, isScopedVar),
|
|
|
|
CapDiagKind);
|
2012-07-06 05:16:29 +08:00
|
|
|
|
2018-08-13 20:50:30 +08:00
|
|
|
if (isScopedVar) {
|
2014-09-03 14:00:11 +08:00
|
|
|
// Add the managing object as a dummy mutex, mapped to the underlying mutex.
|
2012-07-06 05:16:29 +08:00
|
|
|
SourceLocation MLoc = VD->getLocation();
|
2018-12-21 22:10:18 +08:00
|
|
|
DeclRefExpr DRE(VD->getASTContext(), VD, false, VD->getType(), VK_LValue,
|
|
|
|
VD->getLocation());
|
2014-08-05 00:10:59 +08:00
|
|
|
// FIXME: does this store a pointer to DRE?
|
|
|
|
CapabilityExpr Scp = Analyzer->SxBuilder.translateAttrExpr(&DRE, nullptr);
|
2015-02-05 05:16:17 +08:00
|
|
|
|
2018-12-17 00:19:11 +08:00
|
|
|
auto ScopedEntry = llvm::make_unique<ScopedLockableFactEntry>(Scp, MLoc);
|
|
|
|
for (const auto &M : ExclusiveLocksToAdd)
|
|
|
|
ScopedEntry->addExclusiveLock(M);
|
|
|
|
for (const auto &M : ScopedExclusiveReqs)
|
|
|
|
ScopedEntry->addExclusiveLock(M);
|
|
|
|
for (const auto &M : SharedLocksToAdd)
|
|
|
|
ScopedEntry->addSharedLock(M);
|
|
|
|
for (const auto &M : ScopedSharedReqs)
|
|
|
|
ScopedEntry->addSharedLock(M);
|
|
|
|
for (const auto &M : ExclusiveLocksToRemove)
|
|
|
|
ScopedEntry->addExclusiveUnlock(M);
|
|
|
|
for (const auto &M : SharedLocksToRemove)
|
|
|
|
ScopedEntry->addSharedUnlock(M);
|
|
|
|
Analyzer->addLock(FSet, std::move(ScopedEntry), CapDiagKind);
|
2012-07-06 05:16:29 +08:00
|
|
|
}
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// For unary operations which read and write a variable, we need to
|
2011-10-22 02:06:53 +08:00
|
|
|
/// check whether we hold any required mutexes. Reads are checked in
|
|
|
|
/// VisitCastExpr.
|
2018-08-24 05:53:04 +08:00
|
|
|
void BuildLockset::VisitUnaryOperator(const UnaryOperator *UO) {
|
2011-10-22 02:06:53 +08:00
|
|
|
switch (UO->getOpcode()) {
|
2018-03-17 05:22:42 +08:00
|
|
|
case UO_PostDec:
|
|
|
|
case UO_PostInc:
|
|
|
|
case UO_PreDec:
|
|
|
|
case 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.
|
2018-08-24 05:53:04 +08:00
|
|
|
void BuildLockset::VisitBinaryOperator(const BinaryOperator *BO) {
|
2011-10-22 02:06:53 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2018-08-24 05:53:04 +08:00
|
|
|
void BuildLockset::VisitCastExpr(const CastExpr *CE) {
|
2011-10-22 02:06:53 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-05 07:51:14 +08:00
|
|
|
void BuildLockset::examineArguments(const FunctionDecl *FD,
|
|
|
|
CallExpr::const_arg_iterator ArgBegin,
|
|
|
|
CallExpr::const_arg_iterator ArgEnd,
|
|
|
|
bool SkipFirstParam) {
|
|
|
|
// Currently we can't do anything if we don't know the function declaration.
|
|
|
|
if (!FD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// NO_THREAD_SAFETY_ANALYSIS does double duty here. Normally it
|
|
|
|
// only turns off checking within the body of a function, but we also
|
|
|
|
// use it to turn off checking in arguments to the function. This
|
|
|
|
// could result in some false negatives, but the alternative is to
|
|
|
|
// create yet another attribute.
|
|
|
|
if (FD->hasAttr<NoThreadSafetyAnalysisAttr>())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ArrayRef<ParmVarDecl *> Params = FD->parameters();
|
|
|
|
auto Param = Params.begin();
|
|
|
|
if (SkipFirstParam)
|
|
|
|
++Param;
|
|
|
|
|
|
|
|
// There can be default arguments, so we stop when one iterator is at end().
|
|
|
|
for (auto Arg = ArgBegin; Param != Params.end() && Arg != ArgEnd;
|
|
|
|
++Param, ++Arg) {
|
|
|
|
QualType Qt = (*Param)->getType();
|
|
|
|
if (Qt->isReferenceType())
|
|
|
|
checkAccess(*Arg, AK_Read, POK_PassByRef);
|
|
|
|
}
|
|
|
|
}
|
2014-09-19 07:02:26 +08:00
|
|
|
|
2018-10-05 07:51:14 +08:00
|
|
|
void BuildLockset::VisitCallExpr(const CallExpr *Exp) {
|
2018-03-17 05:22:42 +08:00
|
|
|
if (const auto *CE = dyn_cast<CXXMemberCallExpr>(Exp)) {
|
|
|
|
const auto *ME = dyn_cast<MemberExpr>(CE->getCallee());
|
2013-04-02 01:47:37 +08:00
|
|
|
// ME can be null when calling a method pointer
|
2018-03-17 05:22:42 +08:00
|
|
|
const CXXMethodDecl *MD = CE->getMethodDecl();
|
2013-04-02 01:47:37 +08:00
|
|
|
|
|
|
|
if (ME && MD) {
|
|
|
|
if (ME->isArrow()) {
|
2018-03-17 05:22:42 +08:00
|
|
|
if (MD->isConst())
|
2013-04-02 01:47:37 +08:00
|
|
|
checkPtAccess(CE->getImplicitObjectArgument(), AK_Read);
|
2018-03-17 05:22:42 +08:00
|
|
|
else // FIXME -- should be AK_Written
|
2013-04-02 01:47:37 +08:00
|
|
|
checkPtAccess(CE->getImplicitObjectArgument(), AK_Read);
|
|
|
|
} 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
|
|
|
}
|
2014-09-19 07:02:26 +08:00
|
|
|
|
2018-10-05 07:51:14 +08:00
|
|
|
examineArguments(CE->getDirectCallee(), CE->arg_begin(), CE->arg_end());
|
|
|
|
} else if (const auto *OE = dyn_cast<CXXOperatorCallExpr>(Exp)) {
|
2014-09-19 07:02:26 +08:00
|
|
|
auto OEop = OE->getOperator();
|
|
|
|
switch (OEop) {
|
2013-04-02 01:47:37 +08:00
|
|
|
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:
|
2018-10-05 07:51:14 +08:00
|
|
|
case OO_Subscript:
|
2014-09-19 07:02:26 +08:00
|
|
|
if (!(OEop == OO_Star && OE->getNumArgs() > 1)) {
|
|
|
|
// Grrr. operator* can be multiplication...
|
2018-10-05 07:51:14 +08:00
|
|
|
checkPtAccess(OE->getArg(0), AK_Read);
|
2014-09-19 07:02:26 +08:00
|
|
|
}
|
2018-10-05 07:51:14 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2013-04-02 01:47:37 +08:00
|
|
|
default: {
|
2014-09-19 07:02:26 +08:00
|
|
|
// TODO: get rid of this, and rely on pass-by-ref instead.
|
2013-11-07 02:40:01 +08:00
|
|
|
const Expr *Obj = OE->getArg(0);
|
|
|
|
checkAccess(Obj, AK_Read);
|
2018-10-05 07:51:14 +08:00
|
|
|
// Check the remaining arguments. For method operators, the first
|
|
|
|
// argument is the implicit self argument, and doesn't appear in the
|
|
|
|
// FunctionDecl, but for non-methods it does.
|
|
|
|
const FunctionDecl *FD = OE->getDirectCallee();
|
|
|
|
examineArguments(FD, std::next(OE->arg_begin()), OE->arg_end(),
|
|
|
|
/*SkipFirstParam*/ !isa<CXXMethodDecl>(FD));
|
2013-04-02 01:47:37 +08:00
|
|
|
break;
|
2012-12-05 09:20:45 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-05 07:51:14 +08:00
|
|
|
} else {
|
|
|
|
examineArguments(Exp->getDirectCallee(), Exp->arg_begin(), Exp->arg_end());
|
2014-09-19 07:02:26 +08:00
|
|
|
}
|
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
auto *D = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl());
|
2011-10-22 02:06:53 +08:00
|
|
|
if(!D || !D->hasAttrs())
|
|
|
|
return;
|
|
|
|
handleCall(Exp, D);
|
|
|
|
}
|
|
|
|
|
2018-08-24 05:53:04 +08:00
|
|
|
void BuildLockset::VisitCXXConstructExpr(const 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);
|
2018-10-05 07:51:14 +08:00
|
|
|
} else {
|
|
|
|
examineArguments(D, Exp->arg_begin(), Exp->arg_end());
|
2012-12-05 09:20:45 +08:00
|
|
|
}
|
2011-12-09 04:23:06 +08:00
|
|
|
}
|
|
|
|
|
2018-01-12 06:13:57 +08:00
|
|
|
static CXXConstructorDecl *
|
|
|
|
findConstructorForByValueReturn(const CXXRecordDecl *RD) {
|
|
|
|
// Prefer a move constructor over a copy constructor. If there's more than
|
|
|
|
// one copy constructor or more than one move constructor, we arbitrarily
|
|
|
|
// pick the first declared such constructor rather than trying to guess which
|
|
|
|
// one is more appropriate.
|
|
|
|
CXXConstructorDecl *CopyCtor = nullptr;
|
2018-03-17 05:22:42 +08:00
|
|
|
for (auto *Ctor : RD->ctors()) {
|
2018-01-12 06:13:57 +08:00
|
|
|
if (Ctor->isDeleted())
|
|
|
|
continue;
|
|
|
|
if (Ctor->isMoveConstructor())
|
|
|
|
return Ctor;
|
|
|
|
if (!CopyCtor && Ctor->isCopyConstructor())
|
|
|
|
CopyCtor = Ctor;
|
|
|
|
}
|
|
|
|
return CopyCtor;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Expr *buildFakeCtorCall(CXXConstructorDecl *CD, ArrayRef<Expr *> Args,
|
|
|
|
SourceLocation Loc) {
|
|
|
|
ASTContext &Ctx = CD->getASTContext();
|
|
|
|
return CXXConstructExpr::Create(Ctx, Ctx.getRecordType(CD->getParent()), Loc,
|
|
|
|
CD, true, Args, false, false, false, false,
|
|
|
|
CXXConstructExpr::CK_Complete,
|
|
|
|
SourceRange(Loc, Loc));
|
|
|
|
}
|
|
|
|
|
2018-08-24 05:53:04 +08:00
|
|
|
void BuildLockset::VisitDeclStmt(const 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()) {
|
2018-03-17 05:22:42 +08:00
|
|
|
if (auto *VD = dyn_cast_or_null<VarDecl>(D)) {
|
2011-12-09 04:23:06 +08:00
|
|
|
Expr *E = VD->getInit();
|
2018-01-12 06:13:57 +08:00
|
|
|
if (!E)
|
|
|
|
continue;
|
|
|
|
E = E->IgnoreParens();
|
|
|
|
|
2012-07-04 02:25:56 +08:00
|
|
|
// handle constructors that involve temporaries
|
2018-01-12 06:13:57 +08:00
|
|
|
if (auto *EWC = dyn_cast<ExprWithCleanups>(E))
|
2012-07-04 02:25:56 +08:00
|
|
|
E = EWC->getSubExpr();
|
2018-01-12 06:13:57 +08:00
|
|
|
if (auto *BTE = dyn_cast<CXXBindTemporaryExpr>(E))
|
|
|
|
E = BTE->getSubExpr();
|
2012-07-04 02:25:56 +08:00
|
|
|
|
2018-03-17 05:22:42 +08:00
|
|
|
if (const auto *CE = dyn_cast<CXXConstructExpr>(E)) {
|
|
|
|
const auto *CtorD = dyn_cast_or_null<NamedDecl>(CE->getConstructor());
|
2011-12-09 04:23:06 +08:00
|
|
|
if (!CtorD || !CtorD->hasAttrs())
|
2018-01-12 06:13:57 +08:00
|
|
|
continue;
|
|
|
|
handleCall(E, CtorD, VD);
|
|
|
|
} else if (isa<CallExpr>(E) && E->isRValue()) {
|
|
|
|
// If the object is initialized by a function call that returns a
|
|
|
|
// scoped lockable by value, use the attributes on the copy or move
|
|
|
|
// constructor to figure out what effect that should have on the
|
|
|
|
// lockset.
|
|
|
|
// FIXME: Is this really the best way to handle this situation?
|
|
|
|
auto *RD = E->getType()->getAsCXXRecordDecl();
|
|
|
|
if (!RD || !RD->hasAttr<ScopedLockableAttr>())
|
|
|
|
continue;
|
|
|
|
CXXConstructorDecl *CtorD = findConstructorForByValueReturn(RD);
|
|
|
|
if (!CtorD || !CtorD->hasAttrs())
|
|
|
|
continue;
|
2018-08-10 05:08:08 +08:00
|
|
|
handleCall(buildFakeCtorCall(CtorD, {E}, E->getBeginLoc()), CtorD, VD);
|
2011-12-09 04:23:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-22 02:06:53 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Compute the intersection of two locksets and issue warnings for any
|
2011-09-16 01:25:19 +08:00
|
|
|
/// 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-08-05 00:10:59 +08:00
|
|
|
const FactEntry *LDat1 = nullptr;
|
|
|
|
const FactEntry *LDat2 = &FactMan[Fact];
|
|
|
|
FactSet::iterator Iter1 = FSet1.findLockIter(FactMan, *LDat2);
|
|
|
|
if (Iter1 != FSet1.end()) LDat1 = &FactMan[*Iter1];
|
|
|
|
|
|
|
|
if (LDat1) {
|
|
|
|
if (LDat1->kind() != LDat2->kind()) {
|
|
|
|
Handler.handleExclusiveAndShared("mutex", LDat2->toString(),
|
|
|
|
LDat2->loc(), LDat1->loc());
|
|
|
|
if (Modify && LDat1->kind() != LK_Exclusive) {
|
2013-05-21 01:57:55 +08:00
|
|
|
// Take the exclusive lock, which is the one in FSet2.
|
2014-08-05 00:10:59 +08:00
|
|
|
*Iter1 = Fact;
|
2012-08-11 02:39:05 +08:00
|
|
|
}
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
2014-08-05 00:10:59 +08:00
|
|
|
else if (Modify && LDat1->asserted() && !LDat2->asserted()) {
|
2013-05-21 01:57:55 +08:00
|
|
|
// The non-asserted lock in FSet2 is the one we want to track.
|
2014-08-05 00:10:59 +08:00
|
|
|
*Iter1 = Fact;
|
2013-05-18 07:02:59 +08:00
|
|
|
}
|
2011-09-10 00:11:56 +08:00
|
|
|
} else {
|
2014-09-03 14:00:11 +08:00
|
|
|
LDat2->handleRemovalFromIntersection(FSet2, FactMan, JoinLoc, LEK1,
|
|
|
|
Handler);
|
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-08-05 00:10:59 +08:00
|
|
|
const FactEntry *LDat1 = &FactMan[Fact];
|
|
|
|
const FactEntry *LDat2 = FSet2.findLock(FactMan, *LDat1);
|
2012-07-03 06:26:29 +08:00
|
|
|
|
2014-08-05 00:10:59 +08:00
|
|
|
if (!LDat2) {
|
2014-09-03 14:00:11 +08:00
|
|
|
LDat1->handleRemovalFromIntersection(FSet1Orig, FactMan, JoinLoc, LEK2,
|
|
|
|
Handler);
|
2012-08-11 02:39:05 +08:00
|
|
|
if (Modify)
|
2014-08-05 00:10:59 +08:00
|
|
|
FSet1.removeLock(FactMan, *LDat1);
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-19 06:15:45 +08:00
|
|
|
// Return true if block B never continues to its successors.
|
2015-03-09 22:19:54 +08:00
|
|
|
static bool neverReturns(const CFGBlock *B) {
|
2013-01-19 06:15:45 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Check a function's CFG for thread-safety violations.
|
2011-09-10 00:11:56 +08:00
|
|
|
///
|
|
|
|
/// 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();
|
2018-03-17 05:22:42 +08:00
|
|
|
const auto *CurrentFunction = dyn_cast<FunctionDecl>(D);
|
2014-08-05 00:10:59 +08:00
|
|
|
CurrentMethod = dyn_cast<CXXMethodDecl>(D);
|
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
|
|
|
|
2014-08-15 05:40:15 +08:00
|
|
|
Handler.enterFunction(CurrentFunction);
|
|
|
|
|
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);
|
|
|
|
|
2014-08-05 00:10:59 +08:00
|
|
|
CapExprSet ExclusiveLocksAcquired;
|
|
|
|
CapExprSet SharedLocksAcquired;
|
|
|
|
CapExprSet LocksReleased;
|
2013-04-09 04:11:11 +08:00
|
|
|
|
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;
|
2012-07-06 05:16:29 +08:00
|
|
|
|
2014-08-05 00:10:59 +08:00
|
|
|
CapExprSet ExclusiveLocksToAdd;
|
|
|
|
CapExprSet SharedLocksToAdd;
|
2014-04-02 05:43:23 +08:00
|
|
|
StringRef CapDiagKind = "mutex";
|
2012-07-06 05:16:29 +08:00
|
|
|
|
|
|
|
SourceLocation Loc = D->getLocation();
|
2015-02-04 06:11:04 +08:00
|
|
|
for (const auto *Attr : D->attrs()) {
|
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;
|
2018-08-04 03:37:45 +08:00
|
|
|
getMutexIDs(A->isShared() ? SharedLocksToAdd : ExclusiveLocksToAdd, A,
|
|
|
|
nullptr, D);
|
2014-04-18 21:13:15 +08:00
|
|
|
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)) {
|
2018-04-13 01:53:21 +08:00
|
|
|
// Don't try to check trylock functions for now.
|
2012-07-03 05:59:24 +08:00
|
|
|
return;
|
|
|
|
} else if (isa<SharedTrylockFunctionAttr>(Attr)) {
|
2018-04-13 01:53:21 +08:00
|
|
|
// Don't try to check trylock functions for now.
|
|
|
|
return;
|
|
|
|
} else if (isa<TryAcquireCapabilityAttr>(Attr)) {
|
|
|
|
// Don't try to check trylock functions for now.
|
2012-07-03 05:59:24 +08:00
|
|
|
return;
|
2011-09-16 01:43:08 +08:00
|
|
|
}
|
|
|
|
}
|
2012-07-06 05:16:29 +08:00
|
|
|
|
|
|
|
// FIXME -- Loc can be wrong here.
|
2015-02-04 06:11:04 +08:00
|
|
|
for (const auto &Mu : ExclusiveLocksToAdd) {
|
|
|
|
auto Entry = llvm::make_unique<LockableFactEntry>(Mu, LK_Exclusive, Loc);
|
|
|
|
Entry->setDeclared(true);
|
|
|
|
addLock(InitialLockset, std::move(Entry), CapDiagKind, true);
|
|
|
|
}
|
|
|
|
for (const auto &Mu : SharedLocksToAdd) {
|
|
|
|
auto Entry = llvm::make_unique<LockableFactEntry>(Mu, LK_Shared, Loc);
|
|
|
|
Entry->setDeclared(true);
|
|
|
|
addLock(InitialLockset, std::move(Entry), CapDiagKind, true);
|
|
|
|
}
|
2011-09-16 01:43:08 +08:00
|
|
|
}
|
|
|
|
|
2014-04-18 05:44:08 +08:00
|
|
|
for (const auto *CurrBlock : *SortedGraph) {
|
2018-09-23 05:56:16 +08:00
|
|
|
unsigned 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;
|
|
|
|
|
2018-09-23 05:56:16 +08:00
|
|
|
unsigned PrevBlockID = (*PI)->getBlockID();
|
2012-09-22 01:57:00 +08:00
|
|
|
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.
|
2019-05-24 09:34:22 +08:00
|
|
|
if (const Stmt *Terminator = (*PI)->getTerminatorStmt()) {
|
2012-02-03 11:30:07 +08:00
|
|
|
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) {
|
2018-09-23 05:56:16 +08:00
|
|
|
unsigned PrevBlockID = PrevBlock->getBlockID();
|
2012-02-03 11:30:07 +08:00
|
|
|
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.
|
2019-05-24 09:34:22 +08:00
|
|
|
const Stmt *Terminator = PrevBlock->getTerminatorStmt();
|
2012-02-03 11:30:07 +08:00
|
|
|
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.
|
2018-03-17 05:22:42 +08:00
|
|
|
for (const auto &BI : *CurrBlock) {
|
|
|
|
switch (BI.getKind()) {
|
2011-10-22 04:51:27 +08:00
|
|
|
case CFGElement::Statement: {
|
2018-03-17 05:22:42 +08:00
|
|
|
CFGStmt CS = BI.castAs<CFGStmt>();
|
2018-08-24 05:53:04 +08:00
|
|
|
LocksetBuilder.Visit(CS.getStmt());
|
2011-10-22 04:51:27 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Ignore BaseDtor, MemberDtor, and TemporaryDtor for now.
|
|
|
|
case CFGElement::AutomaticObjectDtor: {
|
2018-03-17 05:22:42 +08:00
|
|
|
CFGAutomaticObjDtor AD = BI.castAs<CFGAutomaticObjDtor>();
|
2018-08-24 05:53:04 +08:00
|
|
|
const auto *DD = AD.getDestructorDecl(AC.getASTContext());
|
2011-10-22 04:51:27 +08:00
|
|
|
if (!DD->hasAttrs())
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Create a dummy expression,
|
2018-03-17 05:22:42 +08:00
|
|
|
auto *VD = const_cast<VarDecl *>(AD.getVarDecl());
|
2018-12-21 22:10:18 +08:00
|
|
|
DeclRefExpr DRE(VD->getASTContext(), VD, false,
|
|
|
|
VD->getType().getNonReferenceType(), VK_LValue,
|
|
|
|
AD.getTriggerStmt()->getEndLoc());
|
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)
|
2014-09-03 14:00:11 +08:00
|
|
|
ExpectedExitSet.addLock(FactMan, llvm::make_unique<LockableFactEntry>(
|
|
|
|
Lock, LK_Exclusive, D->getLocation()));
|
2014-04-18 21:13:15 +08:00
|
|
|
for (const auto &Lock : SharedLocksAcquired)
|
2014-09-03 14:00:11 +08:00
|
|
|
ExpectedExitSet.addLock(FactMan, llvm::make_unique<LockableFactEntry>(
|
|
|
|
Lock, LK_Shared, D->getLocation()));
|
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);
|
2014-08-15 05:40:15 +08:00
|
|
|
|
|
|
|
Handler.leaveFunction(CurrentFunction);
|
2011-10-22 00:14:33 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Check a function's CFG for thread-safety violations.
|
2011-10-22 00:14:33 +08:00
|
|
|
///
|
|
|
|
/// 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.
|
2015-03-09 22:19:54 +08:00
|
|
|
void threadSafety::runThreadSafetyAnalysis(AnalysisDeclContext &AC,
|
|
|
|
ThreadSafetyHandler &Handler,
|
|
|
|
BeforeSet **BSet) {
|
2015-02-04 06:11:04 +08:00
|
|
|
if (!*BSet)
|
|
|
|
*BSet = new BeforeSet;
|
|
|
|
ThreadSafetyAnalyzer Analyzer(Handler, *BSet);
|
2011-10-22 00:14:33 +08:00
|
|
|
Analyzer.runAnalysis(AC);
|
2011-09-10 00:11:56 +08:00
|
|
|
}
|
|
|
|
|
2015-03-09 22:19:54 +08:00
|
|
|
void threadSafety::threadSafetyCleanup(BeforeSet *Cache) { delete Cache; }
|
2015-02-04 06:11:04 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Helper function that returns a LockKind required for the given level
|
2011-09-10 00:11:56 +08:00
|
|
|
/// of access.
|
2015-03-09 22:19:54 +08:00
|
|
|
LockKind threadSafety::getLockKindFromAccessKind(AccessKind AK) {
|
2011-09-10 00:11:56 +08:00
|
|
|
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
|
|
|
}
|