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