1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 11:02:59 +02:00
llvm-mirror/unittests/ADT
Duncan P. N. Exon Smith 9ac8992a85 ADT: Remove UB in ilist (and use a circular linked list)
This removes the undefined behaviour (UB) in ilist/ilist_node/etc.,
mainly by removing (gutting) the ilist_sentinel_traits customization
point and canonicalizing on a single, efficient memory layout.  This
fixes PR26753.

The new ilist is a doubly-linked circular list.
- ilist_node_base has two ilist_node_base*: Next and Prev.  Size-of: two
  pointers.
- ilist_node<T> (size-of: two pointers) is a type-safe wrapper around
  ilist_node_base.
- ilist_iterator<T> (size-of: two pointers) operates on an
  ilist_node<T>*, and downcasts to T* on dereference.
- ilist_sentinel<T> (size-of: two pointers) is a wrapper around
  ilist_node<T> that has some extra API for list management.
- ilist<T> (size-of: two pointers) has an ilist_sentinel<T>, whose
  address is returned for end().

The new memory layout matches ilist_half_embedded_sentinel_traits<T>
exactly.  The Head pointer that previously lived in ilist<T> is
effectively glued to the ilist_half_node<T> that lived in
ilist_half_embedded_sentinel_traits<T>, becoming the Next and Prev in
the ilist_sentinel_node<T>, respectively.  sizeof(ilist<T>) is now the
size of two pointers, and there is never any additional storage for a
sentinel.

This is a much simpler design for a doubly-linked list, removing most of
the corner cases of list manipulation (add, remove, etc.).  In follow-up
commits, I intend to move as many algorithms as possible into a
non-templated base class (ilist_base) to reduce code size.

Moreover, this fixes the UB in ilist_iterator/getNext/getPrev
operations.  Previously, ilist_iterator<T> operated on a T*, even when
the sentinel was not of type T (i.e., ilist_embedded_sentinel_traits and
ilist_half_embedded_sentinel_traits).  This added UB to all operations
involving end().   Now, ilist_iterator<T> operates on an ilist_node<T>*,
and only downcasts when the full type is guaranteed to be T*.

What did we lose?  There used to be a crash (in some configurations) on
++end().  Curiously (via UB), ++end() would return begin() for users of
ilist_half_embedded_sentinel_traits<T>, but otherwise ++end() would
cause a nice dependable nullptr dereference, crashing instead of a
possible infinite loop.  Options:
 1. Lose that behaviour.
 2. Keep it, by stealing a bit from Prev in asserts builds.
 3. Crash on dereference instead, using the same technique.

Hans convinced me (because of the number of problems this and r278532
exposed on Windows) that we really need some assertion here, at least in
the short term.  I've opted for #3 since I think it catches more bugs.

I added only a couple of unit tests to root out specific bugs I hit
during bring-up, but otherwise this is tested implicitly via the
extensive usage throughout LLVM.

Planned follow-ups:
- Remove ilist_*sentinel_traits<T>.  Here I've just gutted them to
  prevent build failures in sub-projects.  Once I stop referring to them
  in sub-projects, I'll come back and delete them.
- Add ilist_base and move algorithms there.
- Check and fix move construction and assignment.

Eventually, there are other interesting directions:
- Rewrite reverse iterators, so that rbegin().getNodePtr()==&*rbegin().
  This allows much simpler logic when erasing elements during a reverse
  traversal.
- Remove ilist_traits::createNode, by deleting the remaining API that
  creates nodes.  Intrusive lists shouldn't be creating nodes
  themselves.
- Remove ilist_traits::deleteNode, by (1) asserting that lists are empty
  on destruction and (2) changing API that calls it to take a Deleter
  functor (intrusive lists shouldn't be in the memory management
  business).
- Reconfigure the remaining callback traits (addNodeToList, etc.) to be
  higher-level, pulling out a simple_ilist<T> that is much easier to
  read and understand.
- Allow tags (e.g., ilist_node<T,tag1> and ilist_node<T,tag2>) so that T
  can be a member of multiple intrusive lists.

llvm-svn: 278974
2016-08-17 20:44:33 +00:00
..
APFloatTest.cpp [NFC] Header cleanup 2016-04-18 09:17:29 +00:00
APIntTest.cpp Fix UB in APInt::ashr 2016-08-10 19:50:14 +00:00
APSIntTest.cpp
ArrayRefTest.cpp [ADT] Pass ArrayRef::slice size_t instead of unsigned. 2016-06-02 17:26:03 +00:00
BitmaskEnumTest.cpp [ADT] Add LLVM_MARK_AS_BITMASK_ENUM, used to enable bitwise operations on enums without static_cast. 2016-07-13 18:23:16 +00:00
BitVectorTest.cpp Fix BitVector move ctor/assignment. 2016-06-16 21:45:13 +00:00
CMakeLists.txt [ADT] Add make_scope_exit(). 2016-08-10 17:52:09 +00:00
DAGDeltaAlgorithmTest.cpp
DeltaAlgorithmTest.cpp
DenseMapTest.cpp [DenseMap] Add a C++17-style try_emplace method. 2016-07-21 13:37:53 +00:00
DenseSetTest.cpp [ADT] Add a reserve() method to DenseSet as well as an insert() for R-value 2016-08-13 20:42:19 +00:00
FoldingSet.cpp Adding reserve and capacity methods to FoldingSet 2016-06-03 13:54:48 +00:00
FunctionRefTest.cpp
HashingTest.cpp
ilistTest.cpp ADT: Remove UB in ilist (and use a circular linked list) 2016-08-17 20:44:33 +00:00
ImmutableMapTest.cpp
ImmutableSetTest.cpp Fix warnings in ImmutableSetTest and SequenceTest. 2016-07-17 18:10:30 +00:00
IntEqClassesTest.cpp
IntervalMapTest.cpp
IntrusiveRefCntPtrTest.cpp
MakeUniqueTest.cpp
MapVectorTest.cpp
OptionalTest.cpp [ADT] Add relation operators for Optional 2016-08-11 20:10:15 +00:00
PackedVectorTest.cpp
PointerEmbeddedIntTest.cpp [ADT] Fix PointerEmbeddedInt when the underlying type is uintptr_t. 2016-02-18 21:00:08 +00:00
PointerIntPairTest.cpp Fix PointerIntPair so that it can use an enum class as its integer template argument. 2016-01-13 05:59:13 +00:00
PointerSumTypeTest.cpp [ADT] Add a sum type abstraction for pointer-like types. 2016-01-10 08:48:23 +00:00
PointerUnionTest.cpp
PostOrderIteratorTest.cpp [ADT] Change PostOrderIterator to use NodeRef. NFC. 2016-08-15 21:52:54 +00:00
PriorityWorklistTest.cpp [ADT] Add a new data structure for managing a priority worklist where 2016-06-30 02:32:20 +00:00
RangeAdapterTest.cpp
SCCIteratorTest.cpp [ADT] NFC: Generalize GraphTraits requirement of "NodeType *" in interfaces to "NodeRef", and migrate SCCIterator.h to use NodeRef 2016-08-01 22:32:20 +00:00
ScopeExitTest.cpp [ADT] Add make_scope_exit(). 2016-08-10 17:52:09 +00:00
SequenceTest.cpp Fix warnings in ImmutableSetTest and SequenceTest. 2016-07-17 18:10:30 +00:00
SetVectorTest.cpp [SetVector] Add erase() method 2016-03-25 19:28:08 +00:00
SmallPtrSetTest.cpp SmallPtrSetTest: More checks for the swap() testing 2016-01-29 03:34:36 +00:00
SmallStringTest.cpp Revert "Fix Clang-tidy modernize-deprecated-headers warnings in remaining files; other minor fixes." 2016-04-05 20:45:04 +00:00
SmallVectorTest.cpp Revert "Fix Clang-tidy modernize-deprecated-headers warnings in remaining files; other minor fixes." 2016-04-05 20:45:04 +00:00
SparseBitVectorTest.cpp
SparseMultiSetTest.cpp
SparseSetTest.cpp [ADT] Add a pop_back_val method to the SparseSet container. 2016-03-14 18:10:41 +00:00
StringMapTest.cpp Rename StringMap::emplace_second to try_emplace. 2016-07-21 13:37:48 +00:00
StringRefTest.cpp [ADT] Add 'consume_front' and 'consume_back' methods to StringRef which 2016-07-31 02:19:13 +00:00
TinyPtrVectorTest.cpp [NFC] Header cleanup 2016-04-18 09:17:29 +00:00
TripleTest.cpp Remove the Triple tests that stressing the TargetParser's behaviour. 2016-08-17 03:17:07 +00:00
TwineTest.cpp
VariadicFunctionTest.cpp