tsan_sync.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //===-- tsan_sync.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. // This file is a part of ThreadSanitizer (TSan), a race detector.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef TSAN_SYNC_H
  13. #define TSAN_SYNC_H
  14. #include "sanitizer_common/sanitizer_atomic.h"
  15. #include "sanitizer_common/sanitizer_common.h"
  16. #include "sanitizer_common/sanitizer_deadlock_detector_interface.h"
  17. #include "tsan_defs.h"
  18. #include "tsan_dense_alloc.h"
  19. #include "tsan_shadow.h"
  20. #include "tsan_vector_clock.h"
  21. namespace __tsan {
  22. // These need to match __tsan_mutex_* flags defined in tsan_interface.h.
  23. // See documentation there as well.
  24. enum MutexFlags {
  25. MutexFlagLinkerInit = 1 << 0, // __tsan_mutex_linker_init
  26. MutexFlagWriteReentrant = 1 << 1, // __tsan_mutex_write_reentrant
  27. MutexFlagReadReentrant = 1 << 2, // __tsan_mutex_read_reentrant
  28. MutexFlagReadLock = 1 << 3, // __tsan_mutex_read_lock
  29. MutexFlagTryLock = 1 << 4, // __tsan_mutex_try_lock
  30. MutexFlagTryLockFailed = 1 << 5, // __tsan_mutex_try_lock_failed
  31. MutexFlagRecursiveLock = 1 << 6, // __tsan_mutex_recursive_lock
  32. MutexFlagRecursiveUnlock = 1 << 7, // __tsan_mutex_recursive_unlock
  33. MutexFlagNotStatic = 1 << 8, // __tsan_mutex_not_static
  34. // The following flags are runtime private.
  35. // Mutex API misuse was detected, so don't report any more.
  36. MutexFlagBroken = 1 << 30,
  37. // We did not intercept pre lock event, so handle it on post lock.
  38. MutexFlagDoPreLockOnPostLock = 1 << 29,
  39. // Must list all mutex creation flags.
  40. MutexCreationFlagMask = MutexFlagLinkerInit |
  41. MutexFlagWriteReentrant |
  42. MutexFlagReadReentrant |
  43. MutexFlagNotStatic,
  44. };
  45. // SyncVar is a descriptor of a user synchronization object
  46. // (mutex or an atomic variable).
  47. struct SyncVar {
  48. SyncVar();
  49. uptr addr; // overwritten by DenseSlabAlloc freelist
  50. Mutex mtx;
  51. StackID creation_stack_id;
  52. Tid owner_tid; // Set only by exclusive owners.
  53. FastState last_lock;
  54. int recursion;
  55. atomic_uint32_t flags;
  56. u32 next; // in MetaMap
  57. DDMutex dd;
  58. VectorClock *read_clock; // Used for rw mutexes only.
  59. VectorClock *clock;
  60. void Init(ThreadState *thr, uptr pc, uptr addr, bool save_stack);
  61. void Reset();
  62. bool IsFlagSet(u32 f) const {
  63. return atomic_load_relaxed(&flags) & f;
  64. }
  65. void SetFlags(u32 f) {
  66. atomic_store_relaxed(&flags, atomic_load_relaxed(&flags) | f);
  67. }
  68. void UpdateFlags(u32 flagz) {
  69. // Filter out operation flags.
  70. if (!(flagz & MutexCreationFlagMask))
  71. return;
  72. u32 current = atomic_load_relaxed(&flags);
  73. if (current & MutexCreationFlagMask)
  74. return;
  75. // Note: this can be called from MutexPostReadLock which holds only read
  76. // lock on the SyncVar.
  77. atomic_store_relaxed(&flags, current | (flagz & MutexCreationFlagMask));
  78. }
  79. };
  80. // MetaMap maps app addresses to heap block (MBlock) and sync var (SyncVar)
  81. // descriptors. It uses 1/2 direct shadow, see tsan_platform.h for the mapping.
  82. class MetaMap {
  83. public:
  84. MetaMap();
  85. void AllocBlock(ThreadState *thr, uptr pc, uptr p, uptr sz);
  86. // FreeBlock resets all sync objects in the range if reset=true and must not
  87. // run concurrently with ResetClocks which resets all sync objects
  88. // w/o any synchronization (as part of DoReset).
  89. // If we don't have a thread slot (very early/late in thread lifetime or
  90. // Go/Java callbacks) or the slot is not locked, then reset must be set to
  91. // false. In such case sync object clocks will be reset later (when it's
  92. // reused or during the next ResetClocks).
  93. uptr FreeBlock(Processor *proc, uptr p, bool reset);
  94. bool FreeRange(Processor *proc, uptr p, uptr sz, bool reset);
  95. void ResetRange(Processor *proc, uptr p, uptr sz, bool reset);
  96. // Reset vector clocks of all sync objects.
  97. // Must be called when no other threads access sync objects.
  98. void ResetClocks();
  99. MBlock* GetBlock(uptr p);
  100. SyncVar *GetSyncOrCreate(ThreadState *thr, uptr pc, uptr addr,
  101. bool save_stack) {
  102. return GetSync(thr, pc, addr, true, save_stack);
  103. }
  104. SyncVar *GetSyncIfExists(uptr addr) {
  105. return GetSync(nullptr, 0, addr, false, false);
  106. }
  107. void MoveMemory(uptr src, uptr dst, uptr sz);
  108. void OnProcIdle(Processor *proc);
  109. struct MemoryStats {
  110. uptr mem_block;
  111. uptr sync_obj;
  112. };
  113. MemoryStats GetMemoryStats() const;
  114. private:
  115. static const u32 kFlagMask = 3u << 30;
  116. static const u32 kFlagBlock = 1u << 30;
  117. static const u32 kFlagSync = 2u << 30;
  118. typedef DenseSlabAlloc<MBlock, 1 << 18, 1 << 12, kFlagMask> BlockAlloc;
  119. typedef DenseSlabAlloc<SyncVar, 1 << 20, 1 << 10, kFlagMask> SyncAlloc;
  120. BlockAlloc block_alloc_;
  121. SyncAlloc sync_alloc_;
  122. SyncVar *GetSync(ThreadState *thr, uptr pc, uptr addr, bool create,
  123. bool save_stack);
  124. };
  125. } // namespace __tsan
  126. #endif // TSAN_SYNC_H