blob: c19b9bf5e509c18daa392af314cf4a075123bf12 [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",
Alexey Samsonovcbed82e2013-03-18 07:33:00 +0000107 tid, (uptr)epoch0, args->stk_addr, args->stk_size,
108 args->tls_addr, args->tls_size);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000109 thr->is_alive = true;
110}
111
112void ThreadContext::OnFinished() {
113 if (!detached) {
114 thr->fast_state.IncrementEpoch();
115 // Can't increment epoch w/o writing to the trace as well.
116 TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
117 thr->clock.set(thr->tid, thr->fast_state.epoch());
118 thr->fast_synch_epoch = thr->fast_state.epoch();
119 thr->clock.release(&sync);
120 StatInc(thr, StatSyncRelease);
121 }
122 // Save from info about the thread.
123 dead_info = new(internal_alloc(MBlockDeadInfo, sizeof(ThreadDeadInfo)))
124 ThreadDeadInfo();
125 for (uptr i = 0; i < TraceParts(); i++) {
126 dead_info->trace.headers[i].epoch0 = thr->trace.headers[i].epoch0;
127 dead_info->trace.headers[i].stack0.CopyFrom(
128 thr->trace.headers[i].stack0);
129 }
130 epoch1 = thr->fast_state.epoch();
131
132#ifndef TSAN_GO
133 AllocatorThreadFinish(thr);
134#endif
135 thr->~ThreadState();
136 StatAggregate(CTX()->stat, thr->stat);
137 thr = 0;
138}
139
140static void MaybeReportThreadLeak(ThreadContextBase *tctx_base, void *unused) {
141 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000142 if (tctx->detached)
143 return;
144 if (tctx->status != ThreadStatusCreated
145 && tctx->status != ThreadStatusRunning
146 && tctx->status != ThreadStatusFinished)
147 return;
148 ScopedReport rep(ReportTypeThreadLeak);
149 rep.AddThread(tctx);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000150 OutputReport(CTX(), rep);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000151}
152
153void ThreadFinalize(ThreadState *thr) {
154 CHECK_GT(thr->in_rtl, 0);
155 if (!flags()->report_thread_leaks)
156 return;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000157 ThreadRegistryLock l(CTX()->thread_registry);
158 CTX()->thread_registry->RunCallbackForEachThreadLocked(
159 MaybeReportThreadLeak, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000160}
161
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000162int ThreadCount(ThreadState *thr) {
163 CHECK_GT(thr->in_rtl, 0);
164 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000165 uptr result;
166 ctx->thread_registry->GetNumberOfThreads(0, 0, &result);
167 return (int)result;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000168}
169
170int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
171 CHECK_GT(thr->in_rtl, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000172 StatInc(thr, StatThreadCreate);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000173 Context *ctx = CTX();
174 OnCreatedArgs args = { thr, pc };
175 int tid = ctx->thread_registry->CreateThread(uid, detached, thr->tid, &args);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000176 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000177 StatSet(thr, StatThreadMaxAlive, ctx->thread_registry->GetMaxAliveThreads());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000178 return tid;
179}
180
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000181void ThreadStart(ThreadState *thr, int tid, uptr os_id) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000182 CHECK_GT(thr->in_rtl, 0);
183 uptr stk_addr = 0;
184 uptr stk_size = 0;
185 uptr tls_addr = 0;
186 uptr tls_size = 0;
Dmitry Vyukov7339eb12012-05-25 11:15:04 +0000187 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000188
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000189 if (tid) {
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000190 if (stk_addr && stk_size) {
191 MemoryResetRange(thr, /*pc=*/ 1, stk_addr, stk_size);
192 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000193
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000194 if (tls_addr && tls_size) {
195 // Check that the thr object is in tls;
196 const uptr thr_beg = (uptr)thr;
197 const uptr thr_end = (uptr)thr + sizeof(*thr);
198 CHECK_GE(thr_beg, tls_addr);
199 CHECK_LE(thr_beg, tls_addr + tls_size);
200 CHECK_GE(thr_end, tls_addr);
201 CHECK_LE(thr_end, tls_addr + tls_size);
202 // Since the thr object is huge, skip it.
203 MemoryResetRange(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
204 MemoryResetRange(thr, /*pc=*/ 2, thr_end, tls_addr + tls_size - thr_end);
205 }
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000206 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000207
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000208 OnStartedArgs args = { thr, stk_addr, stk_size, tls_addr, tls_size };
209 CTX()->thread_registry->StartThread(tid, os_id, &args);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000210}
211
212void ThreadFinish(ThreadState *thr) {
213 CHECK_GT(thr->in_rtl, 0);
214 StatInc(thr, StatThreadFinish);
215 // FIXME: Treat it as write.
216 if (thr->stk_addr && thr->stk_size)
217 MemoryResetRange(thr, /*pc=*/ 3, thr->stk_addr, thr->stk_size);
218 if (thr->tls_addr && thr->tls_size) {
219 const uptr thr_beg = (uptr)thr;
220 const uptr thr_end = (uptr)thr + sizeof(*thr);
221 // Since the thr object is huge, skip it.
222 MemoryResetRange(thr, /*pc=*/ 4, thr->tls_addr, thr_beg - thr->tls_addr);
223 MemoryResetRange(thr, /*pc=*/ 5,
224 thr_end, thr->tls_addr + thr->tls_size - thr_end);
225 }
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000226 thr->is_alive = false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000227 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000228 ctx->thread_registry->FinishThread(thr->tid);
229}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000230
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000231static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) {
232 uptr uid = (uptr)arg;
233 if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) {
234 tctx->user_id = 0;
235 return true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000236 }
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000237 return false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000238}
239
240int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
241 CHECK_GT(thr->in_rtl, 0);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000242 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000243 int res = ctx->thread_registry->FindThread(FindThreadByUid, (void*)uid);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000244 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000245 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000246}
247
248void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
249 CHECK_GT(thr->in_rtl, 0);
250 CHECK_GT(tid, 0);
251 CHECK_LT(tid, kMaxTid);
252 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
253 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000254 ctx->thread_registry->JoinThread(tid, thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000255}
256
257void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
258 CHECK_GT(thr->in_rtl, 0);
259 CHECK_GT(tid, 0);
260 CHECK_LT(tid, kMaxTid);
261 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000262 ctx->thread_registry->DetachThread(tid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000263}
264
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000265void ThreadSetName(ThreadState *thr, const char *name) {
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000266 CHECK_GT(thr->in_rtl, 0);
267 CTX()->thread_registry->SetThreadName(thr->tid, name);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000268}
269
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000270void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
271 uptr size, bool is_write) {
272 if (size == 0)
273 return;
274
275 u64 *shadow_mem = (u64*)MemToShadow(addr);
276 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
277 thr->tid, (void*)pc, (void*)addr,
278 (int)size, is_write);
279
280#if TSAN_DEBUG
281 if (!IsAppMem(addr)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000282 Printf("Access to non app mem %zx\n", addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000283 DCHECK(IsAppMem(addr));
284 }
285 if (!IsAppMem(addr + size - 1)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000286 Printf("Access to non app mem %zx\n", addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000287 DCHECK(IsAppMem(addr + size - 1));
288 }
289 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000290 Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000291 DCHECK(IsShadowMem((uptr)shadow_mem));
292 }
293 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000294 Printf("Bad shadow addr %p (%zx)\n",
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000295 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000296 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
297 }
298#endif
299
300 StatInc(thr, StatMopRange);
301
302 FastState fast_state = thr->fast_state;
303 if (fast_state.GetIgnoreBit())
304 return;
305
306 fast_state.IncrementEpoch();
307 thr->fast_state = fast_state;
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000308 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000309
310 bool unaligned = (addr % kShadowCell) != 0;
311
312 // Handle unaligned beginning, if any.
313 for (; addr % kShadowCell && size; addr++, size--) {
314 int const kAccessSizeLog = 0;
315 Shadow cur(fast_state);
316 cur.SetWrite(is_write);
317 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000318 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000319 shadow_mem, cur);
320 }
321 if (unaligned)
322 shadow_mem += kShadowCnt;
323 // Handle middle part, if any.
324 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
325 int const kAccessSizeLog = 3;
326 Shadow cur(fast_state);
327 cur.SetWrite(is_write);
328 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000329 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000330 shadow_mem, cur);
331 shadow_mem += kShadowCnt;
332 }
333 // Handle ending, if any.
334 for (; size; addr++, size--) {
335 int const kAccessSizeLog = 0;
336 Shadow cur(fast_state);
337 cur.SetWrite(is_write);
338 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000339 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000340 shadow_mem, cur);
341 }
342}
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000343
344void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr,
345 uptr size, uptr step, bool is_write) {
346 if (size == 0)
347 return;
348 FastState fast_state = thr->fast_state;
349 if (fast_state.GetIgnoreBit())
350 return;
351 StatInc(thr, StatMopRange);
352 fast_state.IncrementEpoch();
353 thr->fast_state = fast_state;
354 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
355
356 for (uptr addr_end = addr + size; addr < addr_end; addr += step) {
357 u64 *shadow_mem = (u64*)MemToShadow(addr);
358 Shadow cur(fast_state);
359 cur.SetWrite(is_write);
360 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kSizeLog1);
361 MemoryAccessImpl(thr, addr, kSizeLog1, is_write, false,
362 shadow_mem, cur);
363 }
364}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000365} // namespace __tsan