EpochTracker.h 3.4 KB

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