blob: 2263a66ba99efa6171c70cd8cc64edd9bac597b2 [file] [log] [blame]
Alexey Samsonov3b2f9f42012-06-04 13:55:19 +00001//===-- tsan_rtl.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// Main file (entry points) for the TSan run-time.
13//===----------------------------------------------------------------------===//
14
Dmitry Vyukov6fa46f72012-06-29 16:58:33 +000015#include "sanitizer_common/sanitizer_atomic.h"
Alexey Samsonov58a3c582012-06-18 08:44:30 +000016#include "sanitizer_common/sanitizer_common.h"
Kostya Serebryanyc5bea202012-05-31 13:42:53 +000017#include "sanitizer_common/sanitizer_libc.h"
Alexey Samsonov8bd90982012-06-07 09:50:16 +000018#include "sanitizer_common/sanitizer_placement_new.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000019#include "tsan_defs.h"
20#include "tsan_platform.h"
21#include "tsan_rtl.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000022#include "tsan_mman.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000023#include "tsan_suppressions.h"
24
Dmitry Vyukov302cebb2012-05-22 18:07:45 +000025volatile int __tsan_resumed = 0;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000026
27extern "C" void __tsan_resume() {
Dmitry Vyukov302cebb2012-05-22 18:07:45 +000028 __tsan_resumed = 1;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000029}
30
31namespace __tsan {
32
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +000033#ifndef TSAN_GO
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000034THREADLOCAL char cur_thread_placeholder[sizeof(ThreadState)] ALIGNED(64);
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000035char allocator_placeholder[sizeof(Allocator)] ALIGNED(64);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +000036#endif
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000037static char ctx_placeholder[sizeof(Context)] ALIGNED(64);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000038
39static Context *ctx;
40Context *CTX() {
41 return ctx;
42}
43
44Context::Context()
45 : initialized()
46 , report_mtx(MutexTypeReport, StatMtxReport)
47 , nreported()
48 , nmissed_expected()
49 , thread_mtx(MutexTypeThreads, StatMtxThreads)
50 , racy_stacks(MBlockRacyStacks)
51 , racy_addresses(MBlockRacyAddresses) {
52}
53
54// The objects are allocated in TLS, so one may rely on zero-initialization.
55ThreadState::ThreadState(Context *ctx, int tid, u64 epoch,
56 uptr stk_addr, uptr stk_size,
57 uptr tls_addr, uptr tls_size)
58 : fast_state(tid, epoch)
59 // Do not touch these, rely on zero initialization,
60 // they may be accessed before the ctor.
61 // , fast_ignore_reads()
62 // , fast_ignore_writes()
63 // , in_rtl()
64 , shadow_stack_pos(&shadow_stack[0])
65 , tid(tid)
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000066 , stk_addr(stk_addr)
67 , stk_size(stk_size)
68 , tls_addr(tls_addr)
69 , tls_size(tls_size) {
70}
71
72ThreadContext::ThreadContext(int tid)
73 : tid(tid)
74 , unique_id()
75 , user_id()
76 , thr()
77 , status(ThreadStatusInvalid)
78 , detached()
79 , reuse_count()
80 , epoch0()
81 , epoch1()
Dmitry Vyukovf6985e32012-05-22 14:34:43 +000082 , dead_info()
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000083 , dead_next() {
84}
85
Dmitry Vyukov15710c92012-05-22 11:33:03 +000086static void WriteMemoryProfile(char *buf, uptr buf_size, int num) {
87 uptr shadow = GetShadowMemoryConsumption();
88
89 int nthread = 0;
90 int nlivethread = 0;
91 uptr threadmem = 0;
92 {
93 Lock l(&ctx->thread_mtx);
94 for (unsigned i = 0; i < kMaxTid; i++) {
95 ThreadContext *tctx = ctx->threads[i];
96 if (tctx == 0)
97 continue;
98 nthread += 1;
99 threadmem += sizeof(ThreadContext);
100 if (tctx->status != ThreadStatusRunning)
101 continue;
102 nlivethread += 1;
103 threadmem += sizeof(ThreadState);
104 }
105 }
106
107 uptr nsync = 0;
108 uptr syncmem = CTX()->synctab.GetMemoryConsumption(&nsync);
109
Alexey Samsonove1cb5242012-06-19 09:21:57 +0000110 internal_snprintf(buf, buf_size, "%d: shadow=%zuMB"
111 " thread=%zuMB(total=%d/live=%d)"
112 " sync=%zuMB(cnt=%zu)\n",
Dmitry Vyukov15710c92012-05-22 11:33:03 +0000113 num,
114 shadow >> 20,
115 threadmem >> 20, nthread, nlivethread,
116 syncmem >> 20, nsync);
117}
118
119static void MemoryProfileThread(void *arg) {
120 ScopedInRtl in_rtl;
121 fd_t fd = (fd_t)(uptr)arg;
122 for (int i = 0; ; i++) {
123 InternalScopedBuf<char> buf(4096);
124 WriteMemoryProfile(buf.Ptr(), buf.Size(), i);
125 internal_write(fd, buf.Ptr(), internal_strlen(buf.Ptr()));
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000126 SleepForSeconds(1);
Dmitry Vyukov15710c92012-05-22 11:33:03 +0000127 }
128}
129
130static void InitializeMemoryProfile() {
131 if (flags()->profile_memory == 0 || flags()->profile_memory[0] == 0)
132 return;
133 InternalScopedBuf<char> filename(4096);
Alexey Samsonove1cb5242012-06-19 09:21:57 +0000134 internal_snprintf(filename.Ptr(), filename.Size(), "%s.%d",
Dmitry Vyukov15710c92012-05-22 11:33:03 +0000135 flags()->profile_memory, GetPid());
136 fd_t fd = internal_open(filename.Ptr(), true);
137 if (fd == kInvalidFd) {
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000138 TsanPrintf("Failed to open memory profile file '%s'\n", &filename[0]);
Dmitry Vyukov15710c92012-05-22 11:33:03 +0000139 Die();
140 }
141 internal_start_thread(&MemoryProfileThread, (void*)(uptr)fd);
142}
143
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000144static void MemoryFlushThread(void *arg) {
145 ScopedInRtl in_rtl;
146 for (int i = 0; ; i++) {
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000147 SleepForMillis(flags()->flush_memory_ms);
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000148 FlushShadowMemory();
149 }
150}
151
152static void InitializeMemoryFlush() {
153 if (flags()->flush_memory_ms == 0)
154 return;
155 if (flags()->flush_memory_ms < 100)
156 flags()->flush_memory_ms = 100;
157 internal_start_thread(&MemoryFlushThread, 0);
158}
159
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000160void Initialize(ThreadState *thr) {
161 // Thread safe because done before all threads exist.
162 static bool is_initialized = false;
163 if (is_initialized)
164 return;
165 is_initialized = true;
166 ScopedInRtl in_rtl;
Dmitry Vyukov9f143c52012-08-16 19:36:45 +0000167#ifndef TSAN_GO
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000168 InitializeAllocator();
Dmitry Vyukov9f143c52012-08-16 19:36:45 +0000169#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000170 InitializeInterceptors();
171 const char *env = InitializePlatform();
172 InitializeMutex();
173 InitializeDynamicAnnotations();
174 ctx = new(ctx_placeholder) Context;
175 InitializeShadowMemory();
176 ctx->dead_list_size = 0;
177 ctx->dead_list_head = 0;
178 ctx->dead_list_tail = 0;
179 InitializeFlags(&ctx->flags, env);
180 InitializeSuppressions();
Dmitry Vyukov15710c92012-05-22 11:33:03 +0000181 InitializeMemoryProfile();
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000182 InitializeMemoryFlush();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000183
184 if (ctx->flags.verbosity)
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000185 TsanPrintf("***** Running under ThreadSanitizer v2 (pid %d) *****\n",
186 GetPid());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000187
188 // Initialize thread 0.
189 ctx->thread_seq = 0;
190 int tid = ThreadCreate(thr, 0, 0, true);
191 CHECK_EQ(tid, 0);
192 ThreadStart(thr, tid);
193 CHECK_EQ(thr->in_rtl, 1);
194 ctx->initialized = true;
195
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000196 if (flags()->stop_on_start) {
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000197 TsanPrintf("ThreadSanitizer is suspended at startup (pid %d)."
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000198 " Call __tsan_resume().\n",
199 GetPid());
200 while (__tsan_resumed == 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000201 }
202}
203
204int Finalize(ThreadState *thr) {
205 ScopedInRtl in_rtl;
206 Context *ctx = __tsan::ctx;
207 bool failed = false;
208
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000209 ThreadFinalize(thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000210
211 if (ctx->nreported) {
212 failed = true;
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000213 TsanPrintf("ThreadSanitizer: reported %d warnings\n", ctx->nreported);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000214 }
215
216 if (ctx->nmissed_expected) {
217 failed = true;
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000218 TsanPrintf("ThreadSanitizer: missed %d expected races\n",
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000219 ctx->nmissed_expected);
220 }
221
222 StatOutput(ctx->stat);
Dmitry Vyukov19b855f2012-05-17 15:00:27 +0000223 return failed ? flags()->exitcode : 0;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000224}
225
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000226void TraceSwitch(ThreadState *thr) {
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000227 thr->nomalloc++;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000228 ScopedInRtl in_rtl;
229 Lock l(&thr->trace.mtx);
230 unsigned trace = (thr->fast_state.epoch() / kTracePartSize) % kTraceParts;
231 TraceHeader *hdr = &thr->trace.headers[trace];
232 hdr->epoch0 = thr->fast_state.epoch();
233 hdr->stack0.ObtainCurrent(thr, 0);
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000234 thr->nomalloc--;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000235}
236
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000237#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000238extern "C" void __tsan_trace_switch() {
239 TraceSwitch(cur_thread());
240}
241
242extern "C" void __tsan_report_race() {
243 ReportRace(cur_thread());
244}
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000245#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000246
247ALWAYS_INLINE
248static Shadow LoadShadow(u64 *p) {
249 u64 raw = atomic_load((atomic_uint64_t*)p, memory_order_relaxed);
250 return Shadow(raw);
251}
252
253ALWAYS_INLINE
254static void StoreShadow(u64 *sp, u64 s) {
255 atomic_store((atomic_uint64_t*)sp, s, memory_order_relaxed);
256}
257
258ALWAYS_INLINE
259static void StoreIfNotYetStored(u64 *sp, u64 *s) {
260 StoreShadow(sp, *s);
261 *s = 0;
262}
263
264static inline void HandleRace(ThreadState *thr, u64 *shadow_mem,
265 Shadow cur, Shadow old) {
266 thr->racy_state[0] = cur.raw();
267 thr->racy_state[1] = old.raw();
268 thr->racy_shadow_addr = shadow_mem;
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000269#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000270 HACKY_CALL(__tsan_report_race);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000271#else
272 ReportRace(thr);
273#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000274}
275
276static inline bool BothReads(Shadow s, int kAccessIsWrite) {
277 return !kAccessIsWrite && !s.is_write();
278}
279
280static inline bool OldIsRWStronger(Shadow old, int kAccessIsWrite) {
281 return old.is_write() || !kAccessIsWrite;
282}
283
284static inline bool OldIsRWWeaker(Shadow old, int kAccessIsWrite) {
285 return !old.is_write() || kAccessIsWrite;
286}
287
288static inline bool OldIsInSameSynchEpoch(Shadow old, ThreadState *thr) {
289 return old.epoch() >= thr->fast_synch_epoch;
290}
291
292static inline bool HappensBefore(Shadow old, ThreadState *thr) {
293 return thr->clock.get(old.tid()) >= old.epoch();
294}
295
296ALWAYS_INLINE
297void MemoryAccessImpl(ThreadState *thr, uptr addr,
298 int kAccessSizeLog, bool kAccessIsWrite, FastState fast_state,
299 u64 *shadow_mem, Shadow cur) {
300 StatInc(thr, StatMop);
301 StatInc(thr, kAccessIsWrite ? StatMopWrite : StatMopRead);
302 StatInc(thr, (StatType)(StatMop1 + kAccessSizeLog));
303
304 // This potentially can live in an MMX/SSE scratch register.
305 // The required intrinsics are:
306 // __m128i _mm_move_epi64(__m128i*);
307 // _mm_storel_epi64(u64*, __m128i);
308 u64 store_word = cur.raw();
309
310 // scan all the shadow values and dispatch to 4 categories:
311 // same, replace, candidate and race (see comments below).
312 // we consider only 3 cases regarding access sizes:
313 // equal, intersect and not intersect. initially I considered
314 // larger and smaller as well, it allowed to replace some
315 // 'candidates' with 'same' or 'replace', but I think
316 // it's just not worth it (performance- and complexity-wise).
317
318 Shadow old(0);
319 if (kShadowCnt == 1) {
320 int idx = 0;
321#include "tsan_update_shadow_word_inl.h"
322 } else if (kShadowCnt == 2) {
323 int idx = 0;
324#include "tsan_update_shadow_word_inl.h"
325 idx = 1;
326#include "tsan_update_shadow_word_inl.h"
327 } else if (kShadowCnt == 4) {
328 int idx = 0;
329#include "tsan_update_shadow_word_inl.h"
330 idx = 1;
331#include "tsan_update_shadow_word_inl.h"
332 idx = 2;
333#include "tsan_update_shadow_word_inl.h"
334 idx = 3;
335#include "tsan_update_shadow_word_inl.h"
336 } else if (kShadowCnt == 8) {
337 int idx = 0;
338#include "tsan_update_shadow_word_inl.h"
339 idx = 1;
340#include "tsan_update_shadow_word_inl.h"
341 idx = 2;
342#include "tsan_update_shadow_word_inl.h"
343 idx = 3;
344#include "tsan_update_shadow_word_inl.h"
345 idx = 4;
346#include "tsan_update_shadow_word_inl.h"
347 idx = 5;
348#include "tsan_update_shadow_word_inl.h"
349 idx = 6;
350#include "tsan_update_shadow_word_inl.h"
351 idx = 7;
352#include "tsan_update_shadow_word_inl.h"
353 } else {
354 CHECK(false);
355 }
356
357 // we did not find any races and had already stored
358 // the current access info, so we are done
359 if (LIKELY(store_word == 0))
360 return;
361 // choose a random candidate slot and replace it
362 StoreShadow(shadow_mem + (cur.epoch() % kShadowCnt), store_word);
363 StatInc(thr, StatShadowReplace);
364 return;
365 RACE:
366 HandleRace(thr, shadow_mem, cur, old);
367 return;
368}
369
370ALWAYS_INLINE
371void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
372 int kAccessSizeLog, bool kAccessIsWrite) {
373 u64 *shadow_mem = (u64*)MemToShadow(addr);
374 DPrintf2("#%d: tsan::OnMemoryAccess: @%p %p size=%d"
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000375 " is_write=%d shadow_mem=%p {%zx, %zx, %zx, %zx}\n",
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000376 (int)thr->fast_state.tid(), (void*)pc, (void*)addr,
377 (int)(1 << kAccessSizeLog), kAccessIsWrite, shadow_mem,
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000378 (uptr)shadow_mem[0], (uptr)shadow_mem[1],
379 (uptr)shadow_mem[2], (uptr)shadow_mem[3]);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000380#if TSAN_DEBUG
381 if (!IsAppMem(addr)) {
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000382 TsanPrintf("Access to non app mem %zx\n", addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000383 DCHECK(IsAppMem(addr));
384 }
385 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000386 TsanPrintf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000387 DCHECK(IsShadowMem((uptr)shadow_mem));
388 }
389#endif
390
391 FastState fast_state = thr->fast_state;
392 if (fast_state.GetIgnoreBit())
393 return;
394 fast_state.IncrementEpoch();
395 thr->fast_state = fast_state;
396 Shadow cur(fast_state);
397 cur.SetAddr0AndSizeLog(addr & 7, kAccessSizeLog);
398 cur.SetWrite(kAccessIsWrite);
399
400 // We must not store to the trace if we do not store to the shadow.
401 // That is, this call must be moved somewhere below.
402 TraceAddEvent(thr, fast_state.epoch(), EventTypeMop, pc);
403
404 MemoryAccessImpl(thr, addr, kAccessSizeLog, kAccessIsWrite, fast_state,
405 shadow_mem, cur);
406}
407
408static void MemoryRangeSet(ThreadState *thr, uptr pc, uptr addr, uptr size,
409 u64 val) {
410 if (size == 0)
411 return;
412 // FIXME: fix me.
413 uptr offset = addr % kShadowCell;
414 if (offset) {
415 offset = kShadowCell - offset;
416 if (size <= offset)
417 return;
418 addr += offset;
419 size -= offset;
420 }
421 CHECK_EQ(addr % 8, 0);
422 CHECK(IsAppMem(addr));
423 CHECK(IsAppMem(addr + size - 1));
424 (void)thr;
425 (void)pc;
426 // Some programs mmap like hundreds of GBs but actually used a small part.
427 // So, it's better to report a false positive on the memory
428 // then to hang here senselessly.
429 const uptr kMaxResetSize = 1024*1024*1024;
430 if (size > kMaxResetSize)
431 size = kMaxResetSize;
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000432 size = (size + (kShadowCell - 1)) & ~(kShadowCell - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000433 u64 *p = (u64*)MemToShadow(addr);
434 CHECK(IsShadowMem((uptr)p));
435 CHECK(IsShadowMem((uptr)(p + size * kShadowCnt / kShadowCell - 1)));
436 // FIXME: may overwrite a part outside the region
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000437 for (uptr i = 0; i < size * kShadowCnt / kShadowCell;) {
438 p[i++] = val;
439 for (uptr j = 1; j < kShadowCnt; j++)
440 p[i++] = 0;
441 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000442}
443
444void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size) {
445 MemoryRangeSet(thr, pc, addr, size, 0);
446}
447
448void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size) {
449 MemoryAccessRange(thr, pc, addr, size, true);
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000450 Shadow s(thr->fast_state);
451 s.MarkAsFreed();
452 s.SetWrite(true);
453 s.SetAddr0AndSizeLog(0, 3);
454 MemoryRangeSet(thr, pc, addr, size, s.raw());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000455}
456
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000457void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size) {
458 Shadow s(thr->fast_state);
459 s.SetWrite(true);
460 s.SetAddr0AndSizeLog(0, 3);
461 MemoryRangeSet(thr, pc, addr, size, s.raw());
462}
463
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000464void FuncEntry(ThreadState *thr, uptr pc) {
465 DCHECK_EQ(thr->in_rtl, 0);
466 StatInc(thr, StatFuncEnter);
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000467 DPrintf2("#%d: FuncEntry %p\n", (int)thr->fast_state.tid(), (void*)pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000468 thr->fast_state.IncrementEpoch();
469 TraceAddEvent(thr, thr->fast_state.epoch(), EventTypeFuncEnter, pc);
470
471 // Shadow stack maintenance can be replaced with
472 // stack unwinding during trace switch (which presumably must be faster).
Dmitry Vyukov3de9ca02012-05-28 07:45:35 +0000473 DCHECK_GE(thr->shadow_stack_pos, &thr->shadow_stack[0]);
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000474#ifndef TSAN_GO
Dmitry Vyukov3de9ca02012-05-28 07:45:35 +0000475 DCHECK_LT(thr->shadow_stack_pos, &thr->shadow_stack[kShadowStackSize]);
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000476#else
477 if (thr->shadow_stack_pos == thr->shadow_stack_end) {
478 const int sz = thr->shadow_stack_end - thr->shadow_stack;
479 const int newsz = 2 * sz;
480 uptr *newstack = (uptr*)internal_alloc(MBlockShadowStack,
481 newsz * sizeof(uptr));
482 internal_memcpy(newstack, thr->shadow_stack, sz * sizeof(uptr));
483 internal_free(thr->shadow_stack);
484 thr->shadow_stack = newstack;
485 thr->shadow_stack_pos = newstack + sz;
486 thr->shadow_stack_end = newstack + newsz;
487 }
488#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000489 thr->shadow_stack_pos[0] = pc;
490 thr->shadow_stack_pos++;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000491}
492
493void FuncExit(ThreadState *thr) {
494 DCHECK_EQ(thr->in_rtl, 0);
495 StatInc(thr, StatFuncExit);
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000496 DPrintf2("#%d: FuncExit\n", (int)thr->fast_state.tid());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000497 thr->fast_state.IncrementEpoch();
498 TraceAddEvent(thr, thr->fast_state.epoch(), EventTypeFuncExit, 0);
499
Dmitry Vyukov3de9ca02012-05-28 07:45:35 +0000500 DCHECK_GT(thr->shadow_stack_pos, &thr->shadow_stack[0]);
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000501#ifndef TSAN_GO
Dmitry Vyukov3de9ca02012-05-28 07:45:35 +0000502 DCHECK_LT(thr->shadow_stack_pos, &thr->shadow_stack[kShadowStackSize]);
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000503#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000504 thr->shadow_stack_pos--;
505}
506
507void IgnoreCtl(ThreadState *thr, bool write, bool begin) {
508 DPrintf("#%d: IgnoreCtl(%d, %d)\n", thr->tid, write, begin);
509 thr->ignore_reads_and_writes += begin ? 1 : -1;
510 CHECK_GE(thr->ignore_reads_and_writes, 0);
511 if (thr->ignore_reads_and_writes)
512 thr->fast_state.SetIgnoreBit();
513 else
514 thr->fast_state.ClearIgnoreBit();
515}
516
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000517bool MD5Hash::operator==(const MD5Hash &other) const {
518 return hash[0] == other.hash[0] && hash[1] == other.hash[1];
519}
520
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000521#if TSAN_DEBUG
522void build_consistency_debug() {}
523#else
524void build_consistency_release() {}
525#endif
526
527#if TSAN_COLLECT_STATS
528void build_consistency_stats() {}
529#else
530void build_consistency_nostats() {}
531#endif
532
533#if TSAN_SHADOW_COUNT == 1
534void build_consistency_shadow1() {}
535#elif TSAN_SHADOW_COUNT == 2
536void build_consistency_shadow2() {}
537#elif TSAN_SHADOW_COUNT == 4
538void build_consistency_shadow4() {}
539#else
540void build_consistency_shadow8() {}
541#endif
542
543} // namespace __tsan
544
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000545#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000546// Must be included in this file to make sure everything is inlined.
547#include "tsan_interface_inl.h"
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000548#endif