EpochTracker.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //===- llvm/ADT/EpochTracker.h - ADT epoch tracking --------------*- C++ -*-==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the DebugEpochBase and DebugEpochBase::HandleBase classes.
  11. // These can be used to write iterators that are fail-fast when LLVM is built
  12. // with asserts enabled.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_ADT_EPOCH_TRACKER_H
  16. #define LLVM_ADT_EPOCH_TRACKER_H
  17. #include "llvm/Config/llvm-config.h"
  18. #include <cstdint>
  19. namespace llvm {
  20. #ifndef LLVM_ENABLE_ABI_BREAKING_CHECKS
  21. class DebugEpochBase {
  22. public:
  23. void incrementEpoch() {}
  24. class HandleBase {
  25. public:
  26. HandleBase() = default;
  27. explicit HandleBase(const DebugEpochBase *) {}
  28. bool isHandleInSync() const { return true; }
  29. const void *getEpochAddress() const { return nullptr; }
  30. };
  31. };
  32. #else
  33. /// \brief A base class for data structure classes wishing to make iterators
  34. /// ("handles") pointing into themselves fail-fast. When building without
  35. /// asserts, this class is empty and does nothing.
  36. ///
  37. /// DebugEpochBase does not by itself track handles pointing into itself. The
  38. /// expectation is that routines touching the handles will poll on
  39. /// isHandleInSync at appropriate points to assert that the handle they're using
  40. /// is still valid.
  41. ///
  42. class DebugEpochBase {
  43. uint64_t Epoch;
  44. public:
  45. DebugEpochBase() : Epoch(0) {}
  46. /// \brief Calling incrementEpoch invalidates all handles pointing into the
  47. /// calling instance.
  48. void incrementEpoch() { ++Epoch; }
  49. /// \brief The destructor calls incrementEpoch to make use-after-free bugs
  50. /// more likely to crash deterministically.
  51. ~DebugEpochBase() { incrementEpoch(); }
  52. /// \brief A base class for iterator classes ("handles") that wish to poll for
  53. /// iterator invalidating modifications in the underlying data structure.
  54. /// When LLVM is built without asserts, this class is empty and does nothing.
  55. ///
  56. /// HandleBase does not track the parent data structure by itself. It expects
  57. /// the routines modifying the data structure to call incrementEpoch when they
  58. /// make an iterator-invalidating modification.
  59. ///
  60. class HandleBase {
  61. const uint64_t *EpochAddress;
  62. uint64_t EpochAtCreation;
  63. public:
  64. HandleBase() : EpochAddress(nullptr), EpochAtCreation(UINT64_MAX) {}
  65. explicit HandleBase(const DebugEpochBase *Parent)
  66. : EpochAddress(&Parent->Epoch), EpochAtCreation(Parent->Epoch) {}
  67. /// \brief Returns true if the DebugEpochBase this Handle is linked to has
  68. /// not called incrementEpoch on itself since the creation of this
  69. /// HandleBase instance.
  70. bool isHandleInSync() const { return *EpochAddress == EpochAtCreation; }
  71. /// \brief Returns a pointer to the epoch word stored in the data structure
  72. /// this handle points into. Can be used to check if two iterators point
  73. /// into the same data structure.
  74. const void *getEpochAddress() const { return EpochAddress; }
  75. };
  76. };
  77. #endif // LLVM_ENABLE_ABI_BREAKING_CHECKS
  78. } // namespace llvm
  79. #endif