sanitizer_flat_map.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //===-- sanitizer_flat_map.h ------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Part of the Sanitizer Allocator.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SANITIZER_FLAT_MAP_H
  13. #define SANITIZER_FLAT_MAP_H
  14. #include "sanitizer_atomic.h"
  15. #include "sanitizer_common.h"
  16. #include "sanitizer_internal_defs.h"
  17. #include "sanitizer_local_address_space_view.h"
  18. #include "sanitizer_mutex.h"
  19. namespace __sanitizer {
  20. // Maps integers in rage [0, kSize) to values.
  21. template <typename T, u64 kSize,
  22. typename AddressSpaceViewTy = LocalAddressSpaceView>
  23. class FlatMap {
  24. public:
  25. using AddressSpaceView = AddressSpaceViewTy;
  26. void Init() { internal_memset(map_, 0, sizeof(map_)); }
  27. constexpr uptr size() const { return kSize; }
  28. bool contains(uptr idx) const {
  29. CHECK_LT(idx, kSize);
  30. return true;
  31. }
  32. T &operator[](uptr idx) {
  33. DCHECK_LT(idx, kSize);
  34. return map_[idx];
  35. }
  36. const T &operator[](uptr idx) const {
  37. DCHECK_LT(idx, kSize);
  38. return map_[idx];
  39. }
  40. private:
  41. T map_[kSize];
  42. };
  43. // TwoLevelMap maps integers in range [0, kSize1*kSize2) to values.
  44. // It is implemented as a two-dimensional array: array of kSize1 pointers
  45. // to kSize2-byte arrays. The secondary arrays are mmaped on demand.
  46. // Each value is initially zero and can be set to something else only once.
  47. // Setting and getting values from multiple threads is safe w/o extra locking.
  48. template <typename T, u64 kSize1, u64 kSize2,
  49. typename AddressSpaceViewTy = LocalAddressSpaceView>
  50. class TwoLevelMap {
  51. static_assert(IsPowerOfTwo(kSize2), "Use a power of two for performance.");
  52. public:
  53. using AddressSpaceView = AddressSpaceViewTy;
  54. void Init() {
  55. mu_.Init();
  56. internal_memset(map1_, 0, sizeof(map1_));
  57. }
  58. void TestOnlyUnmap() {
  59. for (uptr i = 0; i < kSize1; i++) {
  60. T *p = Get(i);
  61. if (!p)
  62. continue;
  63. UnmapOrDie(p, kSize2);
  64. }
  65. Init();
  66. }
  67. uptr MemoryUsage() const {
  68. uptr res = 0;
  69. for (uptr i = 0; i < kSize1; i++) {
  70. T *p = Get(i);
  71. if (!p)
  72. continue;
  73. res += MmapSize();
  74. }
  75. return res;
  76. }
  77. constexpr uptr size() const { return kSize1 * kSize2; }
  78. constexpr uptr size1() const { return kSize1; }
  79. constexpr uptr size2() const { return kSize2; }
  80. bool contains(uptr idx) const {
  81. CHECK_LT(idx, kSize1 * kSize2);
  82. return Get(idx / kSize2);
  83. }
  84. const T &operator[](uptr idx) const {
  85. DCHECK_LT(idx, kSize1 * kSize2);
  86. T *map2 = GetOrCreate(idx / kSize2);
  87. return *AddressSpaceView::Load(&map2[idx % kSize2]);
  88. }
  89. T &operator[](uptr idx) {
  90. DCHECK_LT(idx, kSize1 * kSize2);
  91. T *map2 = GetOrCreate(idx / kSize2);
  92. return *AddressSpaceView::LoadWritable(&map2[idx % kSize2]);
  93. }
  94. void Lock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS { mu_.Lock(); }
  95. void Unlock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS { mu_.Unlock(); }
  96. private:
  97. constexpr uptr MmapSize() const {
  98. return RoundUpTo(kSize2 * sizeof(T), GetPageSizeCached());
  99. }
  100. T *Get(uptr idx) const {
  101. DCHECK_LT(idx, kSize1);
  102. return reinterpret_cast<T *>(
  103. atomic_load(&map1_[idx], memory_order_acquire));
  104. }
  105. T *GetOrCreate(uptr idx) const {
  106. DCHECK_LT(idx, kSize1);
  107. // This code needs to use memory_order_acquire/consume, but we use
  108. // memory_order_relaxed for performance reasons (matters for arm64). We
  109. // expect memory_order_relaxed to be effectively equivalent to
  110. // memory_order_consume in this case for all relevant architectures: all
  111. // dependent data is reachable only by dereferencing the resulting pointer.
  112. // If relaxed load fails to see stored ptr, the code will fall back to
  113. // Create() and reload the value again with locked mutex as a memory
  114. // barrier.
  115. T *res = reinterpret_cast<T *>(atomic_load_relaxed(&map1_[idx]));
  116. if (LIKELY(res))
  117. return res;
  118. return Create(idx);
  119. }
  120. NOINLINE T *Create(uptr idx) const {
  121. SpinMutexLock l(&mu_);
  122. T *res = Get(idx);
  123. if (!res) {
  124. res = reinterpret_cast<T *>(MmapOrDie(MmapSize(), "TwoLevelMap"));
  125. atomic_store(&map1_[idx], reinterpret_cast<uptr>(res),
  126. memory_order_release);
  127. }
  128. return res;
  129. }
  130. mutable StaticSpinMutex mu_;
  131. mutable atomic_uintptr_t map1_[kSize1];
  132. };
  133. template <u64 kSize, typename AddressSpaceViewTy = LocalAddressSpaceView>
  134. using FlatByteMap = FlatMap<u8, kSize, AddressSpaceViewTy>;
  135. template <u64 kSize1, u64 kSize2,
  136. typename AddressSpaceViewTy = LocalAddressSpaceView>
  137. using TwoLevelByteMap = TwoLevelMap<u8, kSize1, kSize2, AddressSpaceViewTy>;
  138. } // namespace __sanitizer
  139. #endif