blob: 42cd523480c2f2e8ac3be0603ab4f64348fd131e [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 Vyukovf6985e32012-05-22 14:34:43 +000023const int kThreadQuarantineSize = 16;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000024
25static void MaybeReportThreadLeak(ThreadContext *tctx) {
26 if (tctx->detached)
27 return;
28 if (tctx->status != ThreadStatusCreated
29 && tctx->status != ThreadStatusRunning
30 && tctx->status != ThreadStatusFinished)
31 return;
32 ScopedReport rep(ReportTypeThreadLeak);
33 rep.AddThread(tctx);
34 OutputReport(rep);
35}
36
37void ThreadFinalize(ThreadState *thr) {
38 CHECK_GT(thr->in_rtl, 0);
39 if (!flags()->report_thread_leaks)
40 return;
41 Context *ctx = CTX();
42 Lock l(&ctx->thread_mtx);
Kostya Serebryany07c48052012-05-11 14:42:24 +000043 for (unsigned i = 0; i < kMaxTid; i++) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000044 ThreadContext *tctx = ctx->threads[i];
45 if (tctx == 0)
46 continue;
47 MaybeReportThreadLeak(tctx);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000048 }
49}
50
51static void ThreadDead(ThreadState *thr, ThreadContext *tctx) {
52 Context *ctx = CTX();
53 CHECK_GT(thr->in_rtl, 0);
54 CHECK(tctx->status == ThreadStatusRunning
55 || tctx->status == ThreadStatusFinished);
Alexey Samsonov51ae9832012-06-06 13:11:29 +000056 DPrintf("#%d: ThreadDead uid=%zu\n", thr->tid, tctx->user_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000057 tctx->status = ThreadStatusDead;
58 tctx->user_id = 0;
59 tctx->sync.Reset();
60
61 // Put to dead list.
62 tctx->dead_next = 0;
63 if (ctx->dead_list_size == 0)
64 ctx->dead_list_head = tctx;
65 else
66 ctx->dead_list_tail->dead_next = tctx;
67 ctx->dead_list_tail = tctx;
68 ctx->dead_list_size++;
69}
70
71int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
72 CHECK_GT(thr->in_rtl, 0);
73 Context *ctx = CTX();
74 Lock l(&ctx->thread_mtx);
75 StatInc(thr, StatThreadCreate);
76 int tid = -1;
77 ThreadContext *tctx = 0;
78 if (ctx->dead_list_size > kThreadQuarantineSize
79 || ctx->thread_seq >= kMaxTid) {
80 if (ctx->dead_list_size == 0) {
Alexey Samsonovac4c2902012-06-06 10:13:27 +000081 TsanPrintf("ThreadSanitizer: %d thread limit exceeded. Dying.\n",
82 kMaxTid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000083 Die();
84 }
85 StatInc(thr, StatThreadReuse);
86 tctx = ctx->dead_list_head;
87 ctx->dead_list_head = tctx->dead_next;
88 ctx->dead_list_size--;
89 if (ctx->dead_list_size == 0) {
90 CHECK_EQ(tctx->dead_next, 0);
91 ctx->dead_list_head = 0;
92 }
93 CHECK_EQ(tctx->status, ThreadStatusDead);
94 tctx->status = ThreadStatusInvalid;
95 tctx->reuse_count++;
Dmitry Vyukov302cebb2012-05-22 18:07:45 +000096 tctx->sync.Reset();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000097 tid = tctx->tid;
Dmitry Vyukovf6985e32012-05-22 14:34:43 +000098 DestroyAndFree(tctx->dead_info);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000099 } else {
100 StatInc(thr, StatThreadMaxTid);
101 tid = ctx->thread_seq++;
102 void *mem = internal_alloc(MBlockThreadContex, sizeof(ThreadContext));
103 tctx = new(mem) ThreadContext(tid);
104 ctx->threads[tid] = tctx;
105 }
106 CHECK_NE(tctx, 0);
107 CHECK_GE(tid, 0);
108 CHECK_LT(tid, kMaxTid);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000109 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000110 CHECK_EQ(tctx->status, ThreadStatusInvalid);
111 ctx->alive_threads++;
112 if (ctx->max_alive_threads < ctx->alive_threads) {
113 ctx->max_alive_threads++;
114 CHECK_EQ(ctx->max_alive_threads, ctx->alive_threads);
115 StatInc(thr, StatThreadMaxAlive);
116 }
117 tctx->status = ThreadStatusCreated;
118 tctx->thr = 0;
119 tctx->user_id = uid;
120 tctx->unique_id = ctx->unique_thread_seq++;
121 tctx->detached = detached;
122 if (tid) {
123 thr->fast_state.IncrementEpoch();
124 // Can't increment epoch w/o writing to the trace as well.
125 TraceAddEvent(thr, thr->fast_state.epoch(), EventTypeMop, 0);
126 thr->clock.set(thr->tid, thr->fast_state.epoch());
127 thr->fast_synch_epoch = thr->fast_state.epoch();
128 thr->clock.release(&tctx->sync);
129 StatInc(thr, StatSyncRelease);
130
131 tctx->creation_stack.ObtainCurrent(thr, pc);
132 }
133 return tid;
134}
135
136void ThreadStart(ThreadState *thr, int tid) {
137 CHECK_GT(thr->in_rtl, 0);
138 uptr stk_addr = 0;
139 uptr stk_size = 0;
140 uptr tls_addr = 0;
141 uptr tls_size = 0;
Dmitry Vyukov7339eb12012-05-25 11:15:04 +0000142 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000143
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000144 if (tid) {
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000145 if (stk_addr && stk_size) {
146 MemoryResetRange(thr, /*pc=*/ 1, stk_addr, stk_size);
147 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000148
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000149 if (tls_addr && tls_size) {
150 // Check that the thr object is in tls;
151 const uptr thr_beg = (uptr)thr;
152 const uptr thr_end = (uptr)thr + sizeof(*thr);
153 CHECK_GE(thr_beg, tls_addr);
154 CHECK_LE(thr_beg, tls_addr + tls_size);
155 CHECK_GE(thr_end, tls_addr);
156 CHECK_LE(thr_end, tls_addr + tls_size);
157 // Since the thr object is huge, skip it.
158 MemoryResetRange(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
159 MemoryResetRange(thr, /*pc=*/ 2, thr_end, tls_addr + tls_size - thr_end);
160 }
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000161 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000162
163 Lock l(&CTX()->thread_mtx);
164 ThreadContext *tctx = CTX()->threads[tid];
165 CHECK_NE(tctx, 0);
166 CHECK_EQ(tctx->status, ThreadStatusCreated);
167 tctx->status = ThreadStatusRunning;
168 tctx->epoch0 = tctx->epoch1 + 1;
169 tctx->epoch1 = (u64)-1;
170 new(thr) ThreadState(CTX(), tid, tctx->epoch0, stk_addr, stk_size,
171 tls_addr, tls_size);
172 tctx->thr = thr;
173 thr->fast_synch_epoch = tctx->epoch0;
174 thr->clock.set(tid, tctx->epoch0);
175 thr->clock.acquire(&tctx->sync);
176 StatInc(thr, StatSyncAcquire);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000177 DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx "
178 "tls_addr=%zx tls_size=%zx\n",
179 tid, (uptr)tctx->epoch0, stk_addr, stk_size, tls_addr, tls_size);
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000180 thr->is_alive = true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000181}
182
183void ThreadFinish(ThreadState *thr) {
184 CHECK_GT(thr->in_rtl, 0);
185 StatInc(thr, StatThreadFinish);
186 // FIXME: Treat it as write.
187 if (thr->stk_addr && thr->stk_size)
188 MemoryResetRange(thr, /*pc=*/ 3, thr->stk_addr, thr->stk_size);
189 if (thr->tls_addr && thr->tls_size) {
190 const uptr thr_beg = (uptr)thr;
191 const uptr thr_end = (uptr)thr + sizeof(*thr);
192 // Since the thr object is huge, skip it.
193 MemoryResetRange(thr, /*pc=*/ 4, thr->tls_addr, thr_beg - thr->tls_addr);
194 MemoryResetRange(thr, /*pc=*/ 5,
195 thr_end, thr->tls_addr + thr->tls_size - thr_end);
196 }
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000197 thr->is_alive = false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000198 Context *ctx = CTX();
199 Lock l(&ctx->thread_mtx);
200 ThreadContext *tctx = ctx->threads[thr->tid];
201 CHECK_NE(tctx, 0);
202 CHECK_EQ(tctx->status, ThreadStatusRunning);
203 CHECK_GT(ctx->alive_threads, 0);
204 ctx->alive_threads--;
205 if (tctx->detached) {
206 ThreadDead(thr, tctx);
207 } else {
208 thr->fast_state.IncrementEpoch();
209 // Can't increment epoch w/o writing to the trace as well.
210 TraceAddEvent(thr, thr->fast_state.epoch(), EventTypeMop, 0);
211 thr->clock.set(thr->tid, thr->fast_state.epoch());
212 thr->fast_synch_epoch = thr->fast_state.epoch();
213 thr->clock.release(&tctx->sync);
214 StatInc(thr, StatSyncRelease);
215 tctx->status = ThreadStatusFinished;
216 }
217
218 // Save from info about the thread.
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000219 tctx->dead_info = new(internal_alloc(MBlockDeadInfo, sizeof(ThreadDeadInfo)))
220 ThreadDeadInfo();
Dmitry Vyukov090f3452012-06-27 21:00:23 +0000221 internal_memcpy(&tctx->dead_info->trace.events[0],
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000222 &thr->trace.events[0], sizeof(thr->trace.events));
223 for (int i = 0; i < kTraceParts; i++) {
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000224 tctx->dead_info->trace.headers[i].stack0.CopyFrom(
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000225 thr->trace.headers[i].stack0);
226 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000227 tctx->epoch1 = thr->fast_state.epoch();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000228
229 thr->~ThreadState();
230 StatAggregate(ctx->stat, thr->stat);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000231 tctx->thr = 0;
232}
233
234int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
235 CHECK_GT(thr->in_rtl, 0);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000236 Context *ctx = CTX();
237 Lock l(&ctx->thread_mtx);
238 int res = -1;
Kostya Serebryany07c48052012-05-11 14:42:24 +0000239 for (unsigned tid = 0; tid < kMaxTid; tid++) {
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000240 ThreadContext *tctx = ctx->threads[tid];
241 if (tctx != 0 && tctx->user_id == uid
242 && tctx->status != ThreadStatusInvalid) {
243 tctx->user_id = 0;
244 res = tid;
245 break;
246 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000247 }
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000248 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000249 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000250}
251
252void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
253 CHECK_GT(thr->in_rtl, 0);
254 CHECK_GT(tid, 0);
255 CHECK_LT(tid, kMaxTid);
256 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
257 Context *ctx = CTX();
258 Lock l(&ctx->thread_mtx);
259 ThreadContext *tctx = ctx->threads[tid];
260 if (tctx->status == ThreadStatusInvalid) {
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000261 TsanPrintf("ThreadSanitizer: join of non-existent thread\n");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000262 return;
263 }
264 CHECK_EQ(tctx->detached, false);
265 CHECK_EQ(tctx->status, ThreadStatusFinished);
266 thr->clock.acquire(&tctx->sync);
267 StatInc(thr, StatSyncAcquire);
268 ThreadDead(thr, tctx);
269}
270
271void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
272 CHECK_GT(thr->in_rtl, 0);
273 CHECK_GT(tid, 0);
274 CHECK_LT(tid, kMaxTid);
275 Context *ctx = CTX();
276 Lock l(&ctx->thread_mtx);
277 ThreadContext *tctx = ctx->threads[tid];
278 if (tctx->status == ThreadStatusInvalid) {
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000279 TsanPrintf("ThreadSanitizer: detach of non-existent thread\n");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000280 return;
281 }
282 if (tctx->status == ThreadStatusFinished) {
283 ThreadDead(thr, tctx);
284 } else {
285 tctx->detached = true;
286 }
287}
288
289void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
290 uptr size, bool is_write) {
291 if (size == 0)
292 return;
293
294 u64 *shadow_mem = (u64*)MemToShadow(addr);
295 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
296 thr->tid, (void*)pc, (void*)addr,
297 (int)size, is_write);
298
299#if TSAN_DEBUG
300 if (!IsAppMem(addr)) {
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000301 TsanPrintf("Access to non app mem %zx\n", addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000302 DCHECK(IsAppMem(addr));
303 }
304 if (!IsAppMem(addr + size - 1)) {
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000305 TsanPrintf("Access to non app mem %zx\n", addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000306 DCHECK(IsAppMem(addr + size - 1));
307 }
308 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000309 TsanPrintf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000310 DCHECK(IsShadowMem((uptr)shadow_mem));
311 }
312 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000313 TsanPrintf("Bad shadow addr %p (%zx)\n",
314 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000315 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
316 }
317#endif
318
319 StatInc(thr, StatMopRange);
320
321 FastState fast_state = thr->fast_state;
322 if (fast_state.GetIgnoreBit())
323 return;
324
325 fast_state.IncrementEpoch();
326 thr->fast_state = fast_state;
327 TraceAddEvent(thr, fast_state.epoch(), EventTypeMop, pc);
328
329 bool unaligned = (addr % kShadowCell) != 0;
330
331 // Handle unaligned beginning, if any.
332 for (; addr % kShadowCell && size; addr++, size--) {
333 int const kAccessSizeLog = 0;
334 Shadow cur(fast_state);
335 cur.SetWrite(is_write);
336 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
337 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, fast_state,
338 shadow_mem, cur);
339 }
340 if (unaligned)
341 shadow_mem += kShadowCnt;
342 // Handle middle part, if any.
343 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
344 int const kAccessSizeLog = 3;
345 Shadow cur(fast_state);
346 cur.SetWrite(is_write);
347 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
348 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, fast_state,
349 shadow_mem, cur);
350 shadow_mem += kShadowCnt;
351 }
352 // Handle ending, if any.
353 for (; size; addr++, size--) {
354 int const kAccessSizeLog = 0;
355 Shadow cur(fast_state);
356 cur.SetWrite(is_write);
357 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
358 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, fast_state,
359 shadow_mem, cur);
360 }
361}
362
363void MemoryRead1Byte(ThreadState *thr, uptr pc, uptr addr) {
364 MemoryAccess(thr, pc, addr, 0, 0);
365}
366
367void MemoryWrite1Byte(ThreadState *thr, uptr pc, uptr addr) {
368 MemoryAccess(thr, pc, addr, 0, 1);
369}
370
371void MemoryRead8Byte(ThreadState *thr, uptr pc, uptr addr) {
372 MemoryAccess(thr, pc, addr, 3, 0);
373}
374
375void MemoryWrite8Byte(ThreadState *thr, uptr pc, uptr addr) {
376 MemoryAccess(thr, pc, addr, 3, 1);
377}
378} // namespace __tsan