blob: e93a7a34ee4d8a1ea7706f0fa349cd2ee1add42d [file] [log] [blame]
Alexey Samsonov3b2f9f42012-06-04 13:55:19 +00001//===-- tsan_rtl_thread.cc ------------------------------------------------===//
Kostya Serebryany4ad375f2012-05-10 13:48:04 +00002//
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
Alexey Samsonov8bd90982012-06-07 09:50:16 +000014#include "sanitizer_common/sanitizer_placement_new.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000015#include "tsan_rtl.h"
16#include "tsan_mman.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000017#include "tsan_platform.h"
18#include "tsan_report.h"
19#include "tsan_sync.h"
20
21namespace __tsan {
22
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000023// ThreadContext implementation.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000024
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000025ThreadContext::ThreadContext(int tid)
26 : ThreadContextBase(tid)
27 , thr()
28 , sync()
29 , epoch0()
Dmitry Vyukov79915de2013-03-20 10:31:53 +000030 , epoch1() {
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000031}
32
Dmitry Vyukov49e462f2013-03-18 10:10:15 +000033#ifndef TSAN_GO
34ThreadContext::~ThreadContext() {
35}
36#endif
37
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000038void ThreadContext::OnDead() {
39 sync.Reset();
40}
41
42void ThreadContext::OnJoined(void *arg) {
43 ThreadState *caller_thr = static_cast<ThreadState *>(arg);
44 caller_thr->clock.acquire(&sync);
45 StatInc(caller_thr, StatSyncAcquire);
Dmitry Vyukov509dab32013-03-19 10:22:33 +000046 sync.Reset();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000047}
48
49struct OnCreatedArgs {
50 ThreadState *thr;
51 uptr pc;
52};
53
54void ThreadContext::OnCreated(void *arg) {
55 thr = 0;
Dmitry Vyukov50160032013-03-18 08:52:46 +000056 if (tid == 0)
57 return;
58 OnCreatedArgs *args = static_cast<OnCreatedArgs *>(arg);
59 args->thr->fast_state.IncrementEpoch();
60 // Can't increment epoch w/o writing to the trace as well.
61 TraceAddEvent(args->thr, args->thr->fast_state, EventTypeMop, 0);
62 args->thr->clock.set(args->thr->tid, args->thr->fast_state.epoch());
63 args->thr->fast_synch_epoch = args->thr->fast_state.epoch();
64 args->thr->clock.release(&sync);
65 StatInc(args->thr, StatSyncRelease);
Dmitry Vyukov7cd20252013-03-18 09:02:27 +000066#ifdef TSAN_GO
Dmitry Vyukov50160032013-03-18 08:52:46 +000067 creation_stack.ObtainCurrent(args->thr, args->pc);
Dmitry Vyukov7cd20252013-03-18 09:02:27 +000068#else
69 creation_stack_id = CurrentStackId(args->thr, args->pc);
70#endif
Dmitry Vyukov50160032013-03-18 08:52:46 +000071 if (reuse_count == 0)
72 StatInc(args->thr, StatThreadMaxTid);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000073}
74
Dmitry Vyukov4ecfa692013-03-19 12:25:48 +000075void ThreadContext::OnReset() {
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000076 sync.Reset();
Dmitry Vyukov79915de2013-03-20 10:31:53 +000077 FlushUnneededShadowMemory(GetThreadTrace(tid), TraceSize() * sizeof(Event));
78 //!!! FlushUnneededShadowMemory(GetThreadTraceHeader(tid), sizeof(Trace));
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000079}
80
81struct OnStartedArgs {
82 ThreadState *thr;
83 uptr stk_addr;
84 uptr stk_size;
85 uptr tls_addr;
86 uptr tls_size;
87};
88
89void ThreadContext::OnStarted(void *arg) {
90 OnStartedArgs *args = static_cast<OnStartedArgs*>(arg);
Dmitry Vyukov50160032013-03-18 08:52:46 +000091 thr = args->thr;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000092 // RoundUp so that one trace part does not contain events
93 // from different threads.
94 epoch0 = RoundUp(epoch1 + 1, kTracePartSize);
95 epoch1 = (u64)-1;
Dmitry Vyukov50160032013-03-18 08:52:46 +000096 new(thr) ThreadState(CTX(), tid, unique_id,
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000097 epoch0, args->stk_addr, args->stk_size, args->tls_addr, args->tls_size);
98#ifdef TSAN_GO
99 // Setup dynamic shadow stack.
100 const int kInitStackSize = 8;
101 args->thr->shadow_stack = (uptr*)internal_alloc(MBlockShadowStack,
102 kInitStackSize * sizeof(uptr));
103 args->thr->shadow_stack_pos = thr->shadow_stack;
104 args->thr->shadow_stack_end = thr->shadow_stack + kInitStackSize;
105#endif
106#ifndef TSAN_GO
107 AllocatorThreadStart(args->thr);
108#endif
109 thr = args->thr;
110 thr->fast_synch_epoch = epoch0;
111 thr->clock.set(tid, epoch0);
112 thr->clock.acquire(&sync);
113 thr->fast_state.SetHistorySize(flags()->history_size);
114 const uptr trace = (epoch0 / kTracePartSize) % TraceParts();
Dmitry Vyukov79915de2013-03-20 10:31:53 +0000115 Trace *thr_trace = ThreadTrace(thr->tid);
116 thr_trace->headers[trace].epoch0 = epoch0;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000117 StatInc(thr, StatSyncAcquire);
Dmitry Vyukov79915de2013-03-20 10:31:53 +0000118 sync.Reset();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000119 DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx "
120 "tls_addr=%zx tls_size=%zx\n",
Alexey Samsonovb5d10f62013-03-18 09:45:22 +0000121 tid, (uptr)epoch0, args->stk_addr, args->stk_size,
122 args->tls_addr, args->tls_size);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000123 thr->is_alive = true;
124}
125
126void ThreadContext::OnFinished() {
127 if (!detached) {
128 thr->fast_state.IncrementEpoch();
129 // Can't increment epoch w/o writing to the trace as well.
130 TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
131 thr->clock.set(thr->tid, thr->fast_state.epoch());
132 thr->fast_synch_epoch = thr->fast_state.epoch();
133 thr->clock.release(&sync);
134 StatInc(thr, StatSyncRelease);
135 }
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000136 epoch1 = thr->fast_state.epoch();
137
138#ifndef TSAN_GO
139 AllocatorThreadFinish(thr);
140#endif
141 thr->~ThreadState();
142 StatAggregate(CTX()->stat, thr->stat);
143 thr = 0;
144}
145
146static void MaybeReportThreadLeak(ThreadContextBase *tctx_base, void *unused) {
147 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000148 if (tctx->detached)
149 return;
150 if (tctx->status != ThreadStatusCreated
151 && tctx->status != ThreadStatusRunning
152 && tctx->status != ThreadStatusFinished)
153 return;
154 ScopedReport rep(ReportTypeThreadLeak);
155 rep.AddThread(tctx);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000156 OutputReport(CTX(), rep);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000157}
158
159void ThreadFinalize(ThreadState *thr) {
160 CHECK_GT(thr->in_rtl, 0);
161 if (!flags()->report_thread_leaks)
162 return;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000163 ThreadRegistryLock l(CTX()->thread_registry);
164 CTX()->thread_registry->RunCallbackForEachThreadLocked(
165 MaybeReportThreadLeak, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000166}
167
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000168int ThreadCount(ThreadState *thr) {
169 CHECK_GT(thr->in_rtl, 0);
170 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000171 uptr result;
172 ctx->thread_registry->GetNumberOfThreads(0, 0, &result);
173 return (int)result;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000174}
175
176int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
177 CHECK_GT(thr->in_rtl, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000178 StatInc(thr, StatThreadCreate);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000179 Context *ctx = CTX();
180 OnCreatedArgs args = { thr, pc };
181 int tid = ctx->thread_registry->CreateThread(uid, detached, thr->tid, &args);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000182 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000183 StatSet(thr, StatThreadMaxAlive, ctx->thread_registry->GetMaxAliveThreads());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000184 return tid;
185}
186
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000187void ThreadStart(ThreadState *thr, int tid, uptr os_id) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000188 CHECK_GT(thr->in_rtl, 0);
189 uptr stk_addr = 0;
190 uptr stk_size = 0;
191 uptr tls_addr = 0;
192 uptr tls_size = 0;
Dmitry Vyukov7339eb12012-05-25 11:15:04 +0000193 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000194
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000195 if (tid) {
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000196 if (stk_addr && stk_size)
Dmitry Vyukovce26a0a2013-03-18 16:56:48 +0000197 MemoryRangeImitateWrite(thr, /*pc=*/ 1, stk_addr, stk_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000198
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000199 if (tls_addr && tls_size) {
200 // Check that the thr object is in tls;
201 const uptr thr_beg = (uptr)thr;
202 const uptr thr_end = (uptr)thr + sizeof(*thr);
203 CHECK_GE(thr_beg, tls_addr);
204 CHECK_LE(thr_beg, tls_addr + tls_size);
205 CHECK_GE(thr_end, tls_addr);
206 CHECK_LE(thr_end, tls_addr + tls_size);
207 // Since the thr object is huge, skip it.
Dmitry Vyukovce26a0a2013-03-18 16:56:48 +0000208 MemoryRangeImitateWrite(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
209 MemoryRangeImitateWrite(thr, /*pc=*/ 2,
210 thr_end, tls_addr + tls_size - thr_end);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000211 }
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000212 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000213
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000214 OnStartedArgs args = { thr, stk_addr, stk_size, tls_addr, tls_size };
215 CTX()->thread_registry->StartThread(tid, os_id, &args);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000216}
217
218void ThreadFinish(ThreadState *thr) {
219 CHECK_GT(thr->in_rtl, 0);
220 StatInc(thr, StatThreadFinish);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000221 if (thr->stk_addr && thr->stk_size)
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000222 DontNeedShadowFor(thr->stk_addr, thr->stk_size);
223 if (thr->tls_addr && thr->tls_size)
224 DontNeedShadowFor(thr->tls_addr, thr->tls_size);
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000225 thr->is_alive = false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000226 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000227 ctx->thread_registry->FinishThread(thr->tid);
228}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000229
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000230static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) {
231 uptr uid = (uptr)arg;
232 if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) {
233 tctx->user_id = 0;
234 return true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000235 }
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000236 return false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000237}
238
239int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
240 CHECK_GT(thr->in_rtl, 0);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000241 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000242 int res = ctx->thread_registry->FindThread(FindThreadByUid, (void*)uid);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000243 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000244 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000245}
246
247void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
248 CHECK_GT(thr->in_rtl, 0);
249 CHECK_GT(tid, 0);
250 CHECK_LT(tid, kMaxTid);
251 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
252 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000253 ctx->thread_registry->JoinThread(tid, thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000254}
255
256void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
257 CHECK_GT(thr->in_rtl, 0);
258 CHECK_GT(tid, 0);
259 CHECK_LT(tid, kMaxTid);
260 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000261 ctx->thread_registry->DetachThread(tid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000262}
263
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000264void ThreadSetName(ThreadState *thr, const char *name) {
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000265 CHECK_GT(thr->in_rtl, 0);
266 CTX()->thread_registry->SetThreadName(thr->tid, name);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000267}
268
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000269void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
270 uptr size, bool is_write) {
271 if (size == 0)
272 return;
273
274 u64 *shadow_mem = (u64*)MemToShadow(addr);
275 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
276 thr->tid, (void*)pc, (void*)addr,
277 (int)size, is_write);
278
279#if TSAN_DEBUG
280 if (!IsAppMem(addr)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000281 Printf("Access to non app mem %zx\n", addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000282 DCHECK(IsAppMem(addr));
283 }
284 if (!IsAppMem(addr + size - 1)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000285 Printf("Access to non app mem %zx\n", addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000286 DCHECK(IsAppMem(addr + size - 1));
287 }
288 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000289 Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000290 DCHECK(IsShadowMem((uptr)shadow_mem));
291 }
292 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000293 Printf("Bad shadow addr %p (%zx)\n",
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000294 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000295 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
296 }
297#endif
298
299 StatInc(thr, StatMopRange);
300
Dmitry Vyukovb62c1582013-03-20 13:21:50 +0000301 if (*shadow_mem == kShadowRodata) {
302 // Access to .rodata section, no races here.
303 // Measurements show that it can be 10-20% of all memory accesses.
304 StatInc(thr, StatMopRangeRodata);
305 return;
306 }
307
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000308 FastState fast_state = thr->fast_state;
309 if (fast_state.GetIgnoreBit())
310 return;
311
312 fast_state.IncrementEpoch();
313 thr->fast_state = fast_state;
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000314 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000315
316 bool unaligned = (addr % kShadowCell) != 0;
317
318 // Handle unaligned beginning, if any.
319 for (; addr % kShadowCell && size; addr++, size--) {
320 int const kAccessSizeLog = 0;
321 Shadow cur(fast_state);
322 cur.SetWrite(is_write);
323 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000324 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000325 shadow_mem, cur);
326 }
327 if (unaligned)
328 shadow_mem += kShadowCnt;
329 // Handle middle part, if any.
330 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
331 int const kAccessSizeLog = 3;
332 Shadow cur(fast_state);
333 cur.SetWrite(is_write);
334 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000335 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000336 shadow_mem, cur);
337 shadow_mem += kShadowCnt;
338 }
339 // Handle ending, if any.
340 for (; size; addr++, size--) {
341 int const kAccessSizeLog = 0;
342 Shadow cur(fast_state);
343 cur.SetWrite(is_write);
344 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000345 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000346 shadow_mem, cur);
347 }
348}
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000349
350void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
351 uptr size, uptr step, bool is_write) {
352 if (size == 0)
353 return;
354 FastState fast_state = thr->fast_state;
355 if (fast_state.GetIgnoreBit())
356 return;
357 StatInc(thr, StatMopRange);
358 fast_state.IncrementEpoch();
359 thr->fast_state = fast_state;
360 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
361
362 for (uptr addr_end = addr + size; addr < addr_end; addr += step) {
363 u64 *shadow_mem = (u64*)MemToShadow(addr);
364 Shadow cur(fast_state);
365 cur.SetWrite(is_write);
366 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kSizeLog1);
367 MemoryAccessImpl(thr, addr, kSizeLog1, is_write, false,
368 shadow_mem, cur);
369 }
370}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000371} // namespace __tsan