tsan_shadow.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //===-- tsan_shadow.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. #ifndef TSAN_SHADOW_H
  9. #define TSAN_SHADOW_H
  10. #include "tsan_defs.h"
  11. namespace __tsan {
  12. class FastState {
  13. public:
  14. FastState() { Reset(); }
  15. void Reset() {
  16. part_.unused0_ = 0;
  17. part_.sid_ = static_cast<u8>(kFreeSid);
  18. part_.epoch_ = static_cast<u16>(kEpochLast);
  19. part_.unused1_ = 0;
  20. part_.ignore_accesses_ = false;
  21. }
  22. void SetSid(Sid sid) { part_.sid_ = static_cast<u8>(sid); }
  23. Sid sid() const { return static_cast<Sid>(part_.sid_); }
  24. Epoch epoch() const { return static_cast<Epoch>(part_.epoch_); }
  25. void SetEpoch(Epoch epoch) { part_.epoch_ = static_cast<u16>(epoch); }
  26. void SetIgnoreBit() { part_.ignore_accesses_ = 1; }
  27. void ClearIgnoreBit() { part_.ignore_accesses_ = 0; }
  28. bool GetIgnoreBit() const { return part_.ignore_accesses_; }
  29. private:
  30. friend class Shadow;
  31. struct Parts {
  32. u32 unused0_ : 8;
  33. u32 sid_ : 8;
  34. u32 epoch_ : kEpochBits;
  35. u32 unused1_ : 1;
  36. u32 ignore_accesses_ : 1;
  37. };
  38. union {
  39. Parts part_;
  40. u32 raw_;
  41. };
  42. };
  43. static_assert(sizeof(FastState) == kShadowSize, "bad FastState size");
  44. class Shadow {
  45. public:
  46. static constexpr RawShadow kEmpty = static_cast<RawShadow>(0);
  47. Shadow(FastState state, u32 addr, u32 size, AccessType typ) {
  48. raw_ = state.raw_;
  49. DCHECK_GT(size, 0);
  50. DCHECK_LE(size, 8);
  51. UNUSED Sid sid0 = part_.sid_;
  52. UNUSED u16 epoch0 = part_.epoch_;
  53. raw_ |= (!!(typ & kAccessAtomic) << kIsAtomicShift) |
  54. (!!(typ & kAccessRead) << kIsReadShift) |
  55. (((((1u << size) - 1) << (addr & 0x7)) & 0xff) << kAccessShift);
  56. // Note: we don't check kAccessAtomic because it overlaps with
  57. // FastState::ignore_accesses_ and it may be set spuriously.
  58. DCHECK_EQ(part_.is_read_, !!(typ & kAccessRead));
  59. DCHECK_EQ(sid(), sid0);
  60. DCHECK_EQ(epoch(), epoch0);
  61. }
  62. explicit Shadow(RawShadow x = Shadow::kEmpty) { raw_ = static_cast<u32>(x); }
  63. RawShadow raw() const { return static_cast<RawShadow>(raw_); }
  64. Sid sid() const { return part_.sid_; }
  65. Epoch epoch() const { return static_cast<Epoch>(part_.epoch_); }
  66. u8 access() const { return part_.access_; }
  67. void GetAccess(uptr *addr, uptr *size, AccessType *typ) const {
  68. DCHECK(part_.access_ != 0 || raw_ == static_cast<u32>(Shadow::kRodata));
  69. if (addr)
  70. *addr = part_.access_ ? __builtin_ffs(part_.access_) - 1 : 0;
  71. if (size)
  72. *size = part_.access_ == kFreeAccess ? kShadowCell
  73. : __builtin_popcount(part_.access_);
  74. if (typ)
  75. *typ = (part_.is_read_ ? kAccessRead : kAccessWrite) |
  76. (part_.is_atomic_ ? kAccessAtomic : 0) |
  77. (part_.access_ == kFreeAccess ? kAccessFree : 0);
  78. }
  79. ALWAYS_INLINE
  80. bool IsBothReadsOrAtomic(AccessType typ) const {
  81. u32 is_read = !!(typ & kAccessRead);
  82. u32 is_atomic = !!(typ & kAccessAtomic);
  83. bool res =
  84. raw_ & ((is_atomic << kIsAtomicShift) | (is_read << kIsReadShift));
  85. DCHECK_EQ(res,
  86. (part_.is_read_ && is_read) || (part_.is_atomic_ && is_atomic));
  87. return res;
  88. }
  89. ALWAYS_INLINE
  90. bool IsRWWeakerOrEqual(AccessType typ) const {
  91. u32 is_read = !!(typ & kAccessRead);
  92. u32 is_atomic = !!(typ & kAccessAtomic);
  93. UNUSED u32 res0 =
  94. (part_.is_atomic_ > is_atomic) ||
  95. (part_.is_atomic_ == is_atomic && part_.is_read_ >= is_read);
  96. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  97. const u32 kAtomicReadMask = (1 << kIsAtomicShift) | (1 << kIsReadShift);
  98. bool res = (raw_ & kAtomicReadMask) >=
  99. ((is_atomic << kIsAtomicShift) | (is_read << kIsReadShift));
  100. DCHECK_EQ(res, res0);
  101. return res;
  102. #else
  103. return res0;
  104. #endif
  105. }
  106. // The FreedMarker must not pass "the same access check" so that we don't
  107. // return from the race detection algorithm early.
  108. static RawShadow FreedMarker() {
  109. FastState fs;
  110. fs.SetSid(kFreeSid);
  111. fs.SetEpoch(kEpochLast);
  112. Shadow s(fs, 0, 8, kAccessWrite);
  113. return s.raw();
  114. }
  115. static RawShadow FreedInfo(Sid sid, Epoch epoch) {
  116. Shadow s;
  117. s.part_.sid_ = sid;
  118. s.part_.epoch_ = static_cast<u16>(epoch);
  119. s.part_.access_ = kFreeAccess;
  120. return s.raw();
  121. }
  122. private:
  123. struct Parts {
  124. u8 access_;
  125. Sid sid_;
  126. u16 epoch_ : kEpochBits;
  127. u16 is_read_ : 1;
  128. u16 is_atomic_ : 1;
  129. };
  130. union {
  131. Parts part_;
  132. u32 raw_;
  133. };
  134. static constexpr u8 kFreeAccess = 0x81;
  135. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  136. static constexpr uptr kAccessShift = 0;
  137. static constexpr uptr kIsReadShift = 30;
  138. static constexpr uptr kIsAtomicShift = 31;
  139. #else
  140. static constexpr uptr kAccessShift = 24;
  141. static constexpr uptr kIsReadShift = 1;
  142. static constexpr uptr kIsAtomicShift = 0;
  143. #endif
  144. public:
  145. // .rodata shadow marker, see MapRodata and ContainsSameAccessFast.
  146. static constexpr RawShadow kRodata =
  147. static_cast<RawShadow>(1 << kIsReadShift);
  148. };
  149. static_assert(sizeof(Shadow) == kShadowSize, "bad Shadow size");
  150. } // namespace __tsan
  151. #endif