1*7c3d14c8STreehugger Robot //===-- tsan_sync.cc ------------------------------------------------------===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot // The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is a part of ThreadSanitizer (TSan), a race detector.
11*7c3d14c8STreehugger Robot //
12*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
13*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_placement_new.h"
14*7c3d14c8STreehugger Robot #include "tsan_sync.h"
15*7c3d14c8STreehugger Robot #include "tsan_rtl.h"
16*7c3d14c8STreehugger Robot #include "tsan_mman.h"
17*7c3d14c8STreehugger Robot
18*7c3d14c8STreehugger Robot namespace __tsan {
19*7c3d14c8STreehugger Robot
20*7c3d14c8STreehugger Robot void DDMutexInit(ThreadState *thr, uptr pc, SyncVar *s);
21*7c3d14c8STreehugger Robot
SyncVar()22*7c3d14c8STreehugger Robot SyncVar::SyncVar()
23*7c3d14c8STreehugger Robot : mtx(MutexTypeSyncVar, StatMtxSyncVar) {
24*7c3d14c8STreehugger Robot Reset(0);
25*7c3d14c8STreehugger Robot }
26*7c3d14c8STreehugger Robot
Init(ThreadState * thr,uptr pc,uptr addr,u64 uid)27*7c3d14c8STreehugger Robot void SyncVar::Init(ThreadState *thr, uptr pc, uptr addr, u64 uid) {
28*7c3d14c8STreehugger Robot this->addr = addr;
29*7c3d14c8STreehugger Robot this->uid = uid;
30*7c3d14c8STreehugger Robot this->next = 0;
31*7c3d14c8STreehugger Robot
32*7c3d14c8STreehugger Robot creation_stack_id = 0;
33*7c3d14c8STreehugger Robot if (kCppMode) // Go does not use them
34*7c3d14c8STreehugger Robot creation_stack_id = CurrentStackId(thr, pc);
35*7c3d14c8STreehugger Robot if (common_flags()->detect_deadlocks)
36*7c3d14c8STreehugger Robot DDMutexInit(thr, pc, this);
37*7c3d14c8STreehugger Robot }
38*7c3d14c8STreehugger Robot
Reset(Processor * proc)39*7c3d14c8STreehugger Robot void SyncVar::Reset(Processor *proc) {
40*7c3d14c8STreehugger Robot uid = 0;
41*7c3d14c8STreehugger Robot creation_stack_id = 0;
42*7c3d14c8STreehugger Robot owner_tid = kInvalidTid;
43*7c3d14c8STreehugger Robot last_lock = 0;
44*7c3d14c8STreehugger Robot recursion = 0;
45*7c3d14c8STreehugger Robot is_rw = 0;
46*7c3d14c8STreehugger Robot is_recursive = 0;
47*7c3d14c8STreehugger Robot is_broken = 0;
48*7c3d14c8STreehugger Robot is_linker_init = 0;
49*7c3d14c8STreehugger Robot
50*7c3d14c8STreehugger Robot if (proc == 0) {
51*7c3d14c8STreehugger Robot CHECK_EQ(clock.size(), 0);
52*7c3d14c8STreehugger Robot CHECK_EQ(read_clock.size(), 0);
53*7c3d14c8STreehugger Robot } else {
54*7c3d14c8STreehugger Robot clock.Reset(&proc->clock_cache);
55*7c3d14c8STreehugger Robot read_clock.Reset(&proc->clock_cache);
56*7c3d14c8STreehugger Robot }
57*7c3d14c8STreehugger Robot }
58*7c3d14c8STreehugger Robot
MetaMap()59*7c3d14c8STreehugger Robot MetaMap::MetaMap() {
60*7c3d14c8STreehugger Robot atomic_store(&uid_gen_, 0, memory_order_relaxed);
61*7c3d14c8STreehugger Robot }
62*7c3d14c8STreehugger Robot
AllocBlock(ThreadState * thr,uptr pc,uptr p,uptr sz)63*7c3d14c8STreehugger Robot void MetaMap::AllocBlock(ThreadState *thr, uptr pc, uptr p, uptr sz) {
64*7c3d14c8STreehugger Robot u32 idx = block_alloc_.Alloc(&thr->proc()->block_cache);
65*7c3d14c8STreehugger Robot MBlock *b = block_alloc_.Map(idx);
66*7c3d14c8STreehugger Robot b->siz = sz;
67*7c3d14c8STreehugger Robot b->tid = thr->tid;
68*7c3d14c8STreehugger Robot b->stk = CurrentStackId(thr, pc);
69*7c3d14c8STreehugger Robot u32 *meta = MemToMeta(p);
70*7c3d14c8STreehugger Robot DCHECK_EQ(*meta, 0);
71*7c3d14c8STreehugger Robot *meta = idx | kFlagBlock;
72*7c3d14c8STreehugger Robot }
73*7c3d14c8STreehugger Robot
FreeBlock(Processor * proc,uptr p)74*7c3d14c8STreehugger Robot uptr MetaMap::FreeBlock(Processor *proc, uptr p) {
75*7c3d14c8STreehugger Robot MBlock* b = GetBlock(p);
76*7c3d14c8STreehugger Robot if (b == 0)
77*7c3d14c8STreehugger Robot return 0;
78*7c3d14c8STreehugger Robot uptr sz = RoundUpTo(b->siz, kMetaShadowCell);
79*7c3d14c8STreehugger Robot FreeRange(proc, p, sz);
80*7c3d14c8STreehugger Robot return sz;
81*7c3d14c8STreehugger Robot }
82*7c3d14c8STreehugger Robot
FreeRange(Processor * proc,uptr p,uptr sz)83*7c3d14c8STreehugger Robot bool MetaMap::FreeRange(Processor *proc, uptr p, uptr sz) {
84*7c3d14c8STreehugger Robot bool has_something = false;
85*7c3d14c8STreehugger Robot u32 *meta = MemToMeta(p);
86*7c3d14c8STreehugger Robot u32 *end = MemToMeta(p + sz);
87*7c3d14c8STreehugger Robot if (end == meta)
88*7c3d14c8STreehugger Robot end++;
89*7c3d14c8STreehugger Robot for (; meta < end; meta++) {
90*7c3d14c8STreehugger Robot u32 idx = *meta;
91*7c3d14c8STreehugger Robot if (idx == 0) {
92*7c3d14c8STreehugger Robot // Note: don't write to meta in this case -- the block can be huge.
93*7c3d14c8STreehugger Robot continue;
94*7c3d14c8STreehugger Robot }
95*7c3d14c8STreehugger Robot *meta = 0;
96*7c3d14c8STreehugger Robot has_something = true;
97*7c3d14c8STreehugger Robot while (idx != 0) {
98*7c3d14c8STreehugger Robot if (idx & kFlagBlock) {
99*7c3d14c8STreehugger Robot block_alloc_.Free(&proc->block_cache, idx & ~kFlagMask);
100*7c3d14c8STreehugger Robot break;
101*7c3d14c8STreehugger Robot } else if (idx & kFlagSync) {
102*7c3d14c8STreehugger Robot DCHECK(idx & kFlagSync);
103*7c3d14c8STreehugger Robot SyncVar *s = sync_alloc_.Map(idx & ~kFlagMask);
104*7c3d14c8STreehugger Robot u32 next = s->next;
105*7c3d14c8STreehugger Robot s->Reset(proc);
106*7c3d14c8STreehugger Robot sync_alloc_.Free(&proc->sync_cache, idx & ~kFlagMask);
107*7c3d14c8STreehugger Robot idx = next;
108*7c3d14c8STreehugger Robot } else {
109*7c3d14c8STreehugger Robot CHECK(0);
110*7c3d14c8STreehugger Robot }
111*7c3d14c8STreehugger Robot }
112*7c3d14c8STreehugger Robot }
113*7c3d14c8STreehugger Robot return has_something;
114*7c3d14c8STreehugger Robot }
115*7c3d14c8STreehugger Robot
116*7c3d14c8STreehugger Robot // ResetRange removes all meta objects from the range.
117*7c3d14c8STreehugger Robot // It is called for large mmap-ed regions. The function is best-effort wrt
118*7c3d14c8STreehugger Robot // freeing of meta objects, because we don't want to page in the whole range
119*7c3d14c8STreehugger Robot // which can be huge. The function probes pages one-by-one until it finds a page
120*7c3d14c8STreehugger Robot // without meta objects, at this point it stops freeing meta objects. Because
121*7c3d14c8STreehugger Robot // thread stacks grow top-down, we do the same starting from end as well.
ResetRange(Processor * proc,uptr p,uptr sz)122*7c3d14c8STreehugger Robot void MetaMap::ResetRange(Processor *proc, uptr p, uptr sz) {
123*7c3d14c8STreehugger Robot if (kGoMode) {
124*7c3d14c8STreehugger Robot // UnmapOrDie/MmapFixedNoReserve does not work on Windows,
125*7c3d14c8STreehugger Robot // so we do the optimization only for C/C++.
126*7c3d14c8STreehugger Robot FreeRange(proc, p, sz);
127*7c3d14c8STreehugger Robot return;
128*7c3d14c8STreehugger Robot }
129*7c3d14c8STreehugger Robot const uptr kMetaRatio = kMetaShadowCell / kMetaShadowSize;
130*7c3d14c8STreehugger Robot const uptr kPageSize = GetPageSizeCached() * kMetaRatio;
131*7c3d14c8STreehugger Robot if (sz <= 4 * kPageSize) {
132*7c3d14c8STreehugger Robot // If the range is small, just do the normal free procedure.
133*7c3d14c8STreehugger Robot FreeRange(proc, p, sz);
134*7c3d14c8STreehugger Robot return;
135*7c3d14c8STreehugger Robot }
136*7c3d14c8STreehugger Robot // First, round both ends of the range to page size.
137*7c3d14c8STreehugger Robot uptr diff = RoundUp(p, kPageSize) - p;
138*7c3d14c8STreehugger Robot if (diff != 0) {
139*7c3d14c8STreehugger Robot FreeRange(proc, p, diff);
140*7c3d14c8STreehugger Robot p += diff;
141*7c3d14c8STreehugger Robot sz -= diff;
142*7c3d14c8STreehugger Robot }
143*7c3d14c8STreehugger Robot diff = p + sz - RoundDown(p + sz, kPageSize);
144*7c3d14c8STreehugger Robot if (diff != 0) {
145*7c3d14c8STreehugger Robot FreeRange(proc, p + sz - diff, diff);
146*7c3d14c8STreehugger Robot sz -= diff;
147*7c3d14c8STreehugger Robot }
148*7c3d14c8STreehugger Robot // Now we must have a non-empty page-aligned range.
149*7c3d14c8STreehugger Robot CHECK_GT(sz, 0);
150*7c3d14c8STreehugger Robot CHECK_EQ(p, RoundUp(p, kPageSize));
151*7c3d14c8STreehugger Robot CHECK_EQ(sz, RoundUp(sz, kPageSize));
152*7c3d14c8STreehugger Robot const uptr p0 = p;
153*7c3d14c8STreehugger Robot const uptr sz0 = sz;
154*7c3d14c8STreehugger Robot // Probe start of the range.
155*7c3d14c8STreehugger Robot for (uptr checked = 0; sz > 0; checked += kPageSize) {
156*7c3d14c8STreehugger Robot bool has_something = FreeRange(proc, p, kPageSize);
157*7c3d14c8STreehugger Robot p += kPageSize;
158*7c3d14c8STreehugger Robot sz -= kPageSize;
159*7c3d14c8STreehugger Robot if (!has_something && checked > (128 << 10))
160*7c3d14c8STreehugger Robot break;
161*7c3d14c8STreehugger Robot }
162*7c3d14c8STreehugger Robot // Probe end of the range.
163*7c3d14c8STreehugger Robot for (uptr checked = 0; sz > 0; checked += kPageSize) {
164*7c3d14c8STreehugger Robot bool has_something = FreeRange(proc, p + sz - kPageSize, kPageSize);
165*7c3d14c8STreehugger Robot sz -= kPageSize;
166*7c3d14c8STreehugger Robot // Stacks grow down, so sync object are most likely at the end of the region
167*7c3d14c8STreehugger Robot // (if it is a stack). The very end of the stack is TLS and tsan increases
168*7c3d14c8STreehugger Robot // TLS by at least 256K, so check at least 512K.
169*7c3d14c8STreehugger Robot if (!has_something && checked > (512 << 10))
170*7c3d14c8STreehugger Robot break;
171*7c3d14c8STreehugger Robot }
172*7c3d14c8STreehugger Robot // Finally, page out the whole range (including the parts that we've just
173*7c3d14c8STreehugger Robot // freed). Note: we can't simply madvise, because we need to leave a zeroed
174*7c3d14c8STreehugger Robot // range (otherwise __tsan_java_move can crash if it encounters a left-over
175*7c3d14c8STreehugger Robot // meta objects in java heap).
176*7c3d14c8STreehugger Robot uptr metap = (uptr)MemToMeta(p0);
177*7c3d14c8STreehugger Robot uptr metasz = sz0 / kMetaRatio;
178*7c3d14c8STreehugger Robot UnmapOrDie((void*)metap, metasz);
179*7c3d14c8STreehugger Robot MmapFixedNoReserve(metap, metasz);
180*7c3d14c8STreehugger Robot }
181*7c3d14c8STreehugger Robot
GetBlock(uptr p)182*7c3d14c8STreehugger Robot MBlock* MetaMap::GetBlock(uptr p) {
183*7c3d14c8STreehugger Robot u32 *meta = MemToMeta(p);
184*7c3d14c8STreehugger Robot u32 idx = *meta;
185*7c3d14c8STreehugger Robot for (;;) {
186*7c3d14c8STreehugger Robot if (idx == 0)
187*7c3d14c8STreehugger Robot return 0;
188*7c3d14c8STreehugger Robot if (idx & kFlagBlock)
189*7c3d14c8STreehugger Robot return block_alloc_.Map(idx & ~kFlagMask);
190*7c3d14c8STreehugger Robot DCHECK(idx & kFlagSync);
191*7c3d14c8STreehugger Robot SyncVar * s = sync_alloc_.Map(idx & ~kFlagMask);
192*7c3d14c8STreehugger Robot idx = s->next;
193*7c3d14c8STreehugger Robot }
194*7c3d14c8STreehugger Robot }
195*7c3d14c8STreehugger Robot
GetOrCreateAndLock(ThreadState * thr,uptr pc,uptr addr,bool write_lock)196*7c3d14c8STreehugger Robot SyncVar* MetaMap::GetOrCreateAndLock(ThreadState *thr, uptr pc,
197*7c3d14c8STreehugger Robot uptr addr, bool write_lock) {
198*7c3d14c8STreehugger Robot return GetAndLock(thr, pc, addr, write_lock, true);
199*7c3d14c8STreehugger Robot }
200*7c3d14c8STreehugger Robot
GetIfExistsAndLock(uptr addr,bool write_lock)201*7c3d14c8STreehugger Robot SyncVar* MetaMap::GetIfExistsAndLock(uptr addr, bool write_lock) {
202*7c3d14c8STreehugger Robot return GetAndLock(0, 0, addr, write_lock, false);
203*7c3d14c8STreehugger Robot }
204*7c3d14c8STreehugger Robot
GetAndLock(ThreadState * thr,uptr pc,uptr addr,bool write_lock,bool create)205*7c3d14c8STreehugger Robot SyncVar* MetaMap::GetAndLock(ThreadState *thr, uptr pc,
206*7c3d14c8STreehugger Robot uptr addr, bool write_lock, bool create) {
207*7c3d14c8STreehugger Robot u32 *meta = MemToMeta(addr);
208*7c3d14c8STreehugger Robot u32 idx0 = *meta;
209*7c3d14c8STreehugger Robot u32 myidx = 0;
210*7c3d14c8STreehugger Robot SyncVar *mys = 0;
211*7c3d14c8STreehugger Robot for (;;) {
212*7c3d14c8STreehugger Robot u32 idx = idx0;
213*7c3d14c8STreehugger Robot for (;;) {
214*7c3d14c8STreehugger Robot if (idx == 0)
215*7c3d14c8STreehugger Robot break;
216*7c3d14c8STreehugger Robot if (idx & kFlagBlock)
217*7c3d14c8STreehugger Robot break;
218*7c3d14c8STreehugger Robot DCHECK(idx & kFlagSync);
219*7c3d14c8STreehugger Robot SyncVar * s = sync_alloc_.Map(idx & ~kFlagMask);
220*7c3d14c8STreehugger Robot if (s->addr == addr) {
221*7c3d14c8STreehugger Robot if (myidx != 0) {
222*7c3d14c8STreehugger Robot mys->Reset(thr->proc());
223*7c3d14c8STreehugger Robot sync_alloc_.Free(&thr->proc()->sync_cache, myidx);
224*7c3d14c8STreehugger Robot }
225*7c3d14c8STreehugger Robot if (write_lock)
226*7c3d14c8STreehugger Robot s->mtx.Lock();
227*7c3d14c8STreehugger Robot else
228*7c3d14c8STreehugger Robot s->mtx.ReadLock();
229*7c3d14c8STreehugger Robot return s;
230*7c3d14c8STreehugger Robot }
231*7c3d14c8STreehugger Robot idx = s->next;
232*7c3d14c8STreehugger Robot }
233*7c3d14c8STreehugger Robot if (!create)
234*7c3d14c8STreehugger Robot return 0;
235*7c3d14c8STreehugger Robot if (*meta != idx0) {
236*7c3d14c8STreehugger Robot idx0 = *meta;
237*7c3d14c8STreehugger Robot continue;
238*7c3d14c8STreehugger Robot }
239*7c3d14c8STreehugger Robot
240*7c3d14c8STreehugger Robot if (myidx == 0) {
241*7c3d14c8STreehugger Robot const u64 uid = atomic_fetch_add(&uid_gen_, 1, memory_order_relaxed);
242*7c3d14c8STreehugger Robot myidx = sync_alloc_.Alloc(&thr->proc()->sync_cache);
243*7c3d14c8STreehugger Robot mys = sync_alloc_.Map(myidx);
244*7c3d14c8STreehugger Robot mys->Init(thr, pc, addr, uid);
245*7c3d14c8STreehugger Robot }
246*7c3d14c8STreehugger Robot mys->next = idx0;
247*7c3d14c8STreehugger Robot if (atomic_compare_exchange_strong((atomic_uint32_t*)meta, &idx0,
248*7c3d14c8STreehugger Robot myidx | kFlagSync, memory_order_release)) {
249*7c3d14c8STreehugger Robot if (write_lock)
250*7c3d14c8STreehugger Robot mys->mtx.Lock();
251*7c3d14c8STreehugger Robot else
252*7c3d14c8STreehugger Robot mys->mtx.ReadLock();
253*7c3d14c8STreehugger Robot return mys;
254*7c3d14c8STreehugger Robot }
255*7c3d14c8STreehugger Robot }
256*7c3d14c8STreehugger Robot }
257*7c3d14c8STreehugger Robot
MoveMemory(uptr src,uptr dst,uptr sz)258*7c3d14c8STreehugger Robot void MetaMap::MoveMemory(uptr src, uptr dst, uptr sz) {
259*7c3d14c8STreehugger Robot // src and dst can overlap,
260*7c3d14c8STreehugger Robot // there are no concurrent accesses to the regions (e.g. stop-the-world).
261*7c3d14c8STreehugger Robot CHECK_NE(src, dst);
262*7c3d14c8STreehugger Robot CHECK_NE(sz, 0);
263*7c3d14c8STreehugger Robot uptr diff = dst - src;
264*7c3d14c8STreehugger Robot u32 *src_meta = MemToMeta(src);
265*7c3d14c8STreehugger Robot u32 *dst_meta = MemToMeta(dst);
266*7c3d14c8STreehugger Robot u32 *src_meta_end = MemToMeta(src + sz);
267*7c3d14c8STreehugger Robot uptr inc = 1;
268*7c3d14c8STreehugger Robot if (dst > src) {
269*7c3d14c8STreehugger Robot src_meta = MemToMeta(src + sz) - 1;
270*7c3d14c8STreehugger Robot dst_meta = MemToMeta(dst + sz) - 1;
271*7c3d14c8STreehugger Robot src_meta_end = MemToMeta(src) - 1;
272*7c3d14c8STreehugger Robot inc = -1;
273*7c3d14c8STreehugger Robot }
274*7c3d14c8STreehugger Robot for (; src_meta != src_meta_end; src_meta += inc, dst_meta += inc) {
275*7c3d14c8STreehugger Robot CHECK_EQ(*dst_meta, 0);
276*7c3d14c8STreehugger Robot u32 idx = *src_meta;
277*7c3d14c8STreehugger Robot *src_meta = 0;
278*7c3d14c8STreehugger Robot *dst_meta = idx;
279*7c3d14c8STreehugger Robot // Patch the addresses in sync objects.
280*7c3d14c8STreehugger Robot while (idx != 0) {
281*7c3d14c8STreehugger Robot if (idx & kFlagBlock)
282*7c3d14c8STreehugger Robot break;
283*7c3d14c8STreehugger Robot CHECK(idx & kFlagSync);
284*7c3d14c8STreehugger Robot SyncVar *s = sync_alloc_.Map(idx & ~kFlagMask);
285*7c3d14c8STreehugger Robot s->addr += diff;
286*7c3d14c8STreehugger Robot idx = s->next;
287*7c3d14c8STreehugger Robot }
288*7c3d14c8STreehugger Robot }
289*7c3d14c8STreehugger Robot }
290*7c3d14c8STreehugger Robot
OnProcIdle(Processor * proc)291*7c3d14c8STreehugger Robot void MetaMap::OnProcIdle(Processor *proc) {
292*7c3d14c8STreehugger Robot block_alloc_.FlushCache(&proc->block_cache);
293*7c3d14c8STreehugger Robot sync_alloc_.FlushCache(&proc->sync_cache);
294*7c3d14c8STreehugger Robot }
295*7c3d14c8STreehugger Robot
296*7c3d14c8STreehugger Robot } // namespace __tsan
297