Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 1 | //===-- tsan_sync.h ---------------------------------------------*- 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 is a part of ThreadSanitizer (TSan), a race detector. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #ifndef TSAN_SYNC_H |
| 14 | #define TSAN_SYNC_H |
| 15 | |
Dmitry Vyukov | fce5bd4 | 2012-06-29 16:58:33 +0000 | [diff] [blame] | 16 | #include "sanitizer_common/sanitizer_atomic.h" |
| 17 | #include "sanitizer_common/sanitizer_common.h" |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_deadlock_detector_interface.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 19 | #include "tsan_defs.h" |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 20 | #include "tsan_clock.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 21 | #include "tsan_mutex.h" |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 22 | #include "tsan_dense_alloc.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 23 | |
| 24 | namespace __tsan { |
| 25 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 26 | struct SyncVar { |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 27 | SyncVar(); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 28 | |
| 29 | static const int kInvalidTid = -1; |
| 30 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 31 | uptr addr; // overwritten by DenseSlabAlloc freelist |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 32 | Mutex mtx; |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 33 | u64 uid; // Globally unique id. |
Dmitry Vyukov | 3abf531 | 2013-03-18 08:27:47 +0000 | [diff] [blame] | 34 | u32 creation_stack_id; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 35 | int owner_tid; // Set only by exclusive owners. |
Dmitry Vyukov | 332c62b | 2012-08-16 15:08:49 +0000 | [diff] [blame] | 36 | u64 last_lock; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 37 | int recursion; |
| 38 | bool is_rw; |
| 39 | bool is_recursive; |
| 40 | bool is_broken; |
Dmitry Vyukov | 2e933fc | 2012-08-16 14:21:09 +0000 | [diff] [blame] | 41 | bool is_linker_init; |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 42 | u32 next; // in MetaMap |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 43 | DDMutex dd; |
| 44 | SyncClock read_clock; // Used for rw mutexes only. |
| 45 | // The clock is placed last, so that it is situated on a different cache line |
| 46 | // with the mtx. This reduces contention for hot sync objects. |
| 47 | SyncClock clock; |
Dmitry Vyukov | 2612773 | 2012-05-22 11:33:03 +0000 | [diff] [blame] | 48 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 49 | void Init(ThreadState *thr, uptr pc, uptr addr, u64 uid); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 50 | void Reset(ThreadState *thr); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 51 | |
Dmitry Vyukov | ad9da37 | 2012-12-06 12:16:15 +0000 | [diff] [blame] | 52 | u64 GetId() const { |
| 53 | // 47 lsb is addr, then 14 bits is low part of uid, then 3 zero bits. |
| 54 | return GetLsb((u64)addr | (uid << 47), 61); |
| 55 | } |
| 56 | bool CheckId(u64 uid) const { |
| 57 | CHECK_EQ(uid, GetLsb(uid, 14)); |
| 58 | return GetLsb(this->uid, 14) == uid; |
| 59 | } |
| 60 | static uptr SplitId(u64 id, u64 *uid) { |
| 61 | *uid = id >> 47; |
| 62 | return (uptr)GetLsb(id, 47); |
| 63 | } |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 66 | /* MetaMap allows to map arbitrary user pointers onto various descriptors. |
| 67 | Currently it maps pointers to heap block descriptors and sync var descs. |
| 68 | It uses 1/2 direct shadow, see tsan_platform.h. |
| 69 | */ |
| 70 | class MetaMap { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 71 | public: |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 72 | MetaMap(); |
| 73 | |
| 74 | void AllocBlock(ThreadState *thr, uptr pc, uptr p, uptr sz); |
| 75 | uptr FreeBlock(ThreadState *thr, uptr pc, uptr p); |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 76 | bool FreeRange(ThreadState *thr, uptr pc, uptr p, uptr sz); |
| 77 | void ResetRange(ThreadState *thr, uptr pc, uptr p, uptr sz); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 78 | MBlock* GetBlock(uptr p); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 79 | |
Dmitry Vyukov | ad9da37 | 2012-12-06 12:16:15 +0000 | [diff] [blame] | 80 | SyncVar* GetOrCreateAndLock(ThreadState *thr, uptr pc, |
| 81 | uptr addr, bool write_lock); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 82 | SyncVar* GetIfExistsAndLock(uptr addr); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 83 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 84 | void MoveMemory(uptr src, uptr dst, uptr sz); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 85 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 86 | void OnThreadIdle(ThreadState *thr); |
Dmitry Vyukov | 21cc85d | 2012-12-20 17:29:34 +0000 | [diff] [blame] | 87 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 88 | private: |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 89 | static const u32 kFlagMask = 3 << 30; |
| 90 | static const u32 kFlagBlock = 1 << 30; |
| 91 | static const u32 kFlagSync = 2 << 30; |
| 92 | typedef DenseSlabAlloc<MBlock, 1<<16, 1<<12> BlockAlloc; |
| 93 | typedef DenseSlabAlloc<SyncVar, 1<<16, 1<<10> SyncAlloc; |
| 94 | BlockAlloc block_alloc_; |
| 95 | SyncAlloc sync_alloc_; |
Dmitry Vyukov | ad9da37 | 2012-12-06 12:16:15 +0000 | [diff] [blame] | 96 | atomic_uint64_t uid_gen_; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 97 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 98 | SyncVar* GetAndLock(ThreadState *thr, uptr pc, uptr addr, bool write_lock, |
| 99 | bool create); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | } // namespace __tsan |
| 103 | |
| 104 | #endif // TSAN_SYNC_H |