Skip to content

Commit

Permalink
[SmallPtrSet] Add iterator epoch tracking.
Browse files Browse the repository at this point in the history
This will detect invalid iterators when ABI breaking checks are enabled.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315746 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
d0k committed Oct 13, 2017
1 parent ab26bdd commit 937b560
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
22 changes: 15 additions & 7 deletions include/llvm/ADT/SmallPtrSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef LLVM_ADT_SMALLPTRSET_H
#define LLVM_ADT_SMALLPTRSET_H

#include "llvm/ADT/EpochTracker.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ReverseIteration.h"
#include "llvm/Support/type_traits.h"
Expand Down Expand Up @@ -46,7 +47,7 @@ namespace llvm {
/// (-2), to allow deletion. The hash table is resized when the table is 3/4 or
/// more. When this happens, the table is doubled in size.
///
class SmallPtrSetImplBase {
class SmallPtrSetImplBase : public DebugEpochBase {
friend class SmallPtrSetIteratorImpl;

protected:
Expand Down Expand Up @@ -92,6 +93,7 @@ class SmallPtrSetImplBase {
size_type size() const { return NumNonEmpty - NumTombstones; }

void clear() {
incrementEpoch();
// If the capacity of the array is huge, and the # elements used is small,
// shrink the array.
if (!isSmall()) {
Expand Down Expand Up @@ -138,12 +140,14 @@ class SmallPtrSetImplBase {
if (LastTombstone != nullptr) {
*LastTombstone = Ptr;
--NumTombstones;
incrementEpoch();
return std::make_pair(LastTombstone, true);
}

// Nope, there isn't. If we stay small, just 'pushback' now.
if (NumNonEmpty < CurArraySize) {
SmallArray[NumNonEmpty++] = Ptr;
incrementEpoch();
return std::make_pair(SmallArray + (NumNonEmpty - 1), true);
}
// Otherwise, hit the big set case, which will call grow.
Expand Down Expand Up @@ -259,8 +263,9 @@ class SmallPtrSetIteratorImpl {
};

/// SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
template<typename PtrTy>
class SmallPtrSetIterator : public SmallPtrSetIteratorImpl {
template <typename PtrTy>
class SmallPtrSetIterator : public SmallPtrSetIteratorImpl,
DebugEpochBase::HandleBase {
using PtrTraits = PointerLikeTypeTraits<PtrTy>;

public:
Expand All @@ -270,12 +275,14 @@ class SmallPtrSetIterator : public SmallPtrSetIteratorImpl {
using difference_type = std::ptrdiff_t;
using iterator_category = std::forward_iterator_tag;

explicit SmallPtrSetIterator(const void *const *BP, const void *const *E)
: SmallPtrSetIteratorImpl(BP, E) {}
explicit SmallPtrSetIterator(const void *const *BP, const void *const *E,
const DebugEpochBase &Epoch)
: SmallPtrSetIteratorImpl(BP, E), DebugEpochBase::HandleBase(&Epoch) {}

// Most methods provided by baseclass.

const PtrTy operator*() const {
assert(isHandleInSync() && "invalid iterator access!");
if (shouldReverseIterate()) {
assert(Bucket > End);
return PtrTraits::getFromVoidPointer(const_cast<void *>(Bucket[-1]));
Expand All @@ -285,6 +292,7 @@ class SmallPtrSetIterator : public SmallPtrSetIteratorImpl {
}

inline SmallPtrSetIterator& operator++() { // Preincrement
assert(isHandleInSync() && "invalid iterator access!");
if (shouldReverseIterate()) {
--Bucket;
RetreatIfNotValid();
Expand Down Expand Up @@ -397,8 +405,8 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
/// Create an iterator that dereferences to same place as the given pointer.
iterator makeIterator(const void *const *P) const {
if (shouldReverseIterate())
return iterator(P == EndPointer() ? CurArray : P + 1, CurArray);
return iterator(P, EndPointer());
return iterator(P == EndPointer() ? CurArray : P + 1, CurArray, *this);
return iterator(P, EndPointer(), *this);
}
};

Expand Down
1 change: 1 addition & 0 deletions lib/Support/SmallPtrSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ SmallPtrSetImplBase::insert_imp_big(const void *Ptr) {
else
++NumNonEmpty; // Track density.
*Bucket = Ptr;
incrementEpoch();
return std::make_pair(Bucket, true);
}

Expand Down

0 comments on commit 937b560

Please sign in to comment.