blob: 35e97873a3b9679c4fefaf010dfa36851aa61c85 [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
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000146#ifndef TSAN_GO
147struct ThreadLeak {
148 ThreadContext *tctx;
149 int count;
150};
151
152static void MaybeReportThreadLeak(ThreadContextBase *tctx_base, void *arg) {
153 Vector<ThreadLeak> &leaks = *(Vector<ThreadLeak>*)arg;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000154 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000155 if (tctx->detached || tctx->status != ThreadStatusFinished)
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000156 return;
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000157 for (uptr i = 0; i < leaks.Size(); i++) {
158 if (leaks[i].tctx->creation_stack_id == tctx->creation_stack_id) {
159 leaks[i].count++;
160 return;
161 }
162 }
163 ThreadLeak leak = {tctx, 1};
164 leaks.PushBack(leak);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000165}
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000166#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000167
168void ThreadFinalize(ThreadState *thr) {
169 CHECK_GT(thr->in_rtl, 0);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000170#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000171 if (!flags()->report_thread_leaks)
172 return;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000173 ThreadRegistryLock l(CTX()->thread_registry);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000174 Vector<ThreadLeak> leaks(MBlockScopedBuf);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000175 CTX()->thread_registry->RunCallbackForEachThreadLocked(
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000176 MaybeReportThreadLeak, &leaks);
177 for (uptr i = 0; i < leaks.Size(); i++) {
178 ScopedReport rep(ReportTypeThreadLeak);
179 rep.AddThread(leaks[i].tctx);
180 rep.SetCount(leaks[i].count);
181 OutputReport(CTX(), rep);
182 }
183#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000184}
185
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000186int ThreadCount(ThreadState *thr) {
187 CHECK_GT(thr->in_rtl, 0);
188 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000189 uptr result;
190 ctx->thread_registry->GetNumberOfThreads(0, 0, &result);
191 return (int)result;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000192}
193
194int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
195 CHECK_GT(thr->in_rtl, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000196 StatInc(thr, StatThreadCreate);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000197 Context *ctx = CTX();
198 OnCreatedArgs args = { thr, pc };
199 int tid = ctx->thread_registry->CreateThread(uid, detached, thr->tid, &args);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000200 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000201 StatSet(thr, StatThreadMaxAlive, ctx->thread_registry->GetMaxAliveThreads());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000202 return tid;
203}
204
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000205void ThreadStart(ThreadState *thr, int tid, uptr os_id) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000206 CHECK_GT(thr->in_rtl, 0);
207 uptr stk_addr = 0;
208 uptr stk_size = 0;
209 uptr tls_addr = 0;
210 uptr tls_size = 0;
Dmitry Vyukov7339eb12012-05-25 11:15:04 +0000211 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000212
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000213 if (tid) {
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000214 if (stk_addr && stk_size)
Dmitry Vyukovce26a0a2013-03-18 16:56:48 +0000215 MemoryRangeImitateWrite(thr, /*pc=*/ 1, stk_addr, stk_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000216
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000217 if (tls_addr && tls_size) {
218 // Check that the thr object is in tls;
219 const uptr thr_beg = (uptr)thr;
220 const uptr thr_end = (uptr)thr + sizeof(*thr);
221 CHECK_GE(thr_beg, tls_addr);
222 CHECK_LE(thr_beg, tls_addr + tls_size);
223 CHECK_GE(thr_end, tls_addr);
224 CHECK_LE(thr_end, tls_addr + tls_size);
225 // Since the thr object is huge, skip it.
Dmitry Vyukovce26a0a2013-03-18 16:56:48 +0000226 MemoryRangeImitateWrite(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
227 MemoryRangeImitateWrite(thr, /*pc=*/ 2,
228 thr_end, tls_addr + tls_size - thr_end);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000229 }
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000230 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000231
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000232 OnStartedArgs args = { thr, stk_addr, stk_size, tls_addr, tls_size };
233 CTX()->thread_registry->StartThread(tid, os_id, &args);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000234}
235
236void ThreadFinish(ThreadState *thr) {
237 CHECK_GT(thr->in_rtl, 0);
238 StatInc(thr, StatThreadFinish);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000239 if (thr->stk_addr && thr->stk_size)
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000240 DontNeedShadowFor(thr->stk_addr, thr->stk_size);
241 if (thr->tls_addr && thr->tls_size)
242 DontNeedShadowFor(thr->tls_addr, thr->tls_size);
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000243 thr->is_alive = false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000244 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000245 ctx->thread_registry->FinishThread(thr->tid);
246}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000247
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000248static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) {
249 uptr uid = (uptr)arg;
250 if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) {
251 tctx->user_id = 0;
252 return true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000253 }
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000254 return false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000255}
256
257int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
258 CHECK_GT(thr->in_rtl, 0);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000259 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000260 int res = ctx->thread_registry->FindThread(FindThreadByUid, (void*)uid);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000261 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000262 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000263}
264
265void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
266 CHECK_GT(thr->in_rtl, 0);
267 CHECK_GT(tid, 0);
268 CHECK_LT(tid, kMaxTid);
269 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
270 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000271 ctx->thread_registry->JoinThread(tid, thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000272}
273
274void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
275 CHECK_GT(thr->in_rtl, 0);
276 CHECK_GT(tid, 0);
277 CHECK_LT(tid, kMaxTid);
278 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000279 ctx->thread_registry->DetachThread(tid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000280}
281
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000282void ThreadSetName(ThreadState *thr, const char *name) {
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000283 CHECK_GT(thr->in_rtl, 0);
284 CTX()->thread_registry->SetThreadName(thr->tid, name);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000285}
286
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000287void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
288 uptr size, bool is_write) {
289 if (size == 0)
290 return;
291
292 u64 *shadow_mem = (u64*)MemToShadow(addr);
293 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
294 thr->tid, (void*)pc, (void*)addr,
295 (int)size, is_write);
296
297#if TSAN_DEBUG
298 if (!IsAppMem(addr)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000299 Printf("Access to non app mem %zx\n", addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000300 DCHECK(IsAppMem(addr));
301 }
302 if (!IsAppMem(addr + size - 1)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000303 Printf("Access to non app mem %zx\n", addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000304 DCHECK(IsAppMem(addr + size - 1));
305 }
306 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000307 Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000308 DCHECK(IsShadowMem((uptr)shadow_mem));
309 }
310 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000311 Printf("Bad shadow addr %p (%zx)\n",
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000312 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000313 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
314 }
315#endif
316
317 StatInc(thr, StatMopRange);
318
Dmitry Vyukovb62c1582013-03-20 13:21:50 +0000319 if (*shadow_mem == kShadowRodata) {
320 // Access to .rodata section, no races here.
321 // Measurements show that it can be 10-20% of all memory accesses.
322 StatInc(thr, StatMopRangeRodata);
323 return;
324 }
325
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000326 FastState fast_state = thr->fast_state;
327 if (fast_state.GetIgnoreBit())
328 return;
329
330 fast_state.IncrementEpoch();
331 thr->fast_state = fast_state;
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000332 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000333
334 bool unaligned = (addr % kShadowCell) != 0;
335
336 // Handle unaligned beginning, if any.
337 for (; addr % kShadowCell && size; addr++, size--) {
338 int const kAccessSizeLog = 0;
339 Shadow cur(fast_state);
340 cur.SetWrite(is_write);
341 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000342 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000343 shadow_mem, cur);
344 }
345 if (unaligned)
346 shadow_mem += kShadowCnt;
347 // Handle middle part, if any.
348 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
349 int const kAccessSizeLog = 3;
350 Shadow cur(fast_state);
351 cur.SetWrite(is_write);
352 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000353 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000354 shadow_mem, cur);
355 shadow_mem += kShadowCnt;
356 }
357 // Handle ending, if any.
358 for (; size; addr++, size--) {
359 int const kAccessSizeLog = 0;
360 Shadow cur(fast_state);
361 cur.SetWrite(is_write);
362 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000363 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000364 shadow_mem, cur);
365 }
366}
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000367
368void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
369 uptr size, uptr step, bool is_write) {
370 if (size == 0)
371 return;
372 FastState fast_state = thr->fast_state;
373 if (fast_state.GetIgnoreBit())
374 return;
375 StatInc(thr, StatMopRange);
376 fast_state.IncrementEpoch();
377 thr->fast_state = fast_state;
378 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
379
380 for (uptr addr_end = addr + size; addr < addr_end; addr += step) {
381 u64 *shadow_mem = (u64*)MemToShadow(addr);
382 Shadow cur(fast_state);
383 cur.SetWrite(is_write);
384 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kSizeLog1);
385 MemoryAccessImpl(thr, addr, kSizeLog1, is_write, false,
386 shadow_mem, cur);
387 }
388}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000389} // namespace __tsan