blob: b8748264206965ba27ed79ef8b2bf25caf6d04d5 [file] [log] [blame]
Kostya Serebryany4ad375f2012-05-10 13:48:04 +00001//===-- tsan_rtl_thread.cc --------------------------------------*- C++ -*-===//
2//
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
14#include "tsan_rtl.h"
15#include "tsan_mman.h"
16#include "tsan_placement_new.h"
17#include "tsan_platform.h"
18#include "tsan_report.h"
19#include "tsan_sync.h"
20
21namespace __tsan {
22
23const int kThreadQuarantineSize = 100;
24
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);
43 for (int i = 0; i < kMaxTid; i++) {
44 ThreadContext *tctx = ctx->threads[i];
45 if (tctx == 0)
46 continue;
47 MaybeReportThreadLeak(tctx);
48 DestroyAndFree(tctx);
49 ctx->threads[i] = 0;
50 }
51}
52
53static void ThreadDead(ThreadState *thr, ThreadContext *tctx) {
54 Context *ctx = CTX();
55 CHECK_GT(thr->in_rtl, 0);
56 CHECK(tctx->status == ThreadStatusRunning
57 || tctx->status == ThreadStatusFinished);
58 DPrintf("#%d: ThreadDead uid=%lu\n", thr->tid, tctx->user_id);
59 tctx->status = ThreadStatusDead;
60 tctx->user_id = 0;
61 tctx->sync.Reset();
62
63 // Put to dead list.
64 tctx->dead_next = 0;
65 if (ctx->dead_list_size == 0)
66 ctx->dead_list_head = tctx;
67 else
68 ctx->dead_list_tail->dead_next = tctx;
69 ctx->dead_list_tail = tctx;
70 ctx->dead_list_size++;
71}
72
73int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
74 CHECK_GT(thr->in_rtl, 0);
75 Context *ctx = CTX();
76 Lock l(&ctx->thread_mtx);
77 StatInc(thr, StatThreadCreate);
78 int tid = -1;
79 ThreadContext *tctx = 0;
80 if (ctx->dead_list_size > kThreadQuarantineSize
81 || ctx->thread_seq >= kMaxTid) {
82 if (ctx->dead_list_size == 0) {
83 Printf("ThreadSanitizer: %d thread limit exceeded. Dying.\n", kMaxTid);
84 Die();
85 }
86 StatInc(thr, StatThreadReuse);
87 tctx = ctx->dead_list_head;
88 ctx->dead_list_head = tctx->dead_next;
89 ctx->dead_list_size--;
90 if (ctx->dead_list_size == 0) {
91 CHECK_EQ(tctx->dead_next, 0);
92 ctx->dead_list_head = 0;
93 }
94 CHECK_EQ(tctx->status, ThreadStatusDead);
95 tctx->status = ThreadStatusInvalid;
96 tctx->reuse_count++;
97 tid = tctx->tid;
98 // The point to reclain dead_info.
99 // delete tctx->dead_info;
100 } else {
101 StatInc(thr, StatThreadMaxTid);
102 tid = ctx->thread_seq++;
103 void *mem = internal_alloc(MBlockThreadContex, sizeof(ThreadContext));
104 tctx = new(mem) ThreadContext(tid);
105 ctx->threads[tid] = tctx;
106 }
107 CHECK_NE(tctx, 0);
108 CHECK_GE(tid, 0);
109 CHECK_LT(tid, kMaxTid);
110 DPrintf("#%d: ThreadCreate tid=%d uid=%lu\n", thr->tid, tid, uid);
111 CHECK_EQ(tctx->status, ThreadStatusInvalid);
112 ctx->alive_threads++;
113 if (ctx->max_alive_threads < ctx->alive_threads) {
114 ctx->max_alive_threads++;
115 CHECK_EQ(ctx->max_alive_threads, ctx->alive_threads);
116 StatInc(thr, StatThreadMaxAlive);
117 }
118 tctx->status = ThreadStatusCreated;
119 tctx->thr = 0;
120 tctx->user_id = uid;
121 tctx->unique_id = ctx->unique_thread_seq++;
122 tctx->detached = detached;
123 if (tid) {
124 thr->fast_state.IncrementEpoch();
125 // Can't increment epoch w/o writing to the trace as well.
126 TraceAddEvent(thr, thr->fast_state.epoch(), EventTypeMop, 0);
127 thr->clock.set(thr->tid, thr->fast_state.epoch());
128 thr->fast_synch_epoch = thr->fast_state.epoch();
129 thr->clock.release(&tctx->sync);
130 StatInc(thr, StatSyncRelease);
131
132 tctx->creation_stack.ObtainCurrent(thr, pc);
133 }
134 return tid;
135}
136
137void ThreadStart(ThreadState *thr, int tid) {
138 CHECK_GT(thr->in_rtl, 0);
139 uptr stk_addr = 0;
140 uptr stk_size = 0;
141 uptr tls_addr = 0;
142 uptr tls_size = 0;
143 GetThreadStackAndTls(&stk_addr, &stk_size, &tls_addr, &tls_size);
144
145 MemoryResetRange(thr, /*pc=*/ 1, stk_addr, stk_size);
146
147 // Check that the thr object is in tls;
148 const uptr thr_beg = (uptr)thr;
149 const uptr thr_end = (uptr)thr + sizeof(*thr);
150 CHECK_GE(thr_beg, tls_addr);
151 CHECK_LE(thr_beg, tls_addr + tls_size);
152 CHECK_GE(thr_end, tls_addr);
153 CHECK_LE(thr_end, tls_addr + tls_size);
154 // Since the thr object is huge, skip it.
155 MemoryResetRange(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
156 MemoryResetRange(thr, /*pc=*/ 2, thr_end, tls_addr + tls_size - thr_end);
157
158 Lock l(&CTX()->thread_mtx);
159 ThreadContext *tctx = CTX()->threads[tid];
160 CHECK_NE(tctx, 0);
161 CHECK_EQ(tctx->status, ThreadStatusCreated);
162 tctx->status = ThreadStatusRunning;
163 tctx->epoch0 = tctx->epoch1 + 1;
164 tctx->epoch1 = (u64)-1;
165 new(thr) ThreadState(CTX(), tid, tctx->epoch0, stk_addr, stk_size,
166 tls_addr, tls_size);
167 tctx->thr = thr;
168 thr->fast_synch_epoch = tctx->epoch0;
169 thr->clock.set(tid, tctx->epoch0);
170 thr->clock.acquire(&tctx->sync);
171 StatInc(thr, StatSyncAcquire);
172 DPrintf("#%d: ThreadStart epoch=%llu stk_addr=%lx stk_size=%lx "
173 "tls_addr=%lx tls_size=%lx\n",
174 tid, tctx->epoch0, stk_addr, stk_size, tls_addr, tls_size);
175}
176
177void ThreadFinish(ThreadState *thr) {
178 CHECK_GT(thr->in_rtl, 0);
179 StatInc(thr, StatThreadFinish);
180 // FIXME: Treat it as write.
181 if (thr->stk_addr && thr->stk_size)
182 MemoryResetRange(thr, /*pc=*/ 3, thr->stk_addr, thr->stk_size);
183 if (thr->tls_addr && thr->tls_size) {
184 const uptr thr_beg = (uptr)thr;
185 const uptr thr_end = (uptr)thr + sizeof(*thr);
186 // Since the thr object is huge, skip it.
187 MemoryResetRange(thr, /*pc=*/ 4, thr->tls_addr, thr_beg - thr->tls_addr);
188 MemoryResetRange(thr, /*pc=*/ 5,
189 thr_end, thr->tls_addr + thr->tls_size - thr_end);
190 }
191 Context *ctx = CTX();
192 Lock l(&ctx->thread_mtx);
193 ThreadContext *tctx = ctx->threads[thr->tid];
194 CHECK_NE(tctx, 0);
195 CHECK_EQ(tctx->status, ThreadStatusRunning);
196 CHECK_GT(ctx->alive_threads, 0);
197 ctx->alive_threads--;
198 if (tctx->detached) {
199 ThreadDead(thr, tctx);
200 } else {
201 thr->fast_state.IncrementEpoch();
202 // Can't increment epoch w/o writing to the trace as well.
203 TraceAddEvent(thr, thr->fast_state.epoch(), EventTypeMop, 0);
204 thr->clock.set(thr->tid, thr->fast_state.epoch());
205 thr->fast_synch_epoch = thr->fast_state.epoch();
206 thr->clock.release(&tctx->sync);
207 StatInc(thr, StatSyncRelease);
208 tctx->status = ThreadStatusFinished;
209 }
210
211 // Save from info about the thread.
212 // If dead_info will become dynamically allocated again,
213 // it is the point to allocate it.
214 // tctx->dead_info = new ThreadDeadInfo;
215 internal_memcpy(&tctx->dead_info.trace.events[0],
216 &thr->trace.events[0], sizeof(thr->trace.events));
217 for (int i = 0; i < kTraceParts; i++) {
218 tctx->dead_info.trace.headers[i].stack0.CopyFrom(
219 thr->trace.headers[i].stack0);
220 }
221 tctx->epoch1 = thr->clock.get(tctx->tid);
222
223 thr->~ThreadState();
224 StatAggregate(ctx->stat, thr->stat);
225 InternalAllocStatAggregate(ctx, thr);
226 tctx->thr = 0;
227}
228
229int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
230 CHECK_GT(thr->in_rtl, 0);
231 DPrintf("#%d: ThreadTid uid=%lu\n", thr->tid, uid);
232 Lock l(&CTX()->thread_mtx);
233 for (int tid = 0; tid < kMaxTid; tid++) {
234 if (CTX()->threads[tid] != 0
235 && CTX()->threads[tid]->user_id == uid
236 && CTX()->threads[tid]->status != ThreadStatusInvalid)
237 return tid;
238 }
239 return -1;
240}
241
242void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
243 CHECK_GT(thr->in_rtl, 0);
244 CHECK_GT(tid, 0);
245 CHECK_LT(tid, kMaxTid);
246 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
247 Context *ctx = CTX();
248 Lock l(&ctx->thread_mtx);
249 ThreadContext *tctx = ctx->threads[tid];
250 if (tctx->status == ThreadStatusInvalid) {
251 Printf("ThreadSanitizer: join of non-existent thread\n");
252 return;
253 }
254 CHECK_EQ(tctx->detached, false);
255 CHECK_EQ(tctx->status, ThreadStatusFinished);
256 thr->clock.acquire(&tctx->sync);
257 StatInc(thr, StatSyncAcquire);
258 ThreadDead(thr, tctx);
259}
260
261void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
262 CHECK_GT(thr->in_rtl, 0);
263 CHECK_GT(tid, 0);
264 CHECK_LT(tid, kMaxTid);
265 Context *ctx = CTX();
266 Lock l(&ctx->thread_mtx);
267 ThreadContext *tctx = ctx->threads[tid];
268 if (tctx->status == ThreadStatusInvalid) {
269 Printf("ThreadSanitizer: detach of non-existent thread\n");
270 return;
271 }
272 if (tctx->status == ThreadStatusFinished) {
273 ThreadDead(thr, tctx);
274 } else {
275 tctx->detached = true;
276 }
277}
278
279void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
280 uptr size, bool is_write) {
281 if (size == 0)
282 return;
283
284 u64 *shadow_mem = (u64*)MemToShadow(addr);
285 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
286 thr->tid, (void*)pc, (void*)addr,
287 (int)size, is_write);
288
289#if TSAN_DEBUG
290 if (!IsAppMem(addr)) {
291 Printf("Access to non app mem %lx\n", addr);
292 DCHECK(IsAppMem(addr));
293 }
294 if (!IsAppMem(addr + size - 1)) {
295 Printf("Access to non app mem %lx\n", addr + size - 1);
296 DCHECK(IsAppMem(addr + size - 1));
297 }
298 if (!IsShadowMem((uptr)shadow_mem)) {
299 Printf("Bad shadow addr %p (%lx)\n", shadow_mem, addr);
300 DCHECK(IsShadowMem((uptr)shadow_mem));
301 }
302 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
303 Printf("Bad shadow addr %p (%lx)\n",
304 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
305 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
306 }
307#endif
308
309 StatInc(thr, StatMopRange);
310
311 FastState fast_state = thr->fast_state;
312 if (fast_state.GetIgnoreBit())
313 return;
314
315 fast_state.IncrementEpoch();
316 thr->fast_state = fast_state;
317 TraceAddEvent(thr, fast_state.epoch(), EventTypeMop, pc);
318
319 bool unaligned = (addr % kShadowCell) != 0;
320
321 // Handle unaligned beginning, if any.
322 for (; addr % kShadowCell && size; addr++, size--) {
323 int const kAccessSizeLog = 0;
324 Shadow cur(fast_state);
325 cur.SetWrite(is_write);
326 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
327 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, fast_state,
328 shadow_mem, cur);
329 }
330 if (unaligned)
331 shadow_mem += kShadowCnt;
332 // Handle middle part, if any.
333 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
334 int const kAccessSizeLog = 3;
335 Shadow cur(fast_state);
336 cur.SetWrite(is_write);
337 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
338 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, fast_state,
339 shadow_mem, cur);
340 shadow_mem += kShadowCnt;
341 }
342 // Handle ending, if any.
343 for (; size; addr++, size--) {
344 int const kAccessSizeLog = 0;
345 Shadow cur(fast_state);
346 cur.SetWrite(is_write);
347 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
348 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, fast_state,
349 shadow_mem, cur);
350 }
351}
352
353void MemoryRead1Byte(ThreadState *thr, uptr pc, uptr addr) {
354 MemoryAccess(thr, pc, addr, 0, 0);
355}
356
357void MemoryWrite1Byte(ThreadState *thr, uptr pc, uptr addr) {
358 MemoryAccess(thr, pc, addr, 0, 1);
359}
360
361void MemoryRead8Byte(ThreadState *thr, uptr pc, uptr addr) {
362 MemoryAccess(thr, pc, addr, 3, 0);
363}
364
365void MemoryWrite8Byte(ThreadState *thr, uptr pc, uptr addr) {
366 MemoryAccess(thr, pc, addr, 3, 1);
367}
368} // namespace __tsan