blob: 213b8df19dbaf28cece69cdaa68ca3e419a09887 [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;
Dmitry Vyukov50160032013-03-18 08:52:46 +000051 if (tid == 0)
52 return;
53 OnCreatedArgs *args = static_cast<OnCreatedArgs *>(arg);
54 args->thr->fast_state.IncrementEpoch();
55 // Can't increment epoch w/o writing to the trace as well.
56 TraceAddEvent(args->thr, args->thr->fast_state, EventTypeMop, 0);
57 args->thr->clock.set(args->thr->tid, args->thr->fast_state.epoch());
58 args->thr->fast_synch_epoch = args->thr->fast_state.epoch();
59 args->thr->clock.release(&sync);
60 StatInc(args->thr, StatSyncRelease);
61 creation_stack.ObtainCurrent(args->thr, args->pc);
62 if (reuse_count == 0)
63 StatInc(args->thr, StatThreadMaxTid);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000064}
65
66void ThreadContext::OnReset(void *arg) {
67 OnCreatedArgs *args = static_cast<OnCreatedArgs *>(arg);
68 StatInc(args->thr, StatThreadReuse);
69 sync.Reset();
70}
71
72struct OnStartedArgs {
73 ThreadState *thr;
74 uptr stk_addr;
75 uptr stk_size;
76 uptr tls_addr;
77 uptr tls_size;
78};
79
80void ThreadContext::OnStarted(void *arg) {
81 OnStartedArgs *args = static_cast<OnStartedArgs*>(arg);
Dmitry Vyukov50160032013-03-18 08:52:46 +000082 thr = args->thr;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000083 // RoundUp so that one trace part does not contain events
84 // from different threads.
85 epoch0 = RoundUp(epoch1 + 1, kTracePartSize);
86 epoch1 = (u64)-1;
Dmitry Vyukov50160032013-03-18 08:52:46 +000087 new(thr) ThreadState(CTX(), tid, unique_id,
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000088 epoch0, args->stk_addr, args->stk_size, args->tls_addr, args->tls_size);
89#ifdef TSAN_GO
90 // Setup dynamic shadow stack.
91 const int kInitStackSize = 8;
92 args->thr->shadow_stack = (uptr*)internal_alloc(MBlockShadowStack,
93 kInitStackSize * sizeof(uptr));
94 args->thr->shadow_stack_pos = thr->shadow_stack;
95 args->thr->shadow_stack_end = thr->shadow_stack + kInitStackSize;
96#endif
97#ifndef TSAN_GO
98 AllocatorThreadStart(args->thr);
99#endif
100 thr = args->thr;
101 thr->fast_synch_epoch = epoch0;
102 thr->clock.set(tid, epoch0);
103 thr->clock.acquire(&sync);
104 thr->fast_state.SetHistorySize(flags()->history_size);
105 const uptr trace = (epoch0 / kTracePartSize) % TraceParts();
106 thr->trace.headers[trace].epoch0 = epoch0;
107 StatInc(thr, StatSyncAcquire);
108 DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx "
109 "tls_addr=%zx tls_size=%zx\n",
Dmitry Vyukov50160032013-03-18 08:52:46 +0000110 tid, (uptr)epoch0, stk_addr, stk_size, tls_addr, tls_size);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000111 thr->is_alive = true;
112}
113
114void ThreadContext::OnFinished() {
115 if (!detached) {
116 thr->fast_state.IncrementEpoch();
117 // Can't increment epoch w/o writing to the trace as well.
118 TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
119 thr->clock.set(thr->tid, thr->fast_state.epoch());
120 thr->fast_synch_epoch = thr->fast_state.epoch();
121 thr->clock.release(&sync);
122 StatInc(thr, StatSyncRelease);
123 }
124 // Save from info about the thread.
125 dead_info = new(internal_alloc(MBlockDeadInfo, sizeof(ThreadDeadInfo)))
126 ThreadDeadInfo();
127 for (uptr i = 0; i < TraceParts(); i++) {
128 dead_info->trace.headers[i].epoch0 = thr->trace.headers[i].epoch0;
129 dead_info->trace.headers[i].stack0.CopyFrom(
130 thr->trace.headers[i].stack0);
131 }
132 epoch1 = thr->fast_state.epoch();
133
134#ifndef TSAN_GO
135 AllocatorThreadFinish(thr);
136#endif
137 thr->~ThreadState();
138 StatAggregate(CTX()->stat, thr->stat);
139 thr = 0;
140}
141
142static void MaybeReportThreadLeak(ThreadContextBase *tctx_base, void *unused) {
143 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000144 if (tctx->detached)
145 return;
146 if (tctx->status != ThreadStatusCreated
147 && tctx->status != ThreadStatusRunning
148 && tctx->status != ThreadStatusFinished)
149 return;
150 ScopedReport rep(ReportTypeThreadLeak);
151 rep.AddThread(tctx);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000152 OutputReport(CTX(), rep);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000153}
154
155void ThreadFinalize(ThreadState *thr) {
156 CHECK_GT(thr->in_rtl, 0);
157 if (!flags()->report_thread_leaks)
158 return;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000159 ThreadRegistryLock l(CTX()->thread_registry);
160 CTX()->thread_registry->RunCallbackForEachThreadLocked(
161 MaybeReportThreadLeak, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000162}
163
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000164int ThreadCount(ThreadState *thr) {
165 CHECK_GT(thr->in_rtl, 0);
166 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000167 uptr result;
168 ctx->thread_registry->GetNumberOfThreads(0, 0, &result);
169 return (int)result;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000170}
171
172int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
173 CHECK_GT(thr->in_rtl, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000174 StatInc(thr, StatThreadCreate);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000175 Context *ctx = CTX();
176 OnCreatedArgs args = { thr, pc };
177 int tid = ctx->thread_registry->CreateThread(uid, detached, thr->tid, &args);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000178 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000179 StatSet(thr, StatThreadMaxAlive, ctx->thread_registry->GetMaxAliveThreads());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000180 return tid;
181}
182
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000183void ThreadStart(ThreadState *thr, int tid, uptr os_id) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000184 CHECK_GT(thr->in_rtl, 0);
185 uptr stk_addr = 0;
186 uptr stk_size = 0;
187 uptr tls_addr = 0;
188 uptr tls_size = 0;
Dmitry Vyukov7339eb12012-05-25 11:15:04 +0000189 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000190
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000191 if (tid) {
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000192 if (stk_addr && stk_size) {
193 MemoryResetRange(thr, /*pc=*/ 1, stk_addr, stk_size);
194 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000195
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000196 if (tls_addr && tls_size) {
197 // Check that the thr object is in tls;
198 const uptr thr_beg = (uptr)thr;
199 const uptr thr_end = (uptr)thr + sizeof(*thr);
200 CHECK_GE(thr_beg, tls_addr);
201 CHECK_LE(thr_beg, tls_addr + tls_size);
202 CHECK_GE(thr_end, tls_addr);
203 CHECK_LE(thr_end, tls_addr + tls_size);
204 // Since the thr object is huge, skip it.
205 MemoryResetRange(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
206 MemoryResetRange(thr, /*pc=*/ 2, thr_end, tls_addr + tls_size - thr_end);
207 }
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000208 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000209
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000210 OnStartedArgs args = { thr, stk_addr, stk_size, tls_addr, tls_size };
211 CTX()->thread_registry->StartThread(tid, os_id, &args);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000212}
213
214void ThreadFinish(ThreadState *thr) {
215 CHECK_GT(thr->in_rtl, 0);
216 StatInc(thr, StatThreadFinish);
217 // FIXME: Treat it as write.
218 if (thr->stk_addr && thr->stk_size)
219 MemoryResetRange(thr, /*pc=*/ 3, thr->stk_addr, thr->stk_size);
220 if (thr->tls_addr && thr->tls_size) {
221 const uptr thr_beg = (uptr)thr;
222 const uptr thr_end = (uptr)thr + sizeof(*thr);
223 // Since the thr object is huge, skip it.
224 MemoryResetRange(thr, /*pc=*/ 4, thr->tls_addr, thr_beg - thr->tls_addr);
225 MemoryResetRange(thr, /*pc=*/ 5,
226 thr_end, thr->tls_addr + thr->tls_size - thr_end);
227 }
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000228 thr->is_alive = false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000229 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000230 ctx->thread_registry->FinishThread(thr->tid);
231}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000232
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000233static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) {
234 uptr uid = (uptr)arg;
235 if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) {
236 tctx->user_id = 0;
237 return true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000238 }
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000239 return false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000240}
241
242int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
243 CHECK_GT(thr->in_rtl, 0);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000244 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000245 int res = ctx->thread_registry->FindThread(FindThreadByUid, (void*)uid);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000246 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000247 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000248}
249
250void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
251 CHECK_GT(thr->in_rtl, 0);
252 CHECK_GT(tid, 0);
253 CHECK_LT(tid, kMaxTid);
254 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
255 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000256 ctx->thread_registry->JoinThread(tid, thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000257}
258
259void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
260 CHECK_GT(thr->in_rtl, 0);
261 CHECK_GT(tid, 0);
262 CHECK_LT(tid, kMaxTid);
263 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000264 ctx->thread_registry->DetachThread(tid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000265}
266
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000267void ThreadSetName(ThreadState *thr, const char *name) {
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000268 CHECK_GT(thr->in_rtl, 0);
269 CTX()->thread_registry->SetThreadName(thr->tid, name);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000270}
271
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000272void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
273 uptr size, bool is_write) {
274 if (size == 0)
275 return;
276
277 u64 *shadow_mem = (u64*)MemToShadow(addr);
278 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
279 thr->tid, (void*)pc, (void*)addr,
280 (int)size, is_write);
281
282#if TSAN_DEBUG
283 if (!IsAppMem(addr)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000284 Printf("Access to non app mem %zx\n", addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000285 DCHECK(IsAppMem(addr));
286 }
287 if (!IsAppMem(addr + size - 1)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000288 Printf("Access to non app mem %zx\n", addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000289 DCHECK(IsAppMem(addr + size - 1));
290 }
291 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000292 Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000293 DCHECK(IsShadowMem((uptr)shadow_mem));
294 }
295 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000296 Printf("Bad shadow addr %p (%zx)\n",
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000297 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000298 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
299 }
300#endif
301
302 StatInc(thr, StatMopRange);
303
304 FastState fast_state = thr->fast_state;
305 if (fast_state.GetIgnoreBit())
306 return;
307
308 fast_state.IncrementEpoch();
309 thr->fast_state = fast_state;
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000310 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000311
312 bool unaligned = (addr % kShadowCell) != 0;
313
314 // Handle unaligned beginning, if any.
315 for (; addr % kShadowCell && size; addr++, size--) {
316 int const kAccessSizeLog = 0;
317 Shadow cur(fast_state);
318 cur.SetWrite(is_write);
319 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000320 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000321 shadow_mem, cur);
322 }
323 if (unaligned)
324 shadow_mem += kShadowCnt;
325 // Handle middle part, if any.
326 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
327 int const kAccessSizeLog = 3;
328 Shadow cur(fast_state);
329 cur.SetWrite(is_write);
330 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000331 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000332 shadow_mem, cur);
333 shadow_mem += kShadowCnt;
334 }
335 // Handle ending, if any.
336 for (; size; addr++, size--) {
337 int const kAccessSizeLog = 0;
338 Shadow cur(fast_state);
339 cur.SetWrite(is_write);
340 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000341 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000342 shadow_mem, cur);
343 }
344}
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000345
346void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
347 uptr size, uptr step, bool is_write) {
348 if (size == 0)
349 return;
350 FastState fast_state = thr->fast_state;
351 if (fast_state.GetIgnoreBit())
352 return;
353 StatInc(thr, StatMopRange);
354 fast_state.IncrementEpoch();
355 thr->fast_state = fast_state;
356 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
357
358 for (uptr addr_end = addr + size; addr < addr_end; addr += step) {
359 u64 *shadow_mem = (u64*)MemToShadow(addr);
360 Shadow cur(fast_state);
361 cur.SetWrite(is_write);
362 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kSizeLog1);
363 MemoryAccessImpl(thr, addr, kSizeLog1, is_write, false,
364 shadow_mem, cur);
365 }
366}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000367} // namespace __tsan