blob: 0372bb539646aa905d402857d62a889dc7f65ac2 [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);
47}
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
75void ThreadContext::OnReset(void *arg) {
76 OnCreatedArgs *args = static_cast<OnCreatedArgs *>(arg);
77 StatInc(args->thr, StatThreadReuse);
78 sync.Reset();
Dmitry Vyukova1bdd2d2013-03-18 09:09:41 +000079 DestroyAndFree(dead_info);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000080}
81
82struct OnStartedArgs {
83 ThreadState *thr;
84 uptr stk_addr;
85 uptr stk_size;
86 uptr tls_addr;
87 uptr tls_size;
88};
89
90void ThreadContext::OnStarted(void *arg) {
91 OnStartedArgs *args = static_cast<OnStartedArgs*>(arg);
Dmitry Vyukov50160032013-03-18 08:52:46 +000092 thr = args->thr;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000093 // RoundUp so that one trace part does not contain events
94 // from different threads.
95 epoch0 = RoundUp(epoch1 + 1, kTracePartSize);
96 epoch1 = (u64)-1;
Dmitry Vyukov50160032013-03-18 08:52:46 +000097 new(thr) ThreadState(CTX(), tid, unique_id,
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000098 epoch0, args->stk_addr, args->stk_size, args->tls_addr, args->tls_size);
99#ifdef TSAN_GO
100 // Setup dynamic shadow stack.
101 const int kInitStackSize = 8;
102 args->thr->shadow_stack = (uptr*)internal_alloc(MBlockShadowStack,
103 kInitStackSize * sizeof(uptr));
104 args->thr->shadow_stack_pos = thr->shadow_stack;
105 args->thr->shadow_stack_end = thr->shadow_stack + kInitStackSize;
106#endif
107#ifndef TSAN_GO
108 AllocatorThreadStart(args->thr);
109#endif
110 thr = args->thr;
111 thr->fast_synch_epoch = epoch0;
112 thr->clock.set(tid, epoch0);
113 thr->clock.acquire(&sync);
114 thr->fast_state.SetHistorySize(flags()->history_size);
115 const uptr trace = (epoch0 / kTracePartSize) % TraceParts();
116 thr->trace.headers[trace].epoch0 = epoch0;
117 StatInc(thr, StatSyncAcquire);
118 DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx "
119 "tls_addr=%zx tls_size=%zx\n",
Alexey Samsonovb5d10f62013-03-18 09:45:22 +0000120 tid, (uptr)epoch0, args->stk_addr, args->stk_size,
121 args->tls_addr, args->tls_size);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000122 thr->is_alive = true;
123}
124
125void ThreadContext::OnFinished() {
126 if (!detached) {
127 thr->fast_state.IncrementEpoch();
128 // Can't increment epoch w/o writing to the trace as well.
129 TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
130 thr->clock.set(thr->tid, thr->fast_state.epoch());
131 thr->fast_synch_epoch = thr->fast_state.epoch();
132 thr->clock.release(&sync);
133 StatInc(thr, StatSyncRelease);
134 }
135 // Save from info about the thread.
136 dead_info = new(internal_alloc(MBlockDeadInfo, sizeof(ThreadDeadInfo)))
137 ThreadDeadInfo();
138 for (uptr i = 0; i < TraceParts(); i++) {
139 dead_info->trace.headers[i].epoch0 = thr->trace.headers[i].epoch0;
140 dead_info->trace.headers[i].stack0.CopyFrom(
141 thr->trace.headers[i].stack0);
142 }
143 epoch1 = thr->fast_state.epoch();
144
145#ifndef TSAN_GO
146 AllocatorThreadFinish(thr);
147#endif
148 thr->~ThreadState();
149 StatAggregate(CTX()->stat, thr->stat);
150 thr = 0;
151}
152
153static void MaybeReportThreadLeak(ThreadContextBase *tctx_base, void *unused) {
154 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000155 if (tctx->detached)
156 return;
157 if (tctx->status != ThreadStatusCreated
158 && tctx->status != ThreadStatusRunning
159 && tctx->status != ThreadStatusFinished)
160 return;
161 ScopedReport rep(ReportTypeThreadLeak);
162 rep.AddThread(tctx);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000163 OutputReport(CTX(), rep);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000164}
165
166void ThreadFinalize(ThreadState *thr) {
167 CHECK_GT(thr->in_rtl, 0);
168 if (!flags()->report_thread_leaks)
169 return;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000170 ThreadRegistryLock l(CTX()->thread_registry);
171 CTX()->thread_registry->RunCallbackForEachThreadLocked(
172 MaybeReportThreadLeak, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000173}
174
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000175int ThreadCount(ThreadState *thr) {
176 CHECK_GT(thr->in_rtl, 0);
177 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000178 uptr result;
179 ctx->thread_registry->GetNumberOfThreads(0, 0, &result);
180 return (int)result;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000181}
182
183int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
184 CHECK_GT(thr->in_rtl, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000185 StatInc(thr, StatThreadCreate);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000186 Context *ctx = CTX();
187 OnCreatedArgs args = { thr, pc };
188 int tid = ctx->thread_registry->CreateThread(uid, detached, thr->tid, &args);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000189 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000190 StatSet(thr, StatThreadMaxAlive, ctx->thread_registry->GetMaxAliveThreads());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000191 return tid;
192}
193
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000194void ThreadStart(ThreadState *thr, int tid, uptr os_id) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000195 CHECK_GT(thr->in_rtl, 0);
196 uptr stk_addr = 0;
197 uptr stk_size = 0;
198 uptr tls_addr = 0;
199 uptr tls_size = 0;
Dmitry Vyukov7339eb12012-05-25 11:15:04 +0000200 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000201
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000202 if (tid) {
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000203 if (stk_addr && stk_size)
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000204 MemoryResetRange(thr, /*pc=*/ 1, stk_addr, stk_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000205
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000206 if (tls_addr && tls_size) {
207 // Check that the thr object is in tls;
208 const uptr thr_beg = (uptr)thr;
209 const uptr thr_end = (uptr)thr + sizeof(*thr);
210 CHECK_GE(thr_beg, tls_addr);
211 CHECK_LE(thr_beg, tls_addr + tls_size);
212 CHECK_GE(thr_end, tls_addr);
213 CHECK_LE(thr_end, tls_addr + tls_size);
214 // Since the thr object is huge, skip it.
215 MemoryResetRange(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
216 MemoryResetRange(thr, /*pc=*/ 2, thr_end, tls_addr + tls_size - thr_end);
217 }
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000218 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000219
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000220 OnStartedArgs args = { thr, stk_addr, stk_size, tls_addr, tls_size };
221 CTX()->thread_registry->StartThread(tid, os_id, &args);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000222}
223
224void ThreadFinish(ThreadState *thr) {
225 CHECK_GT(thr->in_rtl, 0);
226 StatInc(thr, StatThreadFinish);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000227 if (thr->stk_addr && thr->stk_size)
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000228 DontNeedShadowFor(thr->stk_addr, thr->stk_size);
229 if (thr->tls_addr && thr->tls_size)
230 DontNeedShadowFor(thr->tls_addr, thr->tls_size);
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000231 thr->is_alive = false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000232 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000233 ctx->thread_registry->FinishThread(thr->tid);
234}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000235
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000236static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) {
237 uptr uid = (uptr)arg;
238 if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) {
239 tctx->user_id = 0;
240 return true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000241 }
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000242 return false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000243}
244
245int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
246 CHECK_GT(thr->in_rtl, 0);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000247 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000248 int res = ctx->thread_registry->FindThread(FindThreadByUid, (void*)uid);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000249 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000250 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000251}
252
253void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
254 CHECK_GT(thr->in_rtl, 0);
255 CHECK_GT(tid, 0);
256 CHECK_LT(tid, kMaxTid);
257 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
258 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000259 ctx->thread_registry->JoinThread(tid, thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000260}
261
262void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
263 CHECK_GT(thr->in_rtl, 0);
264 CHECK_GT(tid, 0);
265 CHECK_LT(tid, kMaxTid);
266 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000267 ctx->thread_registry->DetachThread(tid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000268}
269
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000270void ThreadSetName(ThreadState *thr, const char *name) {
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000271 CHECK_GT(thr->in_rtl, 0);
272 CTX()->thread_registry->SetThreadName(thr->tid, name);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000273}
274
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000275void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
276 uptr size, bool is_write) {
277 if (size == 0)
278 return;
279
280 u64 *shadow_mem = (u64*)MemToShadow(addr);
281 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
282 thr->tid, (void*)pc, (void*)addr,
283 (int)size, is_write);
284
285#if TSAN_DEBUG
286 if (!IsAppMem(addr)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000287 Printf("Access to non app mem %zx\n", addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000288 DCHECK(IsAppMem(addr));
289 }
290 if (!IsAppMem(addr + size - 1)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000291 Printf("Access to non app mem %zx\n", addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000292 DCHECK(IsAppMem(addr + size - 1));
293 }
294 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000295 Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000296 DCHECK(IsShadowMem((uptr)shadow_mem));
297 }
298 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000299 Printf("Bad shadow addr %p (%zx)\n",
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000300 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000301 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
302 }
303#endif
304
305 StatInc(thr, StatMopRange);
306
307 FastState fast_state = thr->fast_state;
308 if (fast_state.GetIgnoreBit())
309 return;
310
311 fast_state.IncrementEpoch();
312 thr->fast_state = fast_state;
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000313 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000314
315 bool unaligned = (addr % kShadowCell) != 0;
316
317 // Handle unaligned beginning, if any.
318 for (; addr % kShadowCell && size; addr++, size--) {
319 int const kAccessSizeLog = 0;
320 Shadow cur(fast_state);
321 cur.SetWrite(is_write);
322 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000323 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000324 shadow_mem, cur);
325 }
326 if (unaligned)
327 shadow_mem += kShadowCnt;
328 // Handle middle part, if any.
329 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
330 int const kAccessSizeLog = 3;
331 Shadow cur(fast_state);
332 cur.SetWrite(is_write);
333 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000334 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000335 shadow_mem, cur);
336 shadow_mem += kShadowCnt;
337 }
338 // Handle ending, if any.
339 for (; size; addr++, size--) {
340 int const kAccessSizeLog = 0;
341 Shadow cur(fast_state);
342 cur.SetWrite(is_write);
343 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000344 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000345 shadow_mem, cur);
346 }
347}
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000348
349void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
350 uptr size, uptr step, bool is_write) {
351 if (size == 0)
352 return;
353 FastState fast_state = thr->fast_state;
354 if (fast_state.GetIgnoreBit())
355 return;
356 StatInc(thr, StatMopRange);
357 fast_state.IncrementEpoch();
358 thr->fast_state = fast_state;
359 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
360
361 for (uptr addr_end = addr + size; addr < addr_end; addr += step) {
362 u64 *shadow_mem = (u64*)MemToShadow(addr);
363 Shadow cur(fast_state);
364 cur.SetWrite(is_write);
365 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kSizeLog1);
366 MemoryAccessImpl(thr, addr, kSizeLog1, is_write, false,
367 shadow_mem, cur);
368 }
369}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000370} // namespace __tsan