blob: ad7d505695a23f9e079676c129535e07f6312bec [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
34void ThreadContext::OnDead() {
35 sync.Reset();
36}
37
38void ThreadContext::OnJoined(void *arg) {
39 ThreadState *caller_thr = static_cast<ThreadState *>(arg);
40 caller_thr->clock.acquire(&sync);
41 StatInc(caller_thr, StatSyncAcquire);
42}
43
44struct OnCreatedArgs {
45 ThreadState *thr;
46 uptr pc;
47};
48
49void ThreadContext::OnCreated(void *arg) {
50 thr = 0;
51 if (tid != 0) {
52 OnCreatedArgs *args = static_cast<OnCreatedArgs *>(arg);
53 args->thr->fast_state.IncrementEpoch();
54 // Can't increment epoch w/o writing to the trace as well.
55 TraceAddEvent(args->thr, args->thr->fast_state, EventTypeMop, 0);
56 args->thr->clock.set(args->thr->tid, args->thr->fast_state.epoch());
57 args->thr->fast_synch_epoch = args->thr->fast_state.epoch();
58 args->thr->clock.release(&sync);
59 StatInc(args->thr, StatSyncRelease);
60 creation_stack.ObtainCurrent(args->thr, args->pc);
61 }
62}
63
64void ThreadContext::OnReset(void *arg) {
65 OnCreatedArgs *args = static_cast<OnCreatedArgs *>(arg);
66 StatInc(args->thr, StatThreadReuse);
67 sync.Reset();
68}
69
70struct OnStartedArgs {
71 ThreadState *thr;
72 uptr stk_addr;
73 uptr stk_size;
74 uptr tls_addr;
75 uptr tls_size;
76};
77
78void ThreadContext::OnStarted(void *arg) {
79 OnStartedArgs *args = static_cast<OnStartedArgs*>(arg);
80 // RoundUp so that one trace part does not contain events
81 // from different threads.
82 epoch0 = RoundUp(epoch1 + 1, kTracePartSize);
83 epoch1 = (u64)-1;
84 new(args->thr) ThreadState(CTX(), tid, unique_id,
85 epoch0, args->stk_addr, args->stk_size, args->tls_addr, args->tls_size);
86#ifdef TSAN_GO
87 // Setup dynamic shadow stack.
88 const int kInitStackSize = 8;
89 args->thr->shadow_stack = (uptr*)internal_alloc(MBlockShadowStack,
90 kInitStackSize * sizeof(uptr));
91 args->thr->shadow_stack_pos = thr->shadow_stack;
92 args->thr->shadow_stack_end = thr->shadow_stack + kInitStackSize;
93#endif
94#ifndef TSAN_GO
95 AllocatorThreadStart(args->thr);
96#endif
97 thr = args->thr;
98 thr->fast_synch_epoch = epoch0;
99 thr->clock.set(tid, epoch0);
100 thr->clock.acquire(&sync);
101 thr->fast_state.SetHistorySize(flags()->history_size);
102 const uptr trace = (epoch0 / kTracePartSize) % TraceParts();
103 thr->trace.headers[trace].epoch0 = epoch0;
104 StatInc(thr, StatSyncAcquire);
105 DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx "
106 "tls_addr=%zx tls_size=%zx\n",
107 tid, (uptr)epoch0, stk_addr, stk_size, tls_addr, tls_size);
108 thr->is_alive = true;
109}
110
111void ThreadContext::OnFinished() {
112 if (!detached) {
113 thr->fast_state.IncrementEpoch();
114 // Can't increment epoch w/o writing to the trace as well.
115 TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
116 thr->clock.set(thr->tid, thr->fast_state.epoch());
117 thr->fast_synch_epoch = thr->fast_state.epoch();
118 thr->clock.release(&sync);
119 StatInc(thr, StatSyncRelease);
120 }
121 // Save from info about the thread.
122 dead_info = new(internal_alloc(MBlockDeadInfo, sizeof(ThreadDeadInfo)))
123 ThreadDeadInfo();
124 for (uptr i = 0; i < TraceParts(); i++) {
125 dead_info->trace.headers[i].epoch0 = thr->trace.headers[i].epoch0;
126 dead_info->trace.headers[i].stack0.CopyFrom(
127 thr->trace.headers[i].stack0);
128 }
129 epoch1 = thr->fast_state.epoch();
130
131#ifndef TSAN_GO
132 AllocatorThreadFinish(thr);
133#endif
134 thr->~ThreadState();
135 StatAggregate(CTX()->stat, thr->stat);
136 thr = 0;
137}
138
139static void MaybeReportThreadLeak(ThreadContextBase *tctx_base, void *unused) {
140 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000141 if (tctx->detached)
142 return;
143 if (tctx->status != ThreadStatusCreated
144 && tctx->status != ThreadStatusRunning
145 && tctx->status != ThreadStatusFinished)
146 return;
147 ScopedReport rep(ReportTypeThreadLeak);
148 rep.AddThread(tctx);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000149 OutputReport(CTX(), rep);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000150}
151
152void ThreadFinalize(ThreadState *thr) {
153 CHECK_GT(thr->in_rtl, 0);
154 if (!flags()->report_thread_leaks)
155 return;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000156 ThreadRegistryLock l(CTX()->thread_registry);
157 CTX()->thread_registry->RunCallbackForEachThreadLocked(
158 MaybeReportThreadLeak, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000159}
160
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000161int ThreadCount(ThreadState *thr) {
162 CHECK_GT(thr->in_rtl, 0);
163 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000164 uptr result;
165 ctx->thread_registry->GetNumberOfThreads(0, 0, &result);
166 return (int)result;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000167}
168
169int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
170 CHECK_GT(thr->in_rtl, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000171 StatInc(thr, StatThreadCreate);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000172 Context *ctx = CTX();
173 OnCreatedArgs args = { thr, pc };
174 int tid = ctx->thread_registry->CreateThread(uid, detached, thr->tid, &args);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000175 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000176 StatSet(thr, StatThreadMaxAlive, ctx->thread_registry->GetMaxAliveThreads());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000177 return tid;
178}
179
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000180void ThreadStart(ThreadState *thr, int tid, uptr os_id) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000181 CHECK_GT(thr->in_rtl, 0);
182 uptr stk_addr = 0;
183 uptr stk_size = 0;
184 uptr tls_addr = 0;
185 uptr tls_size = 0;
Dmitry Vyukov7339eb12012-05-25 11:15:04 +0000186 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000187
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000188 if (tid) {
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000189 if (stk_addr && stk_size) {
190 MemoryResetRange(thr, /*pc=*/ 1, stk_addr, stk_size);
191 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000192
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000193 if (tls_addr && tls_size) {
194 // Check that the thr object is in tls;
195 const uptr thr_beg = (uptr)thr;
196 const uptr thr_end = (uptr)thr + sizeof(*thr);
197 CHECK_GE(thr_beg, tls_addr);
198 CHECK_LE(thr_beg, tls_addr + tls_size);
199 CHECK_GE(thr_end, tls_addr);
200 CHECK_LE(thr_end, tls_addr + tls_size);
201 // Since the thr object is huge, skip it.
202 MemoryResetRange(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
203 MemoryResetRange(thr, /*pc=*/ 2, thr_end, tls_addr + tls_size - thr_end);
204 }
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000205 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000206
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000207 OnStartedArgs args = { thr, stk_addr, stk_size, tls_addr, tls_size };
208 CTX()->thread_registry->StartThread(tid, os_id, &args);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000209}
210
211void ThreadFinish(ThreadState *thr) {
212 CHECK_GT(thr->in_rtl, 0);
213 StatInc(thr, StatThreadFinish);
214 // FIXME: Treat it as write.
215 if (thr->stk_addr && thr->stk_size)
216 MemoryResetRange(thr, /*pc=*/ 3, thr->stk_addr, thr->stk_size);
217 if (thr->tls_addr && thr->tls_size) {
218 const uptr thr_beg = (uptr)thr;
219 const uptr thr_end = (uptr)thr + sizeof(*thr);
220 // Since the thr object is huge, skip it.
221 MemoryResetRange(thr, /*pc=*/ 4, thr->tls_addr, thr_beg - thr->tls_addr);
222 MemoryResetRange(thr, /*pc=*/ 5,
223 thr_end, thr->tls_addr + thr->tls_size - thr_end);
224 }
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
301 FastState fast_state = thr->fast_state;
302 if (fast_state.GetIgnoreBit())
303 return;
304
305 fast_state.IncrementEpoch();
306 thr->fast_state = fast_state;
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000307 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000308
309 bool unaligned = (addr % kShadowCell) != 0;
310
311 // Handle unaligned beginning, if any.
312 for (; addr % kShadowCell && size; addr++, size--) {
313 int const kAccessSizeLog = 0;
314 Shadow cur(fast_state);
315 cur.SetWrite(is_write);
316 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000317 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000318 shadow_mem, cur);
319 }
320 if (unaligned)
321 shadow_mem += kShadowCnt;
322 // Handle middle part, if any.
323 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
324 int const kAccessSizeLog = 3;
325 Shadow cur(fast_state);
326 cur.SetWrite(is_write);
327 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000328 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000329 shadow_mem, cur);
330 shadow_mem += kShadowCnt;
331 }
332 // Handle ending, if any.
333 for (; size; addr++, size--) {
334 int const kAccessSizeLog = 0;
335 Shadow cur(fast_state);
336 cur.SetWrite(is_write);
337 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000338 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000339 shadow_mem, cur);
340 }
341}
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000342
343void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
344 uptr size, uptr step, bool is_write) {
345 if (size == 0)
346 return;
347 FastState fast_state = thr->fast_state;
348 if (fast_state.GetIgnoreBit())
349 return;
350 StatInc(thr, StatMopRange);
351 fast_state.IncrementEpoch();
352 thr->fast_state = fast_state;
353 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
354
355 for (uptr addr_end = addr + size; addr < addr_end; addr += step) {
356 u64 *shadow_mem = (u64*)MemToShadow(addr);
357 Shadow cur(fast_state);
358 cur.SetWrite(is_write);
359 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kSizeLog1);
360 MemoryAccessImpl(thr, addr, kSizeLog1, is_write, false,
361 shadow_mem, cur);
362 }
363}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000364} // namespace __tsan