blob: 6756bd317ea3b1e8d3d61969b70a48eb47232ee3 [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
Dmitry Vyukovdc36d692012-07-16 16:03:16 +000023#ifndef TSAN_GO
Dmitry Vyukovf6985e32012-05-22 14:34:43 +000024const int kThreadQuarantineSize = 16;
Dmitry Vyukovdc36d692012-07-16 16:03:16 +000025#else
26const int kThreadQuarantineSize = 64;
27#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000028
29static void MaybeReportThreadLeak(ThreadContext *tctx) {
30 if (tctx->detached)
31 return;
32 if (tctx->status != ThreadStatusCreated
33 && tctx->status != ThreadStatusRunning
34 && tctx->status != ThreadStatusFinished)
35 return;
36 ScopedReport rep(ReportTypeThreadLeak);
37 rep.AddThread(tctx);
38 OutputReport(rep);
39}
40
41void ThreadFinalize(ThreadState *thr) {
42 CHECK_GT(thr->in_rtl, 0);
43 if (!flags()->report_thread_leaks)
44 return;
45 Context *ctx = CTX();
46 Lock l(&ctx->thread_mtx);
Kostya Serebryany07c48052012-05-11 14:42:24 +000047 for (unsigned i = 0; i < kMaxTid; i++) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000048 ThreadContext *tctx = ctx->threads[i];
49 if (tctx == 0)
50 continue;
51 MaybeReportThreadLeak(tctx);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000052 }
53}
54
55static void ThreadDead(ThreadState *thr, ThreadContext *tctx) {
56 Context *ctx = CTX();
57 CHECK_GT(thr->in_rtl, 0);
58 CHECK(tctx->status == ThreadStatusRunning
59 || tctx->status == ThreadStatusFinished);
Alexey Samsonov51ae9832012-06-06 13:11:29 +000060 DPrintf("#%d: ThreadDead uid=%zu\n", thr->tid, tctx->user_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000061 tctx->status = ThreadStatusDead;
62 tctx->user_id = 0;
63 tctx->sync.Reset();
64
65 // Put to dead list.
66 tctx->dead_next = 0;
67 if (ctx->dead_list_size == 0)
68 ctx->dead_list_head = tctx;
69 else
70 ctx->dead_list_tail->dead_next = tctx;
71 ctx->dead_list_tail = tctx;
72 ctx->dead_list_size++;
73}
74
75int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
76 CHECK_GT(thr->in_rtl, 0);
77 Context *ctx = CTX();
78 Lock l(&ctx->thread_mtx);
79 StatInc(thr, StatThreadCreate);
80 int tid = -1;
81 ThreadContext *tctx = 0;
82 if (ctx->dead_list_size > kThreadQuarantineSize
83 || ctx->thread_seq >= kMaxTid) {
84 if (ctx->dead_list_size == 0) {
Alexey Samsonovac4c2902012-06-06 10:13:27 +000085 TsanPrintf("ThreadSanitizer: %d thread limit exceeded. Dying.\n",
86 kMaxTid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000087 Die();
88 }
89 StatInc(thr, StatThreadReuse);
90 tctx = ctx->dead_list_head;
91 ctx->dead_list_head = tctx->dead_next;
92 ctx->dead_list_size--;
93 if (ctx->dead_list_size == 0) {
94 CHECK_EQ(tctx->dead_next, 0);
95 ctx->dead_list_head = 0;
96 }
97 CHECK_EQ(tctx->status, ThreadStatusDead);
98 tctx->status = ThreadStatusInvalid;
99 tctx->reuse_count++;
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000100 tctx->sync.Reset();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000101 tid = tctx->tid;
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000102 DestroyAndFree(tctx->dead_info);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000103 } else {
104 StatInc(thr, StatThreadMaxTid);
105 tid = ctx->thread_seq++;
106 void *mem = internal_alloc(MBlockThreadContex, sizeof(ThreadContext));
107 tctx = new(mem) ThreadContext(tid);
108 ctx->threads[tid] = tctx;
109 }
110 CHECK_NE(tctx, 0);
111 CHECK_GE(tid, 0);
112 CHECK_LT(tid, kMaxTid);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000113 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000114 CHECK_EQ(tctx->status, ThreadStatusInvalid);
115 ctx->alive_threads++;
116 if (ctx->max_alive_threads < ctx->alive_threads) {
117 ctx->max_alive_threads++;
118 CHECK_EQ(ctx->max_alive_threads, ctx->alive_threads);
119 StatInc(thr, StatThreadMaxAlive);
120 }
121 tctx->status = ThreadStatusCreated;
122 tctx->thr = 0;
123 tctx->user_id = uid;
124 tctx->unique_id = ctx->unique_thread_seq++;
125 tctx->detached = detached;
126 if (tid) {
127 thr->fast_state.IncrementEpoch();
128 // Can't increment epoch w/o writing to the trace as well.
129 TraceAddEvent(thr, thr->fast_state.epoch(), 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(&tctx->sync);
133 StatInc(thr, StatSyncRelease);
134
135 tctx->creation_stack.ObtainCurrent(thr, pc);
136 }
137 return tid;
138}
139
140void ThreadStart(ThreadState *thr, int tid) {
141 CHECK_GT(thr->in_rtl, 0);
142 uptr stk_addr = 0;
143 uptr stk_size = 0;
144 uptr tls_addr = 0;
145 uptr tls_size = 0;
Dmitry Vyukov7339eb12012-05-25 11:15:04 +0000146 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000147
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000148 if (tid) {
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000149 if (stk_addr && stk_size) {
150 MemoryResetRange(thr, /*pc=*/ 1, stk_addr, stk_size);
151 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000152
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000153 if (tls_addr && tls_size) {
154 // Check that the thr object is in tls;
155 const uptr thr_beg = (uptr)thr;
156 const uptr thr_end = (uptr)thr + sizeof(*thr);
157 CHECK_GE(thr_beg, tls_addr);
158 CHECK_LE(thr_beg, tls_addr + tls_size);
159 CHECK_GE(thr_end, tls_addr);
160 CHECK_LE(thr_end, tls_addr + tls_size);
161 // Since the thr object is huge, skip it.
162 MemoryResetRange(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
163 MemoryResetRange(thr, /*pc=*/ 2, thr_end, tls_addr + tls_size - thr_end);
164 }
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000165 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000166
167 Lock l(&CTX()->thread_mtx);
168 ThreadContext *tctx = CTX()->threads[tid];
169 CHECK_NE(tctx, 0);
170 CHECK_EQ(tctx->status, ThreadStatusCreated);
171 tctx->status = ThreadStatusRunning;
172 tctx->epoch0 = tctx->epoch1 + 1;
173 tctx->epoch1 = (u64)-1;
174 new(thr) ThreadState(CTX(), tid, tctx->epoch0, stk_addr, stk_size,
175 tls_addr, tls_size);
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000176#ifdef TSAN_GO
177 // Setup dynamic shadow stack.
178 const int kInitStackSize = 8;
179 thr->shadow_stack = (uptr*)internal_alloc(MBlockShadowStack,
180 kInitStackSize * sizeof(uptr));
181 thr->shadow_stack_pos = thr->shadow_stack;
182 thr->shadow_stack_end = thr->shadow_stack + kInitStackSize;
183#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000184 tctx->thr = thr;
185 thr->fast_synch_epoch = tctx->epoch0;
186 thr->clock.set(tid, tctx->epoch0);
187 thr->clock.acquire(&tctx->sync);
188 StatInc(thr, StatSyncAcquire);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000189 DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx "
190 "tls_addr=%zx tls_size=%zx\n",
191 tid, (uptr)tctx->epoch0, stk_addr, stk_size, tls_addr, tls_size);
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000192 thr->is_alive = true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000193}
194
195void ThreadFinish(ThreadState *thr) {
196 CHECK_GT(thr->in_rtl, 0);
197 StatInc(thr, StatThreadFinish);
198 // FIXME: Treat it as write.
199 if (thr->stk_addr && thr->stk_size)
200 MemoryResetRange(thr, /*pc=*/ 3, thr->stk_addr, thr->stk_size);
201 if (thr->tls_addr && thr->tls_size) {
202 const uptr thr_beg = (uptr)thr;
203 const uptr thr_end = (uptr)thr + sizeof(*thr);
204 // Since the thr object is huge, skip it.
205 MemoryResetRange(thr, /*pc=*/ 4, thr->tls_addr, thr_beg - thr->tls_addr);
206 MemoryResetRange(thr, /*pc=*/ 5,
207 thr_end, thr->tls_addr + thr->tls_size - thr_end);
208 }
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000209 thr->is_alive = false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000210 Context *ctx = CTX();
211 Lock l(&ctx->thread_mtx);
212 ThreadContext *tctx = ctx->threads[thr->tid];
213 CHECK_NE(tctx, 0);
214 CHECK_EQ(tctx->status, ThreadStatusRunning);
215 CHECK_GT(ctx->alive_threads, 0);
216 ctx->alive_threads--;
217 if (tctx->detached) {
218 ThreadDead(thr, tctx);
219 } else {
220 thr->fast_state.IncrementEpoch();
221 // Can't increment epoch w/o writing to the trace as well.
222 TraceAddEvent(thr, thr->fast_state.epoch(), EventTypeMop, 0);
223 thr->clock.set(thr->tid, thr->fast_state.epoch());
224 thr->fast_synch_epoch = thr->fast_state.epoch();
225 thr->clock.release(&tctx->sync);
226 StatInc(thr, StatSyncRelease);
227 tctx->status = ThreadStatusFinished;
228 }
229
230 // Save from info about the thread.
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000231 tctx->dead_info = new(internal_alloc(MBlockDeadInfo, sizeof(ThreadDeadInfo)))
232 ThreadDeadInfo();
Dmitry Vyukov090f3452012-06-27 21:00:23 +0000233 internal_memcpy(&tctx->dead_info->trace.events[0],
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000234 &thr->trace.events[0], sizeof(thr->trace.events));
235 for (int i = 0; i < kTraceParts; i++) {
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000236 tctx->dead_info->trace.headers[i].stack0.CopyFrom(
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000237 thr->trace.headers[i].stack0);
238 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000239 tctx->epoch1 = thr->fast_state.epoch();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000240
241 thr->~ThreadState();
242 StatAggregate(ctx->stat, thr->stat);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000243 tctx->thr = 0;
244}
245
246int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
247 CHECK_GT(thr->in_rtl, 0);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000248 Context *ctx = CTX();
249 Lock l(&ctx->thread_mtx);
250 int res = -1;
Kostya Serebryany07c48052012-05-11 14:42:24 +0000251 for (unsigned tid = 0; tid < kMaxTid; tid++) {
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000252 ThreadContext *tctx = ctx->threads[tid];
253 if (tctx != 0 && tctx->user_id == uid
254 && tctx->status != ThreadStatusInvalid) {
255 tctx->user_id = 0;
256 res = tid;
257 break;
258 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000259 }
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000260 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000261 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000262}
263
264void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
265 CHECK_GT(thr->in_rtl, 0);
266 CHECK_GT(tid, 0);
267 CHECK_LT(tid, kMaxTid);
268 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
269 Context *ctx = CTX();
270 Lock l(&ctx->thread_mtx);
271 ThreadContext *tctx = ctx->threads[tid];
272 if (tctx->status == ThreadStatusInvalid) {
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000273 TsanPrintf("ThreadSanitizer: join of non-existent thread\n");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000274 return;
275 }
276 CHECK_EQ(tctx->detached, false);
277 CHECK_EQ(tctx->status, ThreadStatusFinished);
278 thr->clock.acquire(&tctx->sync);
279 StatInc(thr, StatSyncAcquire);
280 ThreadDead(thr, tctx);
281}
282
283void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
284 CHECK_GT(thr->in_rtl, 0);
285 CHECK_GT(tid, 0);
286 CHECK_LT(tid, kMaxTid);
287 Context *ctx = CTX();
288 Lock l(&ctx->thread_mtx);
289 ThreadContext *tctx = ctx->threads[tid];
290 if (tctx->status == ThreadStatusInvalid) {
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000291 TsanPrintf("ThreadSanitizer: detach of non-existent thread\n");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000292 return;
293 }
294 if (tctx->status == ThreadStatusFinished) {
295 ThreadDead(thr, tctx);
296 } else {
297 tctx->detached = true;
298 }
299}
300
301void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
302 uptr size, bool is_write) {
303 if (size == 0)
304 return;
305
306 u64 *shadow_mem = (u64*)MemToShadow(addr);
307 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
308 thr->tid, (void*)pc, (void*)addr,
309 (int)size, is_write);
310
311#if TSAN_DEBUG
312 if (!IsAppMem(addr)) {
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000313 TsanPrintf("Access to non app mem %zx\n", addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000314 DCHECK(IsAppMem(addr));
315 }
316 if (!IsAppMem(addr + size - 1)) {
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000317 TsanPrintf("Access to non app mem %zx\n", addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000318 DCHECK(IsAppMem(addr + size - 1));
319 }
320 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000321 TsanPrintf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000322 DCHECK(IsShadowMem((uptr)shadow_mem));
323 }
324 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000325 TsanPrintf("Bad shadow addr %p (%zx)\n",
326 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000327 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
328 }
329#endif
330
331 StatInc(thr, StatMopRange);
332
333 FastState fast_state = thr->fast_state;
334 if (fast_state.GetIgnoreBit())
335 return;
336
337 fast_state.IncrementEpoch();
338 thr->fast_state = fast_state;
339 TraceAddEvent(thr, fast_state.epoch(), EventTypeMop, pc);
340
341 bool unaligned = (addr % kShadowCell) != 0;
342
343 // Handle unaligned beginning, if any.
344 for (; addr % kShadowCell && size; addr++, size--) {
345 int const kAccessSizeLog = 0;
346 Shadow cur(fast_state);
347 cur.SetWrite(is_write);
348 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
349 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, fast_state,
350 shadow_mem, cur);
351 }
352 if (unaligned)
353 shadow_mem += kShadowCnt;
354 // Handle middle part, if any.
355 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
356 int const kAccessSizeLog = 3;
357 Shadow cur(fast_state);
358 cur.SetWrite(is_write);
359 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
360 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, fast_state,
361 shadow_mem, cur);
362 shadow_mem += kShadowCnt;
363 }
364 // Handle ending, if any.
365 for (; size; addr++, size--) {
366 int const kAccessSizeLog = 0;
367 Shadow cur(fast_state);
368 cur.SetWrite(is_write);
369 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
370 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, fast_state,
371 shadow_mem, cur);
372 }
373}
374
375void MemoryRead1Byte(ThreadState *thr, uptr pc, uptr addr) {
376 MemoryAccess(thr, pc, addr, 0, 0);
377}
378
379void MemoryWrite1Byte(ThreadState *thr, uptr pc, uptr addr) {
380 MemoryAccess(thr, pc, addr, 0, 1);
381}
382
383void MemoryRead8Byte(ThreadState *thr, uptr pc, uptr addr) {
384 MemoryAccess(thr, pc, addr, 3, 0);
385}
386
387void MemoryWrite8Byte(ThreadState *thr, uptr pc, uptr addr) {
388 MemoryAccess(thr, pc, addr, 3, 1);
389}
390} // namespace __tsan