blob: bcb5498cc0a690e51af33c94bf11d6fef511dddb [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()
30 , epoch1()
31 , dead_info() {
32}
33
Dmitry Vyukov49e462f2013-03-18 10:10:15 +000034#ifndef TSAN_GO
35ThreadContext::~ThreadContext() {
36}
37#endif
38
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000039void ThreadContext::OnDead() {
40 sync.Reset();
41}
42
43void ThreadContext::OnJoined(void *arg) {
44 ThreadState *caller_thr = static_cast<ThreadState *>(arg);
45 caller_thr->clock.acquire(&sync);
46 StatInc(caller_thr, StatSyncAcquire);
Dmitry Vyukov509dab32013-03-19 10:22:33 +000047 sync.Reset();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000048}
49
50struct OnCreatedArgs {
51 ThreadState *thr;
52 uptr pc;
53};
54
55void ThreadContext::OnCreated(void *arg) {
56 thr = 0;
Dmitry Vyukov50160032013-03-18 08:52:46 +000057 if (tid == 0)
58 return;
59 OnCreatedArgs *args = static_cast<OnCreatedArgs *>(arg);
60 args->thr->fast_state.IncrementEpoch();
61 // Can't increment epoch w/o writing to the trace as well.
62 TraceAddEvent(args->thr, args->thr->fast_state, EventTypeMop, 0);
63 args->thr->clock.set(args->thr->tid, args->thr->fast_state.epoch());
64 args->thr->fast_synch_epoch = args->thr->fast_state.epoch();
65 args->thr->clock.release(&sync);
66 StatInc(args->thr, StatSyncRelease);
Dmitry Vyukov7cd20252013-03-18 09:02:27 +000067#ifdef TSAN_GO
Dmitry Vyukov50160032013-03-18 08:52:46 +000068 creation_stack.ObtainCurrent(args->thr, args->pc);
Dmitry Vyukov7cd20252013-03-18 09:02:27 +000069#else
70 creation_stack_id = CurrentStackId(args->thr, args->pc);
71#endif
Dmitry Vyukov50160032013-03-18 08:52:46 +000072 if (reuse_count == 0)
73 StatInc(args->thr, StatThreadMaxTid);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000074}
75
76void ThreadContext::OnReset(void *arg) {
77 OnCreatedArgs *args = static_cast<OnCreatedArgs *>(arg);
78 StatInc(args->thr, StatThreadReuse);
79 sync.Reset();
Dmitry Vyukova1bdd2d2013-03-18 09:09:41 +000080 DestroyAndFree(dead_info);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000081}
82
83struct OnStartedArgs {
84 ThreadState *thr;
85 uptr stk_addr;
86 uptr stk_size;
87 uptr tls_addr;
88 uptr tls_size;
89};
90
91void ThreadContext::OnStarted(void *arg) {
92 OnStartedArgs *args = static_cast<OnStartedArgs*>(arg);
Dmitry Vyukov50160032013-03-18 08:52:46 +000093 thr = args->thr;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000094 // RoundUp so that one trace part does not contain events
95 // from different threads.
96 epoch0 = RoundUp(epoch1 + 1, kTracePartSize);
97 epoch1 = (u64)-1;
Dmitry Vyukov50160032013-03-18 08:52:46 +000098 new(thr) ThreadState(CTX(), tid, unique_id,
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000099 epoch0, args->stk_addr, args->stk_size, args->tls_addr, args->tls_size);
100#ifdef TSAN_GO
101 // Setup dynamic shadow stack.
102 const int kInitStackSize = 8;
103 args->thr->shadow_stack = (uptr*)internal_alloc(MBlockShadowStack,
104 kInitStackSize * sizeof(uptr));
105 args->thr->shadow_stack_pos = thr->shadow_stack;
106 args->thr->shadow_stack_end = thr->shadow_stack + kInitStackSize;
107#endif
108#ifndef TSAN_GO
109 AllocatorThreadStart(args->thr);
110#endif
111 thr = args->thr;
112 thr->fast_synch_epoch = epoch0;
113 thr->clock.set(tid, epoch0);
114 thr->clock.acquire(&sync);
115 thr->fast_state.SetHistorySize(flags()->history_size);
116 const uptr trace = (epoch0 / kTracePartSize) % TraceParts();
117 thr->trace.headers[trace].epoch0 = epoch0;
118 StatInc(thr, StatSyncAcquire);
119 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 }
136 // Save from info about the thread.
137 dead_info = new(internal_alloc(MBlockDeadInfo, sizeof(ThreadDeadInfo)))
138 ThreadDeadInfo();
139 for (uptr i = 0; i < TraceParts(); i++) {
140 dead_info->trace.headers[i].epoch0 = thr->trace.headers[i].epoch0;
141 dead_info->trace.headers[i].stack0.CopyFrom(
142 thr->trace.headers[i].stack0);
143 }
144 epoch1 = thr->fast_state.epoch();
145
146#ifndef TSAN_GO
147 AllocatorThreadFinish(thr);
148#endif
149 thr->~ThreadState();
150 StatAggregate(CTX()->stat, thr->stat);
151 thr = 0;
152}
153
154static void MaybeReportThreadLeak(ThreadContextBase *tctx_base, void *unused) {
155 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000156 if (tctx->detached)
157 return;
158 if (tctx->status != ThreadStatusCreated
159 && tctx->status != ThreadStatusRunning
160 && tctx->status != ThreadStatusFinished)
161 return;
162 ScopedReport rep(ReportTypeThreadLeak);
163 rep.AddThread(tctx);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000164 OutputReport(CTX(), rep);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000165}
166
167void ThreadFinalize(ThreadState *thr) {
168 CHECK_GT(thr->in_rtl, 0);
169 if (!flags()->report_thread_leaks)
170 return;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000171 ThreadRegistryLock l(CTX()->thread_registry);
172 CTX()->thread_registry->RunCallbackForEachThreadLocked(
173 MaybeReportThreadLeak, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000174}
175
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000176int ThreadCount(ThreadState *thr) {
177 CHECK_GT(thr->in_rtl, 0);
178 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000179 uptr result;
180 ctx->thread_registry->GetNumberOfThreads(0, 0, &result);
181 return (int)result;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000182}
183
184int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
185 CHECK_GT(thr->in_rtl, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000186 StatInc(thr, StatThreadCreate);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000187 Context *ctx = CTX();
188 OnCreatedArgs args = { thr, pc };
189 int tid = ctx->thread_registry->CreateThread(uid, detached, thr->tid, &args);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000190 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000191 StatSet(thr, StatThreadMaxAlive, ctx->thread_registry->GetMaxAliveThreads());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000192 return tid;
193}
194
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000195void ThreadStart(ThreadState *thr, int tid, uptr os_id) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000196 CHECK_GT(thr->in_rtl, 0);
197 uptr stk_addr = 0;
198 uptr stk_size = 0;
199 uptr tls_addr = 0;
200 uptr tls_size = 0;
Dmitry Vyukov7339eb12012-05-25 11:15:04 +0000201 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000202
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000203 if (tid) {
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000204 if (stk_addr && stk_size)
Dmitry Vyukovce26a0a2013-03-18 16:56:48 +0000205 MemoryRangeImitateWrite(thr, /*pc=*/ 1, stk_addr, stk_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000206
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000207 if (tls_addr && tls_size) {
208 // Check that the thr object is in tls;
209 const uptr thr_beg = (uptr)thr;
210 const uptr thr_end = (uptr)thr + sizeof(*thr);
211 CHECK_GE(thr_beg, tls_addr);
212 CHECK_LE(thr_beg, tls_addr + tls_size);
213 CHECK_GE(thr_end, tls_addr);
214 CHECK_LE(thr_end, tls_addr + tls_size);
215 // Since the thr object is huge, skip it.
Dmitry Vyukovce26a0a2013-03-18 16:56:48 +0000216 MemoryRangeImitateWrite(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
217 MemoryRangeImitateWrite(thr, /*pc=*/ 2,
218 thr_end, tls_addr + tls_size - thr_end);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000219 }
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000220 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000221
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000222 OnStartedArgs args = { thr, stk_addr, stk_size, tls_addr, tls_size };
223 CTX()->thread_registry->StartThread(tid, os_id, &args);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000224}
225
226void ThreadFinish(ThreadState *thr) {
227 CHECK_GT(thr->in_rtl, 0);
228 StatInc(thr, StatThreadFinish);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000229 if (thr->stk_addr && thr->stk_size)
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000230 DontNeedShadowFor(thr->stk_addr, thr->stk_size);
231 if (thr->tls_addr && thr->tls_size)
232 DontNeedShadowFor(thr->tls_addr, thr->tls_size);
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000233 thr->is_alive = false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000234 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000235 ctx->thread_registry->FinishThread(thr->tid);
236}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000237
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000238static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) {
239 uptr uid = (uptr)arg;
240 if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) {
241 tctx->user_id = 0;
242 return true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000243 }
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000244 return false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000245}
246
247int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
248 CHECK_GT(thr->in_rtl, 0);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000249 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000250 int res = ctx->thread_registry->FindThread(FindThreadByUid, (void*)uid);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000251 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000252 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000253}
254
255void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
256 CHECK_GT(thr->in_rtl, 0);
257 CHECK_GT(tid, 0);
258 CHECK_LT(tid, kMaxTid);
259 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
260 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000261 ctx->thread_registry->JoinThread(tid, thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000262}
263
264void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
265 CHECK_GT(thr->in_rtl, 0);
266 CHECK_GT(tid, 0);
267 CHECK_LT(tid, kMaxTid);
268 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000269 ctx->thread_registry->DetachThread(tid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000270}
271
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000272void ThreadSetName(ThreadState *thr, const char *name) {
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000273 CHECK_GT(thr->in_rtl, 0);
274 CTX()->thread_registry->SetThreadName(thr->tid, name);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000275}
276
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000277void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
278 uptr size, bool is_write) {
279 if (size == 0)
280 return;
281
282 u64 *shadow_mem = (u64*)MemToShadow(addr);
283 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
284 thr->tid, (void*)pc, (void*)addr,
285 (int)size, is_write);
286
287#if TSAN_DEBUG
288 if (!IsAppMem(addr)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000289 Printf("Access to non app mem %zx\n", addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000290 DCHECK(IsAppMem(addr));
291 }
292 if (!IsAppMem(addr + size - 1)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000293 Printf("Access to non app mem %zx\n", addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000294 DCHECK(IsAppMem(addr + size - 1));
295 }
296 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000297 Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000298 DCHECK(IsShadowMem((uptr)shadow_mem));
299 }
300 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000301 Printf("Bad shadow addr %p (%zx)\n",
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000302 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000303 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
304 }
305#endif
306
307 StatInc(thr, StatMopRange);
308
309 FastState fast_state = thr->fast_state;
310 if (fast_state.GetIgnoreBit())
311 return;
312
313 fast_state.IncrementEpoch();
314 thr->fast_state = fast_state;
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000315 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000316
317 bool unaligned = (addr % kShadowCell) != 0;
318
319 // Handle unaligned beginning, if any.
320 for (; addr % kShadowCell && size; addr++, size--) {
321 int const kAccessSizeLog = 0;
322 Shadow cur(fast_state);
323 cur.SetWrite(is_write);
324 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000325 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000326 shadow_mem, cur);
327 }
328 if (unaligned)
329 shadow_mem += kShadowCnt;
330 // Handle middle part, if any.
331 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
332 int const kAccessSizeLog = 3;
333 Shadow cur(fast_state);
334 cur.SetWrite(is_write);
335 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000336 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000337 shadow_mem, cur);
338 shadow_mem += kShadowCnt;
339 }
340 // Handle ending, if any.
341 for (; size; addr++, size--) {
342 int const kAccessSizeLog = 0;
343 Shadow cur(fast_state);
344 cur.SetWrite(is_write);
345 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000346 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000347 shadow_mem, cur);
348 }
349}
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000350
351void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
352 uptr size, uptr step, bool is_write) {
353 if (size == 0)
354 return;
355 FastState fast_state = thr->fast_state;
356 if (fast_state.GetIgnoreBit())
357 return;
358 StatInc(thr, StatMopRange);
359 fast_state.IncrementEpoch();
360 thr->fast_state = fast_state;
361 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
362
363 for (uptr addr_end = addr + size; addr < addr_end; addr += step) {
364 u64 *shadow_mem = (u64*)MemToShadow(addr);
365 Shadow cur(fast_state);
366 cur.SetWrite(is_write);
367 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kSizeLog1);
368 MemoryAccessImpl(thr, addr, kSizeLog1, is_write, false,
369 shadow_mem, cur);
370 }
371}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000372} // namespace __tsan