1 //===-- tsan_sync.cc ------------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
10 //===----------------------------------------------------------------------===//
11 #include "sanitizer_common/sanitizer_placement_new.h"
12 #include "tsan_sync.h"
14 #include "tsan_mman.h"
18 void DDMutexInit(ThreadState
*thr
, uptr pc
, SyncVar
*s
);
21 : mtx(MutexTypeSyncVar
, StatMtxSyncVar
) {
25 void SyncVar::Init(ThreadState
*thr
, uptr pc
, uptr addr
, u64 uid
) {
30 creation_stack_id
= 0;
31 if (kCppMode
) // Go does not use them
32 creation_stack_id
= CurrentStackId(thr
, pc
);
33 if (common_flags()->detect_deadlocks
)
34 DDMutexInit(thr
, pc
, this);
37 void SyncVar::Reset(ThreadState
*thr
) {
39 creation_stack_id
= 0;
40 owner_tid
= kInvalidTid
;
49 CHECK_EQ(clock
.size(), 0);
50 CHECK_EQ(read_clock
.size(), 0);
52 clock
.Reset(&thr
->clock_cache
);
53 read_clock
.Reset(&thr
->clock_cache
);
58 atomic_store(&uid_gen_
, 0, memory_order_relaxed
);
61 void MetaMap::AllocBlock(ThreadState
*thr
, uptr pc
, uptr p
, uptr sz
) {
62 u32 idx
= block_alloc_
.Alloc(&thr
->block_cache
);
63 MBlock
*b
= block_alloc_
.Map(idx
);
66 b
->stk
= CurrentStackId(thr
, pc
);
67 u32
*meta
= MemToMeta(p
);
69 *meta
= idx
| kFlagBlock
;
72 uptr
MetaMap::FreeBlock(ThreadState
*thr
, uptr pc
, uptr p
) {
73 MBlock
* b
= GetBlock(p
);
76 uptr sz
= RoundUpTo(b
->siz
, kMetaShadowCell
);
77 FreeRange(thr
, pc
, p
, sz
);
81 bool MetaMap::FreeRange(ThreadState
*thr
, uptr pc
, uptr p
, uptr sz
) {
82 bool has_something
= false;
83 u32
*meta
= MemToMeta(p
);
84 u32
*end
= MemToMeta(p
+ sz
);
87 for (; meta
< end
; meta
++) {
90 // Note: don't write to meta in this case -- the block can be huge.
96 if (idx
& kFlagBlock
) {
97 block_alloc_
.Free(&thr
->block_cache
, idx
& ~kFlagMask
);
99 } else if (idx
& kFlagSync
) {
100 DCHECK(idx
& kFlagSync
);
101 SyncVar
*s
= sync_alloc_
.Map(idx
& ~kFlagMask
);
104 sync_alloc_
.Free(&thr
->sync_cache
, idx
& ~kFlagMask
);
111 return has_something
;
114 // ResetRange removes all meta objects from the range.
115 // It is called for large mmap-ed regions. The function is best-effort wrt
116 // freeing of meta objects, because we don't want to page in the whole range
117 // which can be huge. The function probes pages one-by-one until it finds a page
118 // without meta objects, at this point it stops freeing meta objects. Because
119 // thread stacks grow top-down, we do the same starting from end as well.
120 void MetaMap::ResetRange(ThreadState
*thr
, uptr pc
, uptr p
, uptr sz
) {
121 const uptr kMetaRatio
= kMetaShadowCell
/ kMetaShadowSize
;
122 const uptr kPageSize
= GetPageSizeCached() * kMetaRatio
;
123 if (sz
<= 4 * kPageSize
) {
124 // If the range is small, just do the normal free procedure.
125 FreeRange(thr
, pc
, p
, sz
);
128 // First, round both ends of the range to page size.
129 uptr diff
= RoundUp(p
, kPageSize
) - p
;
131 FreeRange(thr
, pc
, p
, diff
);
135 diff
= p
+ sz
- RoundDown(p
+ sz
, kPageSize
);
137 FreeRange(thr
, pc
, p
+ sz
- diff
, diff
);
140 // Now we must have a non-empty page-aligned range.
142 CHECK_EQ(p
, RoundUp(p
, kPageSize
));
143 CHECK_EQ(sz
, RoundUp(sz
, kPageSize
));
146 // Probe start of the range.
148 bool has_something
= FreeRange(thr
, pc
, p
, kPageSize
);
154 // Probe end of the range.
156 bool has_something
= FreeRange(thr
, pc
, p
- kPageSize
, kPageSize
);
161 // Finally, page out the whole range (including the parts that we've just
162 // freed). Note: we can't simply madvise, because we need to leave a zeroed
163 // range (otherwise __tsan_java_move can crash if it encounters a left-over
164 // meta objects in java heap).
165 uptr metap
= (uptr
)MemToMeta(p0
);
166 uptr metasz
= sz0
/ kMetaRatio
;
167 UnmapOrDie((void*)metap
, metasz
);
168 MmapFixedNoReserve(metap
, metasz
);
171 MBlock
* MetaMap::GetBlock(uptr p
) {
172 u32
*meta
= MemToMeta(p
);
177 if (idx
& kFlagBlock
)
178 return block_alloc_
.Map(idx
& ~kFlagMask
);
179 DCHECK(idx
& kFlagSync
);
180 SyncVar
* s
= sync_alloc_
.Map(idx
& ~kFlagMask
);
185 SyncVar
* MetaMap::GetOrCreateAndLock(ThreadState
*thr
, uptr pc
,
186 uptr addr
, bool write_lock
) {
187 return GetAndLock(thr
, pc
, addr
, write_lock
, true);
190 SyncVar
* MetaMap::GetIfExistsAndLock(uptr addr
) {
191 return GetAndLock(0, 0, addr
, true, false);
194 SyncVar
* MetaMap::GetAndLock(ThreadState
*thr
, uptr pc
,
195 uptr addr
, bool write_lock
, bool create
) {
196 u32
*meta
= MemToMeta(addr
);
205 if (idx
& kFlagBlock
)
207 DCHECK(idx
& kFlagSync
);
208 SyncVar
* s
= sync_alloc_
.Map(idx
& ~kFlagMask
);
209 if (s
->addr
== addr
) {
212 sync_alloc_
.Free(&thr
->sync_cache
, myidx
);
230 const u64 uid
= atomic_fetch_add(&uid_gen_
, 1, memory_order_relaxed
);
231 myidx
= sync_alloc_
.Alloc(&thr
->sync_cache
);
232 mys
= sync_alloc_
.Map(myidx
);
233 mys
->Init(thr
, pc
, addr
, uid
);
236 if (atomic_compare_exchange_strong((atomic_uint32_t
*)meta
, &idx0
,
237 myidx
| kFlagSync
, memory_order_release
)) {
247 void MetaMap::MoveMemory(uptr src
, uptr dst
, uptr sz
) {
248 // src and dst can overlap,
249 // there are no concurrent accesses to the regions (e.g. stop-the-world).
252 uptr diff
= dst
- src
;
253 u32
*src_meta
= MemToMeta(src
);
254 u32
*dst_meta
= MemToMeta(dst
);
255 u32
*src_meta_end
= MemToMeta(src
+ sz
);
258 src_meta
= MemToMeta(src
+ sz
) - 1;
259 dst_meta
= MemToMeta(dst
+ sz
) - 1;
260 src_meta_end
= MemToMeta(src
) - 1;
263 for (; src_meta
!= src_meta_end
; src_meta
+= inc
, dst_meta
+= inc
) {
264 CHECK_EQ(*dst_meta
, 0);
268 // Patch the addresses in sync objects.
270 if (idx
& kFlagBlock
)
272 CHECK(idx
& kFlagSync
);
273 SyncVar
*s
= sync_alloc_
.Map(idx
& ~kFlagMask
);
280 void MetaMap::OnThreadIdle(ThreadState
*thr
) {
281 block_alloc_
.FlushCache(&thr
->block_cache
);
282 sync_alloc_
.FlushCache(&thr
->sync_cache
);
285 } // namespace __tsan