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 (!SANITIZER_GO
) // 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(Processor
*proc
) {
39 creation_stack_id
= 0;
40 owner_tid
= kInvalidTid
;
43 atomic_store_relaxed(&flags
, 0);
46 CHECK_EQ(clock
.size(), 0);
47 CHECK_EQ(read_clock
.size(), 0);
49 clock
.Reset(&proc
->clock_cache
);
50 read_clock
.Reset(&proc
->clock_cache
);
55 : block_alloc_("heap block allocator")
56 , sync_alloc_("sync allocator") {
57 atomic_store(&uid_gen_
, 0, memory_order_relaxed
);
60 void MetaMap::AllocBlock(ThreadState
*thr
, uptr pc
, uptr p
, uptr sz
) {
61 u32 idx
= block_alloc_
.Alloc(&thr
->proc()->block_cache
);
62 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(Processor
*proc
, uptr p
) {
73 MBlock
* b
= GetBlock(p
);
76 uptr sz
= RoundUpTo(b
->siz
, kMetaShadowCell
);
77 FreeRange(proc
, p
, sz
);
81 bool MetaMap::FreeRange(Processor
*proc
, 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(&proc
->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(&proc
->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(Processor
*proc
, uptr p
, uptr sz
) {
122 // UnmapOrDie/MmapFixedNoReserve does not work on Windows,
123 // so we do the optimization only for C/C++.
124 FreeRange(proc
, p
, sz
);
127 const uptr kMetaRatio
= kMetaShadowCell
/ kMetaShadowSize
;
128 const uptr kPageSize
= GetPageSizeCached() * kMetaRatio
;
129 if (sz
<= 4 * kPageSize
) {
130 // If the range is small, just do the normal free procedure.
131 FreeRange(proc
, p
, sz
);
134 // First, round both ends of the range to page size.
135 uptr diff
= RoundUp(p
, kPageSize
) - p
;
137 FreeRange(proc
, p
, diff
);
141 diff
= p
+ sz
- RoundDown(p
+ sz
, kPageSize
);
143 FreeRange(proc
, p
+ sz
- diff
, diff
);
146 // Now we must have a non-empty page-aligned range.
148 CHECK_EQ(p
, RoundUp(p
, kPageSize
));
149 CHECK_EQ(sz
, RoundUp(sz
, kPageSize
));
152 // Probe start of the range.
153 for (uptr checked
= 0; sz
> 0; checked
+= kPageSize
) {
154 bool has_something
= FreeRange(proc
, p
, kPageSize
);
157 if (!has_something
&& checked
> (128 << 10))
160 // Probe end of the range.
161 for (uptr checked
= 0; sz
> 0; checked
+= kPageSize
) {
162 bool has_something
= FreeRange(proc
, p
+ sz
- kPageSize
, kPageSize
);
164 // Stacks grow down, so sync object are most likely at the end of the region
165 // (if it is a stack). The very end of the stack is TLS and tsan increases
166 // TLS by at least 256K, so check at least 512K.
167 if (!has_something
&& checked
> (512 << 10))
170 // Finally, page out the whole range (including the parts that we've just
171 // freed). Note: we can't simply madvise, because we need to leave a zeroed
172 // range (otherwise __tsan_java_move can crash if it encounters a left-over
173 // meta objects in java heap).
174 uptr metap
= (uptr
)MemToMeta(p0
);
175 uptr metasz
= sz0
/ kMetaRatio
;
176 UnmapOrDie((void*)metap
, metasz
);
177 MmapFixedNoReserve(metap
, metasz
);
180 MBlock
* MetaMap::GetBlock(uptr p
) {
181 u32
*meta
= MemToMeta(p
);
186 if (idx
& kFlagBlock
)
187 return block_alloc_
.Map(idx
& ~kFlagMask
);
188 DCHECK(idx
& kFlagSync
);
189 SyncVar
* s
= sync_alloc_
.Map(idx
& ~kFlagMask
);
194 SyncVar
* MetaMap::GetOrCreateAndLock(ThreadState
*thr
, uptr pc
,
195 uptr addr
, bool write_lock
) {
196 return GetAndLock(thr
, pc
, addr
, write_lock
, true);
199 SyncVar
* MetaMap::GetIfExistsAndLock(uptr addr
, bool write_lock
) {
200 return GetAndLock(0, 0, addr
, write_lock
, false);
203 SyncVar
* MetaMap::GetAndLock(ThreadState
*thr
, uptr pc
,
204 uptr addr
, bool write_lock
, bool create
) {
205 u32
*meta
= MemToMeta(addr
);
214 if (idx
& kFlagBlock
)
216 DCHECK(idx
& kFlagSync
);
217 SyncVar
* s
= sync_alloc_
.Map(idx
& ~kFlagMask
);
218 if (s
->addr
== addr
) {
220 mys
->Reset(thr
->proc());
221 sync_alloc_
.Free(&thr
->proc()->sync_cache
, myidx
);
239 const u64 uid
= atomic_fetch_add(&uid_gen_
, 1, memory_order_relaxed
);
240 myidx
= sync_alloc_
.Alloc(&thr
->proc()->sync_cache
);
241 mys
= sync_alloc_
.Map(myidx
);
242 mys
->Init(thr
, pc
, addr
, uid
);
245 if (atomic_compare_exchange_strong((atomic_uint32_t
*)meta
, &idx0
,
246 myidx
| kFlagSync
, memory_order_release
)) {
256 void MetaMap::MoveMemory(uptr src
, uptr dst
, uptr sz
) {
257 // src and dst can overlap,
258 // there are no concurrent accesses to the regions (e.g. stop-the-world).
261 uptr diff
= dst
- src
;
262 u32
*src_meta
= MemToMeta(src
);
263 u32
*dst_meta
= MemToMeta(dst
);
264 u32
*src_meta_end
= MemToMeta(src
+ sz
);
267 src_meta
= MemToMeta(src
+ sz
) - 1;
268 dst_meta
= MemToMeta(dst
+ sz
) - 1;
269 src_meta_end
= MemToMeta(src
) - 1;
272 for (; src_meta
!= src_meta_end
; src_meta
+= inc
, dst_meta
+= inc
) {
273 CHECK_EQ(*dst_meta
, 0);
277 // Patch the addresses in sync objects.
279 if (idx
& kFlagBlock
)
281 CHECK(idx
& kFlagSync
);
282 SyncVar
*s
= sync_alloc_
.Map(idx
& ~kFlagMask
);
289 void MetaMap::OnProcIdle(Processor
*proc
) {
290 block_alloc_
.FlushCache(&proc
->block_cache
);
291 sync_alloc_
.FlushCache(&proc
->sync_cache
);
294 } // namespace __tsan