EpochTracker.h 3.4 KB

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