blob: f07ea3b9776bccfbcdb551604827b0503352f27f [file] [log] [blame]
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001//===-- 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 Vyukovfce5bd42012-06-29 16:58:33 +000016#include "sanitizer_common/sanitizer_atomic.h"
17#include "sanitizer_common/sanitizer_common.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070018#include "sanitizer_common/sanitizer_deadlock_detector_interface.h"
Kostya Serebryany7ac41482012-05-10 13:48:04 +000019#include "tsan_defs.h"
Stephen Hines6a211c52014-07-21 00:49:56 -070020#include "tsan_clock.h"
Kostya Serebryany7ac41482012-05-10 13:48:04 +000021#include "tsan_mutex.h"
Stephen Hines6a211c52014-07-21 00:49:56 -070022#include "tsan_dense_alloc.h"
Kostya Serebryany7ac41482012-05-10 13:48:04 +000023
24namespace __tsan {
25
Kostya Serebryany7ac41482012-05-10 13:48:04 +000026struct SyncVar {
Stephen Hines6a211c52014-07-21 00:49:56 -070027 SyncVar();
Kostya Serebryany7ac41482012-05-10 13:48:04 +000028
29 static const int kInvalidTid = -1;
30
Stephen Hines6a211c52014-07-21 00:49:56 -070031 uptr addr; // overwritten by DenseSlabAlloc freelist
Kostya Serebryany7ac41482012-05-10 13:48:04 +000032 Mutex mtx;
Stephen Hines6a211c52014-07-21 00:49:56 -070033 u64 uid; // Globally unique id.
Dmitry Vyukov3abf5312013-03-18 08:27:47 +000034 u32 creation_stack_id;
Kostya Serebryany7ac41482012-05-10 13:48:04 +000035 int owner_tid; // Set only by exclusive owners.
Dmitry Vyukov332c62b2012-08-16 15:08:49 +000036 u64 last_lock;
Kostya Serebryany7ac41482012-05-10 13:48:04 +000037 int recursion;
38 bool is_rw;
39 bool is_recursive;
40 bool is_broken;
Dmitry Vyukov2e933fc2012-08-16 14:21:09 +000041 bool is_linker_init;
Stephen Hines6a211c52014-07-21 00:49:56 -070042 u32 next; // in MetaMap
Stephen Hines2d1fdb22014-05-28 23:58:16 -070043 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 Vyukov26127732012-05-22 11:33:03 +000048
Stephen Hines6a211c52014-07-21 00:49:56 -070049 void Init(ThreadState *thr, uptr pc, uptr addr, u64 uid);
Stephen Hines6d186232014-11-26 17:56:19 -080050 void Reset(ThreadState *thr);
Stephen Hines6a211c52014-07-21 00:49:56 -070051
Dmitry Vyukovad9da372012-12-06 12:16:15 +000052 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 Serebryany7ac41482012-05-10 13:48:04 +000064};
65
Stephen Hines6a211c52014-07-21 00:49:56 -070066/* 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*/
70class MetaMap {
Kostya Serebryany7ac41482012-05-10 13:48:04 +000071 public:
Stephen Hines6a211c52014-07-21 00:49:56 -070072 MetaMap();
73
74 void AllocBlock(ThreadState *thr, uptr pc, uptr p, uptr sz);
75 uptr FreeBlock(ThreadState *thr, uptr pc, uptr p);
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070076 bool FreeRange(ThreadState *thr, uptr pc, uptr p, uptr sz);
77 void ResetRange(ThreadState *thr, uptr pc, uptr p, uptr sz);
Stephen Hines6a211c52014-07-21 00:49:56 -070078 MBlock* GetBlock(uptr p);
Kostya Serebryany7ac41482012-05-10 13:48:04 +000079
Dmitry Vyukovad9da372012-12-06 12:16:15 +000080 SyncVar* GetOrCreateAndLock(ThreadState *thr, uptr pc,
81 uptr addr, bool write_lock);
Stephen Hines6a211c52014-07-21 00:49:56 -070082 SyncVar* GetIfExistsAndLock(uptr addr);
Kostya Serebryany7ac41482012-05-10 13:48:04 +000083
Stephen Hines6a211c52014-07-21 00:49:56 -070084 void MoveMemory(uptr src, uptr dst, uptr sz);
Kostya Serebryany7ac41482012-05-10 13:48:04 +000085
Stephen Hines6a211c52014-07-21 00:49:56 -070086 void OnThreadIdle(ThreadState *thr);
Dmitry Vyukov21cc85d2012-12-20 17:29:34 +000087
Kostya Serebryany7ac41482012-05-10 13:48:04 +000088 private:
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080089 static const u32 kFlagMask = 3u << 30;
90 static const u32 kFlagBlock = 1u << 30;
91 static const u32 kFlagSync = 2u << 30;
Stephen Hines6a211c52014-07-21 00:49:56 -070092 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 Vyukovad9da372012-12-06 12:16:15 +000096 atomic_uint64_t uid_gen_;
Kostya Serebryany7ac41482012-05-10 13:48:04 +000097
Stephen Hines6a211c52014-07-21 00:49:56 -070098 SyncVar* GetAndLock(ThreadState *thr, uptr pc, uptr addr, bool write_lock,
99 bool create);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000100};
101
102} // namespace __tsan
103
104#endif // TSAN_SYNC_H