Skip to content

Commit

Permalink
[ADT][CMake][AutoConf] Fail-fast iterators for DenseMap
Browse files Browse the repository at this point in the history
Summary:
This patch is an attempt at making `DenseMapIterator`s "fail-fast".
Fail-fast iterators that have been invalidated due to insertion into
the host `DenseMap` deterministically trip an assert (in debug mode)
on access, instead of non-deterministically hitting memory corruption
issues.

Enabling fail-fast iterators breaks the LLVM C++ ABI, so they are
predicated on `LLVM_ENABLE_ABI_BREAKING_CHECKS`.
`LLVM_ENABLE_ABI_BREAKING_CHECKS` by default flips with
`LLVM_ENABLE_ASSERTS`, but can be clamped to ON or OFF using the CMake /
autoconf build system.

Reviewers: chandlerc, dexonsmith, rnk, zturner

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D8351

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@233310 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
sanjoy committed Mar 26, 2015
1 parent 55a5cb1 commit 784545f
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ else()
option(LLVM_ENABLE_ASSERTIONS "Enable assertions" ON)
endif()

set(LLVM_ABI_BREAKING_CHECKS "WITH_ASSERTS" CACHE STRING
"Enable abi-breaking checks. Can be WITH_ASSERTS, FORCE_ON or FORCE_OFF.")

option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN
"Set to ON to force using an old, unsupported host toolchain." OFF)

Expand Down
16 changes: 16 additions & 0 deletions autoconf/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,10 @@ AC_ARG_ENABLE(assertions,AS_HELP_STRING(
--enable-assertions,[Compile with assertion checks enabled (default is YES)]),, enableval="yes")
if test ${enableval} = "yes" ; then
AC_SUBST(DISABLE_ASSERTIONS,[[]])
assertions_enabled="yes"
else
AC_SUBST(DISABLE_ASSERTIONS,[[DISABLE_ASSERTIONS=1]])
assertions_enabled="no"
fi

dnl --enable-werror : check whether we want Werror on by default
Expand All @@ -726,6 +728,20 @@ else
AC_SUBST(EXPENSIVE_CHECKS,[[no]])
fi

dnl --enable-abi-breaking-checks : decide whether we should compile in asserts and
dnl checks that make the build ABI incompatible with an llvm built without these
dnl checks enabled.
AC_ARG_ENABLE(abi-breaking-checks,AS_HELP_STRING(
--enable-abi-breaking-checks,[Compile with abi-breaking asserts support (default is with-asserts)]),, enableval="with-asserts")
case "$enableval" in
with-asserts) if test ${assertions_enabled} = "yes" ; then
AC_DEFINE([LLVM_ENABLE_ABI_BREAKING_CHECKS],[1],[Define to enable checks that alter the LLVM C++ ABI])
fi ;;
yes) AC_DEFINE([LLVM_ENABLE_ABI_BREAKING_CHECKS],[1],[Define to enable checks that alter the LLVM C++ ABI]) ;;
no) ;;
*) AC_MSG_ERROR([Invalid setting for --enable-abi-breaking-checks. Use "with-asserts", "yes" or "no"])
esac

dnl --enable-debug-runtime : should runtime libraries have debug symbols?
AC_ARG_ENABLE(debug-runtime,
AS_HELP_STRING(--enable-debug-runtime,[Build runtime libs with debug symbols (default is NO)]),,enableval=no)
Expand Down
14 changes: 14 additions & 0 deletions cmake/modules/HandleLLVMOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ if( LLVM_ENABLE_ASSERTIONS )
endif()
endif()

string(TOUPPER "${LLVM_ABI_BREAKING_CHECKS}" uppercase_LLVM_ABI_BREAKING_CHECKS)

if( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "WITH_ASSERTS" )
if( LLVM_ENABLE_ASSERTIONS )
set( LLVM_ENABLE_ABI_BREAKING_CHECKS 1 )
endif()
elseif( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "FORCE_ON" )
set( LLVM_ENABLE_ABI_BREAKING_CHECKS 1 )
elseif( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "FORCE_OFF" )
# We don't need to do anything special to turn off ABI breaking checks.
else()
message(FATAL_ERROR "Unknown value for LLVM_ABI_BREAKING_CHECKS: \"${LLVM_ABI_BREAKING_CHECKS}\"!")
endif()

if(WIN32)
set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
if(CYGWIN)
Expand Down
31 changes: 31 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,9 @@ Optional Features:
--enable-expensive-checks
Compile with expensive debug checks enabled (default
is NO)
--enable-abi-breaking-checks
Compile with abi-breaking asserts support (default
is with-asserts)
--enable-debug-runtime Build runtime libs with debug symbols (default is
NO)
--enable-debug-symbols Build compiler with debug symbols (default is NO if
Expand Down Expand Up @@ -4980,9 +4983,11 @@ fi
if test ${enableval} = "yes" ; then
DISABLE_ASSERTIONS=

assertions_enabled="yes"
else
DISABLE_ASSERTIONS=DISABLE_ASSERTIONS=1

assertions_enabled="no"
fi

# Check whether --enable-werror was given.
Expand Down Expand Up @@ -5023,6 +5028,32 @@ else

fi

# Check whether --enable-abi-breaking-checks was given.
if test "${enable_abi_breaking_checks+set}" = set; then
enableval=$enable_abi_breaking_checks;
else
enableval="with-asserts"
fi

case "$enableval" in
with-asserts) if test ${assertions_enabled} = "yes" ; then

cat >>confdefs.h <<\_ACEOF
#define LLVM_ENABLE_ABI_BREAKING_CHECKS 1
_ACEOF

fi ;;
yes)
cat >>confdefs.h <<\_ACEOF
#define LLVM_ENABLE_ABI_BREAKING_CHECKS 1
_ACEOF
;;
no) ;;
*) { { echo "$as_me:$LINENO: error: Invalid setting for --enable-abi-breaking-checks. Use \"with-asserts\", \"yes\" or \"no\"" >&5
echo "$as_me: error: Invalid setting for --enable-abi-breaking-checks. Use \"with-asserts\", \"yes\" or \"no\"" >&2;}
{ (exit 1); exit 1; }; }
esac

# Check whether --enable-debug-runtime was given.
if test "${enable_debug_runtime+set}" = set; then
enableval=$enable_debug_runtime;
Expand Down
9 changes: 9 additions & 0 deletions docs/CMake.rst
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,15 @@ LLVM-specific variables
**LLVM_ENABLE_WERROR**:BOOL
Stop and fail build, if a compiler warning is triggered. Defaults to OFF.

**LLVM_ABI_BREAKING_CHECKS**:STRING
Used to decide if LLVM should be built with ABI breaking checks or
not. Allowed values are `WITH_ASSERTS` (default), `FORCE_ON` and
`FORCE_OFF`. `WITH_ASSERTS` turns on ABI breaking checks in an
assertion enabled build. `FORCE_ON` (`FORCE_OFF`) turns them on
(off) irrespective of whether normal (`NDEBUG` based) assertions are
enabled or not. A version of LLVM built with ABI breaking checks
is not ABI compatible with a version built without it.

**LLVM_BUILD_32_BITS**:BOOL
Build 32-bits executables and libraries on 64-bits systems. This option is
available only on some 64-bits unix systems. Defaults to OFF.
Expand Down
16 changes: 16 additions & 0 deletions docs/ProgrammersManual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2553,6 +2553,22 @@ section on :ref:`isa and dyn_cast <isa>` and our :doc:`detailed document
<HowToSetUpLLVMStyleRTTI>` which describes how you can implement this
pattern for use with the LLVM helpers.

.. _abi_breaking_checks:

ABI Breaking Checks
-------------------

Checks and asserts that alter the LLVM C++ ABI are predicated on the
preprocessor symbol `LLVM_ENABLE_ABI_BREAKING_CHECKS` -- LLVM
libraries built with `LLVM_ENABLE_ABI_BREAKING_CHECKS` are not ABI
compatible LLVM libraries built without it defined. By default,
turning on assertions also turns on `LLVM_ENABLE_ABI_BREAKING_CHECKS`
so a default +Asserts build is not ABI compatible with a
default -Asserts build. Clients that want ABI compatibility
between +Asserts and -Asserts builds should use the CMake or autoconf
build systems to set `LLVM_ENABLE_ABI_BREAKING_CHECKS` independently
of `LLVM_ENABLE_ASSERTIONS`.

.. _coreclasses:

The Core LLVM Class Hierarchy Reference
Expand Down
63 changes: 43 additions & 20 deletions include/llvm/ADT/DenseMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define LLVM_ADT_DENSEMAP_H

#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/EpochTracker.h"
#include "llvm/Support/AlignOf.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MathExtras.h"
Expand Down Expand Up @@ -50,7 +51,7 @@ class DenseMapIterator;

template <typename DerivedT, typename KeyT, typename ValueT, typename KeyInfoT,
typename BucketT>
class DenseMapBase {
class DenseMapBase : public DebugEpochBase {
public:
typedef unsigned size_type;
typedef KeyT key_type;
Expand All @@ -62,16 +63,17 @@ class DenseMapBase {
const_iterator;
inline iterator begin() {
// When the map is empty, avoid the overhead of AdvancePastEmptyBuckets().
return empty() ? end() : iterator(getBuckets(), getBucketsEnd());
return empty() ? end() : iterator(getBuckets(), getBucketsEnd(), *this);
}
inline iterator end() {
return iterator(getBucketsEnd(), getBucketsEnd(), true);
return iterator(getBucketsEnd(), getBucketsEnd(), *this, true);
}
inline const_iterator begin() const {
return empty() ? end() : const_iterator(getBuckets(), getBucketsEnd());
return empty() ? end()
: const_iterator(getBuckets(), getBucketsEnd(), *this);
}
inline const_iterator end() const {
return const_iterator(getBucketsEnd(), getBucketsEnd(), true);
return const_iterator(getBucketsEnd(), getBucketsEnd(), *this, true);
}

bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const {
Expand All @@ -81,11 +83,13 @@ class DenseMapBase {

/// Grow the densemap so that it has at least Size buckets. Does not shrink
void resize(size_type Size) {
incrementEpoch();
if (Size > getNumBuckets())
grow(Size);
}

void clear() {
incrementEpoch();
if (getNumEntries() == 0 && getNumTombstones() == 0) return;

// If the capacity of the array is huge, and the # elements used is small,
Expand Down Expand Up @@ -118,13 +122,13 @@ class DenseMapBase {
iterator find(const KeyT &Val) {
BucketT *TheBucket;
if (LookupBucketFor(Val, TheBucket))
return iterator(TheBucket, getBucketsEnd(), true);
return iterator(TheBucket, getBucketsEnd(), *this, true);
return end();
}
const_iterator find(const KeyT &Val) const {
const BucketT *TheBucket;
if (LookupBucketFor(Val, TheBucket))
return const_iterator(TheBucket, getBucketsEnd(), true);
return const_iterator(TheBucket, getBucketsEnd(), *this, true);
return end();
}

Expand All @@ -137,14 +141,14 @@ class DenseMapBase {
iterator find_as(const LookupKeyT &Val) {
BucketT *TheBucket;
if (LookupBucketFor(Val, TheBucket))
return iterator(TheBucket, getBucketsEnd(), true);
return iterator(TheBucket, getBucketsEnd(), *this, true);
return end();
}
template<class LookupKeyT>
const_iterator find_as(const LookupKeyT &Val) const {
const BucketT *TheBucket;
if (LookupBucketFor(Val, TheBucket))
return const_iterator(TheBucket, getBucketsEnd(), true);
return const_iterator(TheBucket, getBucketsEnd(), *this, true);
return end();
}

Expand All @@ -163,12 +167,13 @@ class DenseMapBase {
std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
BucketT *TheBucket;
if (LookupBucketFor(KV.first, TheBucket))
return std::make_pair(iterator(TheBucket, getBucketsEnd(), true),
return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
false); // Already in map.

// Otherwise, insert the new element.
TheBucket = InsertIntoBucket(KV.first, KV.second, TheBucket);
return std::make_pair(iterator(TheBucket, getBucketsEnd(), true), true);
return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
true);
}

// Inserts key,value pair into the map if the key isn't already in the map.
Expand All @@ -177,14 +182,15 @@ class DenseMapBase {
std::pair<iterator, bool> insert(std::pair<KeyT, ValueT> &&KV) {
BucketT *TheBucket;
if (LookupBucketFor(KV.first, TheBucket))
return std::make_pair(iterator(TheBucket, getBucketsEnd(), true),
return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
false); // Already in map.

// Otherwise, insert the new element.
TheBucket = InsertIntoBucket(std::move(KV.first),
std::move(KV.second),
TheBucket);
return std::make_pair(iterator(TheBucket, getBucketsEnd(), true), true);
return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
true);
}

/// insert - Range insertion of pairs.
Expand Down Expand Up @@ -431,6 +437,8 @@ class DenseMapBase {
}

BucketT *InsertIntoBucketImpl(const KeyT &Key, BucketT *TheBucket) {
incrementEpoch();

// If the load of the hash table is more than 3/4, or if fewer than 1/8 of
// the buckets are empty (meaning that many are filled with tombstones),
// grow the table.
Expand Down Expand Up @@ -987,9 +995,10 @@ class SmallDenseMap

template <typename KeyT, typename ValueT, typename KeyInfoT, typename Bucket,
bool IsConst>
class DenseMapIterator {
class DenseMapIterator : DebugEpochBase::HandleBase {
typedef DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, true> ConstIterator;
friend class DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, true>;
friend class DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, false>;

public:
typedef ptrdiff_t difference_type;
Expand All @@ -1003,8 +1012,10 @@ class DenseMapIterator {
public:
DenseMapIterator() : Ptr(nullptr), End(nullptr) {}

DenseMapIterator(pointer Pos, pointer E, bool NoAdvance = false)
: Ptr(Pos), End(E) {
DenseMapIterator(pointer Pos, pointer E, const DebugEpochBase &Epoch,
bool NoAdvance = false)
: DebugEpochBase::HandleBase(&Epoch), Ptr(Pos), End(E) {
assert(isHandleInSync() && "invalid construction!");
if (!NoAdvance) AdvancePastEmptyBuckets();
}

Expand All @@ -1015,28 +1026,40 @@ class DenseMapIterator {
typename = typename std::enable_if<!IsConstSrc && IsConst>::type>
DenseMapIterator(
const DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, IsConstSrc> &I)
: Ptr(I.Ptr), End(I.End) {}
: DebugEpochBase::HandleBase(I), Ptr(I.Ptr), End(I.End) {}

reference operator*() const {
assert(isHandleInSync() && "invalid iterator access!");
return *Ptr;
}
pointer operator->() const {
assert(isHandleInSync() && "invalid iterator access!");
return Ptr;
}

bool operator==(const ConstIterator &RHS) const {
return Ptr == RHS.operator->();
assert((!Ptr || isHandleInSync()) && "handle not in sync!");
assert((!RHS.Ptr || RHS.isHandleInSync()) && "handle not in sync!");
assert(getEpochAddress() == RHS.getEpochAddress() &&
"comparing incomparable iterators!");
return Ptr == RHS.Ptr;
}
bool operator!=(const ConstIterator &RHS) const {
return Ptr != RHS.operator->();
assert((!Ptr || isHandleInSync()) && "handle not in sync!");
assert((!RHS.Ptr || RHS.isHandleInSync()) && "handle not in sync!");
assert(getEpochAddress() == RHS.getEpochAddress() &&
"comparing incomparable iterators!");
return Ptr != RHS.Ptr;
}

inline DenseMapIterator& operator++() { // Preincrement
assert(isHandleInSync() && "invalid iterator access!");
++Ptr;
AdvancePastEmptyBuckets();
return *this;
}
DenseMapIterator operator++(int) { // Postincrement
assert(isHandleInSync() && "invalid iterator access!");
DenseMapIterator tmp = *this; ++*this; return tmp;
}

Expand Down
Loading

0 comments on commit 784545f

Please sign in to comment.