1 //===-- tsan_rtl.cc -------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
12 // Main file (entry points) for the TSan run-time.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_atomic.h"
16 #include "sanitizer_common/sanitizer_common.h"
17 #include "sanitizer_common/sanitizer_libc.h"
18 #include "sanitizer_common/sanitizer_stackdepot.h"
19 #include "sanitizer_common/sanitizer_placement_new.h"
20 #include "sanitizer_common/sanitizer_symbolizer.h"
21 #include "tsan_defs.h"
22 #include "tsan_platform.h"
24 #include "tsan_mman.h"
25 #include "tsan_suppressions.h"
26 #include "tsan_symbolize.h"
29 // <emmintrin.h> transitively includes <stdlib.h>,
30 // and it's prohibited to include std headers into tsan runtime.
31 // So we do this dirty trick.
32 #define _MM_MALLOC_H_INCLUDED
34 #include <emmintrin.h>
38 volatile int __tsan_resumed = 0;
40 extern "C" void __tsan_resume() {
47 THREADLOCAL char cur_thread_placeholder[sizeof(ThreadState)] ALIGNED(64);
49 static char ctx_placeholder[sizeof(Context)] ALIGNED(64);
52 // Can be overriden by a front-end.
53 #ifdef TSAN_EXTERNAL_HOOKS
54 bool OnFinalize(bool failed);
57 SANITIZER_INTERFACE_ATTRIBUTE
58 bool WEAK OnFinalize(bool failed) {
61 SANITIZER_INTERFACE_ATTRIBUTE
62 void WEAK OnInitialize() {}
65 static char thread_registry_placeholder[sizeof(ThreadRegistry)];
67 static ThreadContextBase *CreateThreadContext(u32 tid) {
68 // Map thread trace when context is created.
69 MapThreadTrace(GetThreadTrace(tid), TraceSize() * sizeof(Event));
70 const uptr hdr = GetThreadTraceHeader(tid);
71 MapThreadTrace(hdr, sizeof(Trace));
72 new((void*)hdr) Trace();
73 // We are going to use only a small part of the trace with the default
74 // value of history_size. However, the constructor writes to the whole trace.
75 // Unmap the unused part.
76 uptr hdr_end = hdr + sizeof(Trace);
77 hdr_end -= sizeof(TraceHeader) * (kTraceParts - TraceParts());
78 hdr_end = RoundUp(hdr_end, GetPageSizeCached());
79 if (hdr_end < hdr + sizeof(Trace))
80 UnmapOrDie((void*)hdr_end, hdr + sizeof(Trace) - hdr_end);
81 void *mem = internal_alloc(MBlockThreadContex, sizeof(ThreadContext));
82 return new(mem) ThreadContext(tid);
86 static const u32 kThreadQuarantineSize = 16;
88 static const u32 kThreadQuarantineSize = 64;
93 , report_mtx(MutexTypeReport, StatMtxReport)
96 , thread_registry(new(thread_registry_placeholder) ThreadRegistry(
97 CreateThreadContext, kMaxTid, kThreadQuarantineSize, kMaxTidReuse))
98 , racy_stacks(MBlockRacyStacks)
99 , racy_addresses(MBlockRacyAddresses)
100 , fired_suppressions(8) {
103 // The objects are allocated in TLS, so one may rely on zero-initialization.
104 ThreadState::ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
105 unsigned reuse_count,
106 uptr stk_addr, uptr stk_size,
107 uptr tls_addr, uptr tls_size)
108 : fast_state(tid, epoch)
109 // Do not touch these, rely on zero initialization,
110 // they may be accessed before the ctor.
111 // , ignore_reads_and_writes()
112 // , ignore_interceptors()
113 , clock(tid, reuse_count)
115 , jmp_bufs(MBlockJmpBuf)
118 , unique_id(unique_id)
124 , last_sleep_clock(tid)
130 static void MemoryProfiler(Context *ctx, fd_t fd, int i) {
132 uptr n_running_threads;
133 ctx->thread_registry->GetNumberOfThreads(&n_threads, &n_running_threads);
134 InternalScopedBuffer<char> buf(4096);
135 WriteMemoryProfile(buf.data(), buf.size(), n_threads, n_running_threads);
136 internal_write(fd, buf.data(), internal_strlen(buf.data()));
139 static void BackgroundThread(void *arg) {
140 // This is a non-initialized non-user thread, nothing to see here.
141 // We don't use ScopedIgnoreInterceptors, because we want ignores to be
142 // enabled even when the thread function exits (e.g. during pthread thread
144 cur_thread()->ignore_interceptors++;
145 const u64 kMs2Ns = 1000 * 1000;
147 fd_t mprof_fd = kInvalidFd;
148 if (flags()->profile_memory && flags()->profile_memory[0]) {
149 if (internal_strcmp(flags()->profile_memory, "stdout") == 0) {
151 } else if (internal_strcmp(flags()->profile_memory, "stderr") == 0) {
154 InternalScopedString filename(kMaxPathLength);
155 filename.append("%s.%d", flags()->profile_memory, (int)internal_getpid());
156 uptr openrv = OpenFile(filename.data(), true);
157 if (internal_iserror(openrv)) {
158 Printf("ThreadSanitizer: failed to open memory profile file '%s'\n",
166 u64 last_flush = NanoTime();
169 atomic_load(&ctx->stop_background_thread, memory_order_relaxed) == 0;
172 u64 now = NanoTime();
174 // Flush memory if requested.
175 if (flags()->flush_memory_ms > 0) {
176 if (last_flush + flags()->flush_memory_ms * kMs2Ns < now) {
177 VPrintf(1, "ThreadSanitizer: periodic memory flush\n");
179 last_flush = NanoTime();
182 // GetRSS can be expensive on huge programs, so don't do it every 100ms.
183 if (flags()->memory_limit_mb > 0) {
185 uptr limit = uptr(flags()->memory_limit_mb) << 20;
186 VPrintf(1, "ThreadSanitizer: memory flush check"
187 " RSS=%llu LAST=%llu LIMIT=%llu\n",
188 (u64)rss >> 20, (u64)last_rss >> 20, (u64)limit >> 20);
189 if (2 * rss > limit + last_rss) {
190 VPrintf(1, "ThreadSanitizer: flushing memory due to RSS\n");
193 VPrintf(1, "ThreadSanitizer: memory flushed RSS=%llu\n", (u64)rss>>20);
198 // Write memory profile if requested.
199 if (mprof_fd != kInvalidFd)
200 MemoryProfiler(ctx, mprof_fd, i);
202 // Flush symbolizer cache if requested.
203 if (flags()->flush_symbolizer_ms > 0) {
204 u64 last = atomic_load(&ctx->last_symbolize_time_ns,
205 memory_order_relaxed);
206 if (last != 0 && last + flags()->flush_symbolizer_ms * kMs2Ns < now) {
207 Lock l(&ctx->report_mtx);
208 SpinMutexLock l2(&CommonSanitizerReportMutex);
210 atomic_store(&ctx->last_symbolize_time_ns, 0, memory_order_relaxed);
216 static void StartBackgroundThread() {
217 ctx->background_thread = internal_start_thread(&BackgroundThread, 0);
221 static void StopBackgroundThread() {
222 atomic_store(&ctx->stop_background_thread, 1, memory_order_relaxed);
223 internal_join_thread(ctx->background_thread);
224 ctx->background_thread = 0;
229 void DontNeedShadowFor(uptr addr, uptr size) {
230 uptr shadow_beg = MemToShadow(addr);
231 uptr shadow_end = MemToShadow(addr + size);
232 FlushUnneededShadowMemory(shadow_beg, shadow_end - shadow_beg);
235 void MapShadow(uptr addr, uptr size) {
236 // Global data is not 64K aligned, but there are no adjacent mappings,
237 // so we can get away with unaligned mapping.
238 // CHECK_EQ(addr, addr & ~((64 << 10) - 1)); // windows wants 64K alignment
239 MmapFixedNoReserve(MemToShadow(addr), size * kShadowMultiplier);
241 // Meta shadow is 2:1, so tread carefully.
242 static bool data_mapped = false;
243 static uptr mapped_meta_end = 0;
244 uptr meta_begin = (uptr)MemToMeta(addr);
245 uptr meta_end = (uptr)MemToMeta(addr + size);
246 meta_begin = RoundDownTo(meta_begin, 64 << 10);
247 meta_end = RoundUpTo(meta_end, 64 << 10);
249 // First call maps data+bss.
251 MmapFixedNoReserve(meta_begin, meta_end - meta_begin);
253 // Mapping continous heap.
254 // Windows wants 64K alignment.
255 meta_begin = RoundDownTo(meta_begin, 64 << 10);
256 meta_end = RoundUpTo(meta_end, 64 << 10);
257 if (meta_end <= mapped_meta_end)
259 if (meta_begin < mapped_meta_end)
260 meta_begin = mapped_meta_end;
261 MmapFixedNoReserve(meta_begin, meta_end - meta_begin);
262 mapped_meta_end = meta_end;
264 VPrintf(2, "mapped meta shadow for (%p-%p) at (%p-%p)\n",
265 addr, addr+size, meta_begin, meta_end);
268 void MapThreadTrace(uptr addr, uptr size) {
269 DPrintf("#0: Mapping trace at %p-%p(0x%zx)\n", addr, addr + size, size);
270 CHECK_GE(addr, kTraceMemBeg);
271 CHECK_LE(addr + size, kTraceMemEnd);
272 CHECK_EQ(addr, addr & ~((64 << 10) - 1)); // windows wants 64K alignment
273 uptr addr1 = (uptr)MmapFixedNoReserve(addr, size);
275 Printf("FATAL: ThreadSanitizer can not mmap thread trace (%p/%p->%p)\n",
281 static void CheckShadowMapping() {
282 for (uptr i = 0; i < ARRAY_SIZE(UserRegions); i += 2) {
283 const uptr beg = UserRegions[i];
284 const uptr end = UserRegions[i + 1];
285 VPrintf(3, "checking shadow region %p-%p\n", beg, end);
286 for (uptr p0 = beg; p0 <= end; p0 += (end - beg) / 4) {
287 for (int x = -1; x <= 1; x++) {
288 const uptr p = p0 + x;
289 if (p < beg || p >= end)
291 const uptr s = MemToShadow(p);
292 const uptr m = (uptr)MemToMeta(p);
293 VPrintf(3, " checking pointer %p: shadow=%p meta=%p\n", p, s, m);
295 CHECK(IsShadowMem(s));
296 CHECK_EQ(p & ~(kShadowCell - 1), ShadowToMem(s));
303 void Initialize(ThreadState *thr) {
304 // Thread safe because done before all threads exist.
305 static bool is_initialized = false;
308 is_initialized = true;
309 // We are not ready to handle interceptors yet.
310 ScopedIgnoreInterceptors ignore;
311 SanitizerToolName = "ThreadSanitizer";
312 // Install tool-specific callbacks in sanitizer_common.
313 SetCheckFailedCallback(TsanCheckFailed);
315 ctx = new(ctx_placeholder) Context;
316 const char *options = GetEnv(kTsanOptionsEnv);
317 InitializeFlags(&ctx->flags, options);
319 InitializeAllocator();
321 InitializeInterceptors();
322 CheckShadowMapping();
323 InitializePlatform();
325 InitializeDynamicAnnotations();
327 InitializeShadowMemory();
329 // Setup correct file descriptor for error reports.
330 __sanitizer_set_report_path(common_flags()->log_path);
331 InitializeSuppressions();
333 InitializeLibIgnore();
334 Symbolizer::GetOrInit()->AddHooks(EnterSymbolizer, ExitSymbolizer);
335 // On MIPS, TSan initialization is run before
336 // __pthread_initialize_minimal_internal() is finished, so we can not spawn
339 StartBackgroundThread();
340 SetSandboxingCallback(StopBackgroundThread);
343 if (common_flags()->detect_deadlocks)
344 ctx->dd = DDetector::Create(flags());
346 VPrintf(1, "***** Running under ThreadSanitizer v2 (pid %d) *****\n",
347 (int)internal_getpid());
349 // Initialize thread 0.
350 int tid = ThreadCreate(thr, 0, 0, true);
352 ThreadStart(thr, tid, internal_getpid());
353 ctx->initialized = true;
355 if (flags()->stop_on_start) {
356 Printf("ThreadSanitizer is suspended at startup (pid %d)."
357 " Call __tsan_resume().\n",
358 (int)internal_getpid());
359 while (__tsan_resumed == 0) {}
365 int Finalize(ThreadState *thr) {
368 if (flags()->atexit_sleep_ms > 0 && ThreadCount(thr) > 1)
369 SleepForMillis(flags()->atexit_sleep_ms);
371 // Wait for pending reports.
372 ctx->report_mtx.Lock();
373 CommonSanitizerReportMutex.Lock();
374 CommonSanitizerReportMutex.Unlock();
375 ctx->report_mtx.Unlock();
378 if (Verbosity()) AllocatorPrintStats();
383 if (ctx->nreported) {
386 Printf("ThreadSanitizer: reported %d warnings\n", ctx->nreported);
388 Printf("Found %d data race(s)\n", ctx->nreported);
392 if (ctx->nmissed_expected) {
394 Printf("ThreadSanitizer: missed %d expected races\n",
395 ctx->nmissed_expected);
398 if (common_flags()->print_suppressions)
399 PrintMatchedSuppressions();
401 if (flags()->print_benign)
402 PrintMatchedBenignRaces();
405 failed = OnFinalize(failed);
407 #if TSAN_COLLECT_STATS
408 StatAggregate(ctx->stat, thr->stat);
409 StatOutput(ctx->stat);
412 return failed ? flags()->exitcode : 0;
416 void ForkBefore(ThreadState *thr, uptr pc) {
417 ctx->thread_registry->Lock();
418 ctx->report_mtx.Lock();
421 void ForkParentAfter(ThreadState *thr, uptr pc) {
422 ctx->report_mtx.Unlock();
423 ctx->thread_registry->Unlock();
426 void ForkChildAfter(ThreadState *thr, uptr pc) {
427 ctx->report_mtx.Unlock();
428 ctx->thread_registry->Unlock();
431 ctx->thread_registry->GetNumberOfThreads(0, 0, &nthread /* alive threads */);
432 VPrintf(1, "ThreadSanitizer: forked new process with pid %d,"
433 " parent had %d threads\n", (int)internal_getpid(), (int)nthread);
435 StartBackgroundThread();
437 // We've just forked a multi-threaded process. We cannot reasonably function
438 // after that (some mutexes may be locked before fork). So just enable
439 // ignores for everything in the hope that we will exec soon.
440 ctx->after_multithreaded_fork = true;
441 thr->ignore_interceptors++;
442 ThreadIgnoreBegin(thr, pc);
443 ThreadIgnoreSyncBegin(thr, pc);
450 void GrowShadowStack(ThreadState *thr) {
451 const int sz = thr->shadow_stack_end - thr->shadow_stack;
452 const int newsz = 2 * sz;
453 uptr *newstack = (uptr*)internal_alloc(MBlockShadowStack,
454 newsz * sizeof(uptr));
455 internal_memcpy(newstack, thr->shadow_stack, sz * sizeof(uptr));
456 internal_free(thr->shadow_stack);
457 thr->shadow_stack = newstack;
458 thr->shadow_stack_pos = newstack + sz;
459 thr->shadow_stack_end = newstack + newsz;
463 u32 CurrentStackId(ThreadState *thr, uptr pc) {
464 if (thr->shadow_stack_pos == 0) // May happen during bootstrap.
468 DCHECK_LT(thr->shadow_stack_pos, thr->shadow_stack_end);
470 if (thr->shadow_stack_pos == thr->shadow_stack_end)
471 GrowShadowStack(thr);
473 thr->shadow_stack_pos[0] = pc;
474 thr->shadow_stack_pos++;
476 u32 id = StackDepotPut(
477 StackTrace(thr->shadow_stack, thr->shadow_stack_pos - thr->shadow_stack));
479 thr->shadow_stack_pos--;
483 void TraceSwitch(ThreadState *thr) {
485 Trace *thr_trace = ThreadTrace(thr->tid);
486 Lock l(&thr_trace->mtx);
487 unsigned trace = (thr->fast_state.epoch() / kTracePartSize) % TraceParts();
488 TraceHeader *hdr = &thr_trace->headers[trace];
489 hdr->epoch0 = thr->fast_state.epoch();
490 ObtainCurrentStack(thr, 0, &hdr->stack0);
491 hdr->mset0 = thr->mset;
495 Trace *ThreadTrace(int tid) {
496 return (Trace*)GetThreadTraceHeader(tid);
499 uptr TraceTopPC(ThreadState *thr) {
500 Event *events = (Event*)GetThreadTrace(thr->tid);
501 uptr pc = events[thr->fast_state.GetTracePos()];
506 return (uptr)(1ull << (kTracePartSizeBits + flags()->history_size + 1));
510 return TraceSize() / kTracePartSize;
514 extern "C" void __tsan_trace_switch() {
515 TraceSwitch(cur_thread());
518 extern "C" void __tsan_report_race() {
519 ReportRace(cur_thread());
524 Shadow LoadShadow(u64 *p) {
525 u64 raw = atomic_load((atomic_uint64_t*)p, memory_order_relaxed);
530 void StoreShadow(u64 *sp, u64 s) {
531 atomic_store((atomic_uint64_t*)sp, s, memory_order_relaxed);
535 void StoreIfNotYetStored(u64 *sp, u64 *s) {
541 void HandleRace(ThreadState *thr, u64 *shadow_mem,
542 Shadow cur, Shadow old) {
543 thr->racy_state[0] = cur.raw();
544 thr->racy_state[1] = old.raw();
545 thr->racy_shadow_addr = shadow_mem;
547 HACKY_CALL(__tsan_report_race);
553 static inline bool HappensBefore(Shadow old, ThreadState *thr) {
554 return thr->clock.get(old.TidWithIgnore()) >= old.epoch();
558 void MemoryAccessImpl1(ThreadState *thr, uptr addr,
559 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic,
560 u64 *shadow_mem, Shadow cur) {
561 StatInc(thr, StatMop);
562 StatInc(thr, kAccessIsWrite ? StatMopWrite : StatMopRead);
563 StatInc(thr, (StatType)(StatMop1 + kAccessSizeLog));
565 // This potentially can live in an MMX/SSE scratch register.
566 // The required intrinsics are:
567 // __m128i _mm_move_epi64(__m128i*);
568 // _mm_storel_epi64(u64*, __m128i);
569 u64 store_word = cur.raw();
571 // scan all the shadow values and dispatch to 4 categories:
572 // same, replace, candidate and race (see comments below).
573 // we consider only 3 cases regarding access sizes:
574 // equal, intersect and not intersect. initially I considered
575 // larger and smaller as well, it allowed to replace some
576 // 'candidates' with 'same' or 'replace', but I think
577 // it's just not worth it (performance- and complexity-wise).
581 // It release mode we manually unroll the loop,
582 // because empirically gcc generates better code this way.
583 // However, we can't afford unrolling in debug mode, because the function
584 // consumes almost 4K of stack. Gtest gives only 4K of stack to death test
585 // threads, which is not enough for the unrolled loop.
587 for (int idx = 0; idx < 4; idx++) {
588 #include "tsan_update_shadow_word_inl.h"
592 #include "tsan_update_shadow_word_inl.h"
594 #include "tsan_update_shadow_word_inl.h"
596 #include "tsan_update_shadow_word_inl.h"
598 #include "tsan_update_shadow_word_inl.h"
601 // we did not find any races and had already stored
602 // the current access info, so we are done
603 if (LIKELY(store_word == 0))
605 // choose a random candidate slot and replace it
606 StoreShadow(shadow_mem + (cur.epoch() % kShadowCnt), store_word);
607 StatInc(thr, StatShadowReplace);
610 HandleRace(thr, shadow_mem, cur, old);
614 void UnalignedMemoryAccess(ThreadState *thr, uptr pc, uptr addr,
615 int size, bool kAccessIsWrite, bool kIsAtomic) {
618 int kAccessSizeLog = kSizeLog1;
619 if (size >= 8 && (addr & ~7) == ((addr + 7) & ~7)) {
621 kAccessSizeLog = kSizeLog8;
622 } else if (size >= 4 && (addr & ~7) == ((addr + 3) & ~7)) {
624 kAccessSizeLog = kSizeLog4;
625 } else if (size >= 2 && (addr & ~7) == ((addr + 1) & ~7)) {
627 kAccessSizeLog = kSizeLog2;
629 MemoryAccess(thr, pc, addr, kAccessSizeLog, kAccessIsWrite, kIsAtomic);
636 bool ContainsSameAccessSlow(u64 *s, u64 a, u64 sync_epoch, bool is_write) {
638 for (uptr i = 0; i < kShadowCnt; i++) {
639 Shadow old(LoadShadow(&s[i]));
640 if (Shadow::Addr0AndSizeAreEqual(cur, old) &&
641 old.TidWithIgnore() == cur.TidWithIgnore() &&
642 old.epoch() > sync_epoch &&
643 old.IsAtomic() == cur.IsAtomic() &&
644 old.IsRead() <= cur.IsRead())
650 #if defined(__SSE3__)
651 #define SHUF(v0, v1, i0, i1, i2, i3) _mm_castps_si128(_mm_shuffle_ps( \
652 _mm_castsi128_ps(v0), _mm_castsi128_ps(v1), \
653 (i0)*1 + (i1)*4 + (i2)*16 + (i3)*64))
655 bool ContainsSameAccessFast(u64 *s, u64 a, u64 sync_epoch, bool is_write) {
656 // This is an optimized version of ContainsSameAccessSlow.
657 // load current access into access[0:63]
658 const m128 access = _mm_cvtsi64_si128(a);
659 // duplicate high part of access in addr0:
660 // addr0[0:31] = access[32:63]
661 // addr0[32:63] = access[32:63]
662 // addr0[64:95] = access[32:63]
663 // addr0[96:127] = access[32:63]
664 const m128 addr0 = SHUF(access, access, 1, 1, 1, 1);
665 // load 4 shadow slots
666 const m128 shadow0 = _mm_load_si128((__m128i*)s);
667 const m128 shadow1 = _mm_load_si128((__m128i*)s + 1);
668 // load high parts of 4 shadow slots into addr_vect:
669 // addr_vect[0:31] = shadow0[32:63]
670 // addr_vect[32:63] = shadow0[96:127]
671 // addr_vect[64:95] = shadow1[32:63]
672 // addr_vect[96:127] = shadow1[96:127]
673 m128 addr_vect = SHUF(shadow0, shadow1, 1, 3, 1, 3);
675 // set IsRead bit in addr_vect
676 const m128 rw_mask1 = _mm_cvtsi64_si128(1<<15);
677 const m128 rw_mask = SHUF(rw_mask1, rw_mask1, 0, 0, 0, 0);
678 addr_vect = _mm_or_si128(addr_vect, rw_mask);
680 // addr0 == addr_vect?
681 const m128 addr_res = _mm_cmpeq_epi32(addr0, addr_vect);
682 // epoch1[0:63] = sync_epoch
683 const m128 epoch1 = _mm_cvtsi64_si128(sync_epoch);
684 // epoch[0:31] = sync_epoch[0:31]
685 // epoch[32:63] = sync_epoch[0:31]
686 // epoch[64:95] = sync_epoch[0:31]
687 // epoch[96:127] = sync_epoch[0:31]
688 const m128 epoch = SHUF(epoch1, epoch1, 0, 0, 0, 0);
689 // load low parts of shadow cell epochs into epoch_vect:
690 // epoch_vect[0:31] = shadow0[0:31]
691 // epoch_vect[32:63] = shadow0[64:95]
692 // epoch_vect[64:95] = shadow1[0:31]
693 // epoch_vect[96:127] = shadow1[64:95]
694 const m128 epoch_vect = SHUF(shadow0, shadow1, 0, 2, 0, 2);
695 // epoch_vect >= sync_epoch?
696 const m128 epoch_res = _mm_cmpgt_epi32(epoch_vect, epoch);
697 // addr_res & epoch_res
698 const m128 res = _mm_and_si128(addr_res, epoch_res);
702 // mask[15] = res[127]
703 const int mask = _mm_movemask_epi8(res);
709 bool ContainsSameAccess(u64 *s, u64 a, u64 sync_epoch, bool is_write) {
710 #if defined(__SSE3__)
711 bool res = ContainsSameAccessFast(s, a, sync_epoch, is_write);
712 // NOTE: this check can fail if the shadow is concurrently mutated
713 // by other threads. But it still can be useful if you modify
714 // ContainsSameAccessFast and want to ensure that it's not completely broken.
715 // DCHECK_EQ(res, ContainsSameAccessSlow(s, a, sync_epoch, is_write));
718 return ContainsSameAccessSlow(s, a, sync_epoch, is_write);
723 void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
724 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic) {
725 u64 *shadow_mem = (u64*)MemToShadow(addr);
726 DPrintf2("#%d: MemoryAccess: @%p %p size=%d"
727 " is_write=%d shadow_mem=%p {%zx, %zx, %zx, %zx}\n",
728 (int)thr->fast_state.tid(), (void*)pc, (void*)addr,
729 (int)(1 << kAccessSizeLog), kAccessIsWrite, shadow_mem,
730 (uptr)shadow_mem[0], (uptr)shadow_mem[1],
731 (uptr)shadow_mem[2], (uptr)shadow_mem[3]);
733 if (!IsAppMem(addr)) {
734 Printf("Access to non app mem %zx\n", addr);
735 DCHECK(IsAppMem(addr));
737 if (!IsShadowMem((uptr)shadow_mem)) {
738 Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
739 DCHECK(IsShadowMem((uptr)shadow_mem));
743 if (kCppMode && *shadow_mem == kShadowRodata) {
744 // Access to .rodata section, no races here.
745 // Measurements show that it can be 10-20% of all memory accesses.
746 StatInc(thr, StatMop);
747 StatInc(thr, kAccessIsWrite ? StatMopWrite : StatMopRead);
748 StatInc(thr, (StatType)(StatMop1 + kAccessSizeLog));
749 StatInc(thr, StatMopRodata);
753 FastState fast_state = thr->fast_state;
754 if (fast_state.GetIgnoreBit()) {
755 StatInc(thr, StatMop);
756 StatInc(thr, kAccessIsWrite ? StatMopWrite : StatMopRead);
757 StatInc(thr, (StatType)(StatMop1 + kAccessSizeLog));
758 StatInc(thr, StatMopIgnored);
762 Shadow cur(fast_state);
763 cur.SetAddr0AndSizeLog(addr & 7, kAccessSizeLog);
764 cur.SetWrite(kAccessIsWrite);
765 cur.SetAtomic(kIsAtomic);
767 if (LIKELY(ContainsSameAccess(shadow_mem, cur.raw(),
768 thr->fast_synch_epoch, kAccessIsWrite))) {
769 StatInc(thr, StatMop);
770 StatInc(thr, kAccessIsWrite ? StatMopWrite : StatMopRead);
771 StatInc(thr, (StatType)(StatMop1 + kAccessSizeLog));
772 StatInc(thr, StatMopSame);
776 if (kCollectHistory) {
777 fast_state.IncrementEpoch();
778 thr->fast_state = fast_state;
779 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
780 cur.IncrementEpoch();
783 MemoryAccessImpl1(thr, addr, kAccessSizeLog, kAccessIsWrite, kIsAtomic,
787 // Called by MemoryAccessRange in tsan_rtl_thread.cc
789 void MemoryAccessImpl(ThreadState *thr, uptr addr,
790 int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic,
791 u64 *shadow_mem, Shadow cur) {
792 if (LIKELY(ContainsSameAccess(shadow_mem, cur.raw(),
793 thr->fast_synch_epoch, kAccessIsWrite))) {
794 StatInc(thr, StatMop);
795 StatInc(thr, kAccessIsWrite ? StatMopWrite : StatMopRead);
796 StatInc(thr, (StatType)(StatMop1 + kAccessSizeLog));
797 StatInc(thr, StatMopSame);
801 MemoryAccessImpl1(thr, addr, kAccessSizeLog, kAccessIsWrite, kIsAtomic,
805 static void MemoryRangeSet(ThreadState *thr, uptr pc, uptr addr, uptr size,
812 uptr offset = addr % kShadowCell;
814 offset = kShadowCell - offset;
820 DCHECK_EQ(addr % 8, 0);
821 // If a user passes some insane arguments (memset(0)),
822 // let it just crash as usual.
823 if (!IsAppMem(addr) || !IsAppMem(addr + size - 1))
825 // Don't want to touch lots of shadow memory.
826 // If a program maps 10MB stack, there is no need reset the whole range.
827 size = (size + (kShadowCell - 1)) & ~(kShadowCell - 1);
828 // UnmapOrDie/MmapFixedNoReserve does not work on Windows,
829 // so we do it only for C/C++.
830 if (kGoMode || size < common_flags()->clear_shadow_mmap_threshold) {
831 u64 *p = (u64*)MemToShadow(addr);
832 CHECK(IsShadowMem((uptr)p));
833 CHECK(IsShadowMem((uptr)(p + size * kShadowCnt / kShadowCell - 1)));
834 // FIXME: may overwrite a part outside the region
835 for (uptr i = 0; i < size / kShadowCell * kShadowCnt;) {
837 for (uptr j = 1; j < kShadowCnt; j++)
841 // The region is big, reset only beginning and end.
842 const uptr kPageSize = GetPageSizeCached();
843 u64 *begin = (u64*)MemToShadow(addr);
844 u64 *end = begin + size / kShadowCell * kShadowCnt;
846 // Set at least first kPageSize/2 to page boundary.
847 while ((p < begin + kPageSize / kShadowSize / 2) || ((uptr)p % kPageSize)) {
849 for (uptr j = 1; j < kShadowCnt; j++)
852 // Reset middle part.
854 p = RoundDown(end, kPageSize);
855 UnmapOrDie((void*)p1, (uptr)p - (uptr)p1);
856 MmapFixedNoReserve((uptr)p1, (uptr)p - (uptr)p1);
860 for (uptr j = 1; j < kShadowCnt; j++)
866 void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size) {
867 MemoryRangeSet(thr, pc, addr, size, 0);
870 void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size) {
871 // Processing more than 1k (4k of shadow) is expensive,
872 // can cause excessive memory consumption (user does not necessary touch
873 // the whole range) and most likely unnecessary.
876 CHECK_EQ(thr->is_freeing, false);
877 thr->is_freeing = true;
878 MemoryAccessRange(thr, pc, addr, size, true);
879 thr->is_freeing = false;
880 if (kCollectHistory) {
881 thr->fast_state.IncrementEpoch();
882 TraceAddEvent(thr, thr->fast_state, EventTypeMop, pc);
884 Shadow s(thr->fast_state);
888 s.SetAddr0AndSizeLog(0, 3);
889 MemoryRangeSet(thr, pc, addr, size, s.raw());
892 void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size) {
893 if (kCollectHistory) {
894 thr->fast_state.IncrementEpoch();
895 TraceAddEvent(thr, thr->fast_state, EventTypeMop, pc);
897 Shadow s(thr->fast_state);
900 s.SetAddr0AndSizeLog(0, 3);
901 MemoryRangeSet(thr, pc, addr, size, s.raw());
905 void FuncEntry(ThreadState *thr, uptr pc) {
906 StatInc(thr, StatFuncEnter);
907 DPrintf2("#%d: FuncEntry %p\n", (int)thr->fast_state.tid(), (void*)pc);
908 if (kCollectHistory) {
909 thr->fast_state.IncrementEpoch();
910 TraceAddEvent(thr, thr->fast_state, EventTypeFuncEnter, pc);
913 // Shadow stack maintenance can be replaced with
914 // stack unwinding during trace switch (which presumably must be faster).
915 DCHECK_GE(thr->shadow_stack_pos, thr->shadow_stack);
917 DCHECK_LT(thr->shadow_stack_pos, thr->shadow_stack_end);
919 if (thr->shadow_stack_pos == thr->shadow_stack_end)
920 GrowShadowStack(thr);
922 thr->shadow_stack_pos[0] = pc;
923 thr->shadow_stack_pos++;
927 void FuncExit(ThreadState *thr) {
928 StatInc(thr, StatFuncExit);
929 DPrintf2("#%d: FuncExit\n", (int)thr->fast_state.tid());
930 if (kCollectHistory) {
931 thr->fast_state.IncrementEpoch();
932 TraceAddEvent(thr, thr->fast_state, EventTypeFuncExit, 0);
935 DCHECK_GT(thr->shadow_stack_pos, thr->shadow_stack);
937 DCHECK_LT(thr->shadow_stack_pos, thr->shadow_stack_end);
939 thr->shadow_stack_pos--;
942 void ThreadIgnoreBegin(ThreadState *thr, uptr pc) {
943 DPrintf("#%d: ThreadIgnoreBegin\n", thr->tid);
944 thr->ignore_reads_and_writes++;
945 CHECK_GT(thr->ignore_reads_and_writes, 0);
946 thr->fast_state.SetIgnoreBit();
948 if (!ctx->after_multithreaded_fork)
949 thr->mop_ignore_set.Add(CurrentStackId(thr, pc));
953 void ThreadIgnoreEnd(ThreadState *thr, uptr pc) {
954 DPrintf("#%d: ThreadIgnoreEnd\n", thr->tid);
955 thr->ignore_reads_and_writes--;
956 CHECK_GE(thr->ignore_reads_and_writes, 0);
957 if (thr->ignore_reads_and_writes == 0) {
958 thr->fast_state.ClearIgnoreBit();
960 thr->mop_ignore_set.Reset();
965 void ThreadIgnoreSyncBegin(ThreadState *thr, uptr pc) {
966 DPrintf("#%d: ThreadIgnoreSyncBegin\n", thr->tid);
968 CHECK_GT(thr->ignore_sync, 0);
970 if (!ctx->after_multithreaded_fork)
971 thr->sync_ignore_set.Add(CurrentStackId(thr, pc));
975 void ThreadIgnoreSyncEnd(ThreadState *thr, uptr pc) {
976 DPrintf("#%d: ThreadIgnoreSyncEnd\n", thr->tid);
978 CHECK_GE(thr->ignore_sync, 0);
980 if (thr->ignore_sync == 0)
981 thr->sync_ignore_set.Reset();
985 bool MD5Hash::operator==(const MD5Hash &other) const {
986 return hash[0] == other.hash[0] && hash[1] == other.hash[1];
990 void build_consistency_debug() {}
992 void build_consistency_release() {}
995 #if TSAN_COLLECT_STATS
996 void build_consistency_stats() {}
998 void build_consistency_nostats() {}
1001 } // namespace __tsan
1003 #ifndef SANITIZER_GO
1004 // Must be included in this file to make sure everything is inlined.
1005 #include "tsan_interface_inl.h"