Alexey Samsonov | 603c4be | 2012-06-04 13:55:19 +0000 | [diff] [blame] | 1 | //===-- tsan_rtl.cc -------------------------------------------------------===// |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 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 | // Main file (entry points) for the TSan run-time. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Dmitry Vyukov | fce5bd4 | 2012-06-29 16:58:33 +0000 | [diff] [blame] | 15 | #include "sanitizer_common/sanitizer_atomic.h" |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 16 | #include "sanitizer_common/sanitizer_common.h" |
Kostya Serebryany | 16e0075 | 2012-05-31 13:42:53 +0000 | [diff] [blame] | 17 | #include "sanitizer_common/sanitizer_libc.h" |
Dmitry Vyukov | 8485311 | 2012-08-31 17:27:49 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_stackdepot.h" |
Alexey Samsonov | 47b1634 | 2012-06-07 09:50:16 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_placement_new.h" |
Alexey Samsonov | 8cc1f81 | 2012-09-06 08:48:43 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_symbolizer.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 21 | #include "tsan_defs.h" |
| 22 | #include "tsan_platform.h" |
| 23 | #include "tsan_rtl.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 24 | #include "tsan_mman.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 25 | #include "tsan_suppressions.h" |
Dmitry Vyukov | a38e40f | 2013-03-21 07:02:36 +0000 | [diff] [blame] | 26 | #include "tsan_symbolize.h" |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 27 | #include "ubsan/ubsan_init.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 28 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 29 | #ifdef __SSE3__ |
| 30 | // <emmintrin.h> transitively includes <stdlib.h>, |
| 31 | // and it's prohibited to include std headers into tsan runtime. |
| 32 | // So we do this dirty trick. |
| 33 | #define _MM_MALLOC_H_INCLUDED |
| 34 | #define __MM_MALLOC_H |
| 35 | #include <emmintrin.h> |
| 36 | typedef __m128i m128; |
| 37 | #endif |
| 38 | |
Dmitry Vyukov | adfb650 | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 39 | volatile int __tsan_resumed = 0; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 40 | |
| 41 | extern "C" void __tsan_resume() { |
Dmitry Vyukov | adfb650 | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 42 | __tsan_resumed = 1; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | namespace __tsan { |
| 46 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 47 | #ifndef SANITIZER_GO |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 48 | THREADLOCAL char cur_thread_placeholder[sizeof(ThreadState)] ALIGNED(64); |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 49 | #endif |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 50 | static char ctx_placeholder[sizeof(Context)] ALIGNED(64); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 51 | Context *ctx; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 52 | |
Dmitry Vyukov | 22881ec | 2013-01-30 09:24:00 +0000 | [diff] [blame] | 53 | // Can be overriden by a front-end. |
Dmitry Vyukov | 6a135be | 2013-10-14 06:31:03 +0000 | [diff] [blame] | 54 | #ifdef TSAN_EXTERNAL_HOOKS |
| 55 | bool OnFinalize(bool failed); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 56 | void OnInitialize(); |
Dmitry Vyukov | 6a135be | 2013-10-14 06:31:03 +0000 | [diff] [blame] | 57 | #else |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 58 | SANITIZER_INTERFACE_ATTRIBUTE |
Dmitry Vyukov | 6a135be | 2013-10-14 06:31:03 +0000 | [diff] [blame] | 59 | bool WEAK OnFinalize(bool failed) { |
Dmitry Vyukov | 22881ec | 2013-01-30 09:24:00 +0000 | [diff] [blame] | 60 | return failed; |
| 61 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 62 | SANITIZER_INTERFACE_ATTRIBUTE |
| 63 | void WEAK OnInitialize() {} |
Dmitry Vyukov | 6a135be | 2013-10-14 06:31:03 +0000 | [diff] [blame] | 64 | #endif |
Dmitry Vyukov | 22881ec | 2013-01-30 09:24:00 +0000 | [diff] [blame] | 65 | |
Alexey Samsonov | 2bbd8be | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 66 | static char thread_registry_placeholder[sizeof(ThreadRegistry)]; |
| 67 | |
| 68 | static ThreadContextBase *CreateThreadContext(u32 tid) { |
Alexey Samsonov | 2bbd8be | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 69 | // Map thread trace when context is created. |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 70 | char name[50]; |
| 71 | internal_snprintf(name, sizeof(name), "trace %u", tid); |
| 72 | MapThreadTrace(GetThreadTrace(tid), TraceSize() * sizeof(Event), name); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 73 | const uptr hdr = GetThreadTraceHeader(tid); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 74 | internal_snprintf(name, sizeof(name), "trace header %u", tid); |
| 75 | MapThreadTrace(hdr, sizeof(Trace), name); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 76 | new((void*)hdr) Trace(); |
| 77 | // We are going to use only a small part of the trace with the default |
| 78 | // value of history_size. However, the constructor writes to the whole trace. |
| 79 | // Unmap the unused part. |
| 80 | uptr hdr_end = hdr + sizeof(Trace); |
| 81 | hdr_end -= sizeof(TraceHeader) * (kTraceParts - TraceParts()); |
| 82 | hdr_end = RoundUp(hdr_end, GetPageSizeCached()); |
| 83 | if (hdr_end < hdr + sizeof(Trace)) |
| 84 | UnmapOrDie((void*)hdr_end, hdr + sizeof(Trace) - hdr_end); |
Dmitry Vyukov | 9743d74 | 2013-03-20 10:31:53 +0000 | [diff] [blame] | 85 | void *mem = internal_alloc(MBlockThreadContex, sizeof(ThreadContext)); |
Alexey Samsonov | 2bbd8be | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 86 | return new(mem) ThreadContext(tid); |
| 87 | } |
| 88 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 89 | #ifndef SANITIZER_GO |
Alexey Samsonov | 2bbd8be | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 90 | static const u32 kThreadQuarantineSize = 16; |
| 91 | #else |
| 92 | static const u32 kThreadQuarantineSize = 64; |
| 93 | #endif |
| 94 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 95 | Context::Context() |
| 96 | : initialized() |
| 97 | , report_mtx(MutexTypeReport, StatMtxReport) |
| 98 | , nreported() |
| 99 | , nmissed_expected() |
Alexey Samsonov | 2bbd8be | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 100 | , thread_registry(new(thread_registry_placeholder) ThreadRegistry( |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 101 | CreateThreadContext, kMaxTid, kThreadQuarantineSize, kMaxTidReuse)) |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 102 | , racy_stacks(MBlockRacyStacks) |
Dmitry Vyukov | 158c6ac | 2012-10-05 15:51:32 +0000 | [diff] [blame] | 103 | , racy_addresses(MBlockRacyAddresses) |
Alexey Samsonov | 0a05e5f | 2013-06-14 11:18:58 +0000 | [diff] [blame] | 104 | , fired_suppressions(8) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // The objects are allocated in TLS, so one may rely on zero-initialization. |
Dmitry Vyukov | ff35f1d | 2012-08-30 13:02:30 +0000 | [diff] [blame] | 108 | ThreadState::ThreadState(Context *ctx, int tid, int unique_id, u64 epoch, |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 109 | unsigned reuse_count, |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 110 | uptr stk_addr, uptr stk_size, |
| 111 | uptr tls_addr, uptr tls_size) |
| 112 | : fast_state(tid, epoch) |
| 113 | // Do not touch these, rely on zero initialization, |
| 114 | // they may be accessed before the ctor. |
Dmitry Vyukov | dc563c0 | 2013-05-21 08:12:35 +0000 | [diff] [blame] | 115 | // , ignore_reads_and_writes() |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 116 | // , ignore_interceptors() |
| 117 | , clock(tid, reuse_count) |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 118 | #ifndef SANITIZER_GO |
Dmitry Vyukov | 8b30c25 | 2013-03-25 10:10:44 +0000 | [diff] [blame] | 119 | , jmp_bufs(MBlockJmpBuf) |
| 120 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 121 | , tid(tid) |
Dmitry Vyukov | ff35f1d | 2012-08-30 13:02:30 +0000 | [diff] [blame] | 122 | , unique_id(unique_id) |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 123 | , stk_addr(stk_addr) |
| 124 | , stk_size(stk_size) |
| 125 | , tls_addr(tls_addr) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 126 | , tls_size(tls_size) |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 127 | #ifndef SANITIZER_GO |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 128 | , last_sleep_clock(tid) |
| 129 | #endif |
| 130 | { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 133 | #ifndef SANITIZER_GO |
Dmitry Vyukov | a38e40f | 2013-03-21 07:02:36 +0000 | [diff] [blame] | 134 | static void MemoryProfiler(Context *ctx, fd_t fd, int i) { |
Dmitry Vyukov | 4bebe7b | 2013-03-21 06:24:31 +0000 | [diff] [blame] | 135 | uptr n_threads; |
| 136 | uptr n_running_threads; |
| 137 | ctx->thread_registry->GetNumberOfThreads(&n_threads, &n_running_threads); |
Dmitry Vyukov | a38e40f | 2013-03-21 07:02:36 +0000 | [diff] [blame] | 138 | InternalScopedBuffer<char> buf(4096); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 139 | WriteMemoryProfile(buf.data(), buf.size(), n_threads, n_running_threads); |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 140 | WriteToFile(fd, buf.data(), internal_strlen(buf.data())); |
Dmitry Vyukov | 2612773 | 2012-05-22 11:33:03 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Dmitry Vyukov | 4bebe7b | 2013-03-21 06:24:31 +0000 | [diff] [blame] | 143 | static void BackgroundThread(void *arg) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 144 | // This is a non-initialized non-user thread, nothing to see here. |
| 145 | // We don't use ScopedIgnoreInterceptors, because we want ignores to be |
| 146 | // enabled even when the thread function exits (e.g. during pthread thread |
| 147 | // shutdown code). |
| 148 | cur_thread()->ignore_interceptors++; |
Dmitry Vyukov | f63dde3 | 2013-03-21 13:01:50 +0000 | [diff] [blame] | 149 | const u64 kMs2Ns = 1000 * 1000; |
Dmitry Vyukov | 4bebe7b | 2013-03-21 06:24:31 +0000 | [diff] [blame] | 150 | |
| 151 | fd_t mprof_fd = kInvalidFd; |
| 152 | if (flags()->profile_memory && flags()->profile_memory[0]) { |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 153 | if (internal_strcmp(flags()->profile_memory, "stdout") == 0) { |
| 154 | mprof_fd = 1; |
| 155 | } else if (internal_strcmp(flags()->profile_memory, "stderr") == 0) { |
| 156 | mprof_fd = 2; |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 157 | } else { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 158 | InternalScopedString filename(kMaxPathLength); |
| 159 | filename.append("%s.%d", flags()->profile_memory, (int)internal_getpid()); |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 160 | fd_t fd = OpenFile(filename.data(), WrOnly); |
| 161 | if (fd == kInvalidFd) { |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 162 | Printf("ThreadSanitizer: failed to open memory profile file '%s'\n", |
| 163 | &filename[0]); |
| 164 | } else { |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 165 | mprof_fd = fd; |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 166 | } |
Dmitry Vyukov | 4bebe7b | 2013-03-21 06:24:31 +0000 | [diff] [blame] | 167 | } |
Dmitry Vyukov | 2612773 | 2012-05-22 11:33:03 +0000 | [diff] [blame] | 168 | } |
Dmitry Vyukov | 4bebe7b | 2013-03-21 06:24:31 +0000 | [diff] [blame] | 169 | |
| 170 | u64 last_flush = NanoTime(); |
Dmitry Vyukov | 92b5479 | 2013-10-03 17:14:35 +0000 | [diff] [blame] | 171 | uptr last_rss = 0; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 172 | for (int i = 0; |
| 173 | atomic_load(&ctx->stop_background_thread, memory_order_relaxed) == 0; |
| 174 | i++) { |
| 175 | SleepForMillis(100); |
Dmitry Vyukov | 4bebe7b | 2013-03-21 06:24:31 +0000 | [diff] [blame] | 176 | u64 now = NanoTime(); |
| 177 | |
Dmitry Vyukov | a38e40f | 2013-03-21 07:02:36 +0000 | [diff] [blame] | 178 | // Flush memory if requested. |
Dmitry Vyukov | 92b5479 | 2013-10-03 17:14:35 +0000 | [diff] [blame] | 179 | if (flags()->flush_memory_ms > 0) { |
Dmitry Vyukov | f63dde3 | 2013-03-21 13:01:50 +0000 | [diff] [blame] | 180 | if (last_flush + flags()->flush_memory_ms * kMs2Ns < now) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 181 | VPrintf(1, "ThreadSanitizer: periodic memory flush\n"); |
Dmitry Vyukov | 4bebe7b | 2013-03-21 06:24:31 +0000 | [diff] [blame] | 182 | FlushShadowMemory(); |
| 183 | last_flush = NanoTime(); |
| 184 | } |
| 185 | } |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 186 | // GetRSS can be expensive on huge programs, so don't do it every 100ms. |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 187 | if (flags()->memory_limit_mb > 0) { |
Dmitry Vyukov | 92b5479 | 2013-10-03 17:14:35 +0000 | [diff] [blame] | 188 | uptr rss = GetRSS(); |
| 189 | uptr limit = uptr(flags()->memory_limit_mb) << 20; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 190 | VPrintf(1, "ThreadSanitizer: memory flush check" |
| 191 | " RSS=%llu LAST=%llu LIMIT=%llu\n", |
| 192 | (u64)rss >> 20, (u64)last_rss >> 20, (u64)limit >> 20); |
Dmitry Vyukov | 92b5479 | 2013-10-03 17:14:35 +0000 | [diff] [blame] | 193 | if (2 * rss > limit + last_rss) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 194 | VPrintf(1, "ThreadSanitizer: flushing memory due to RSS\n"); |
Dmitry Vyukov | 92b5479 | 2013-10-03 17:14:35 +0000 | [diff] [blame] | 195 | FlushShadowMemory(); |
| 196 | rss = GetRSS(); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 197 | VPrintf(1, "ThreadSanitizer: memory flushed RSS=%llu\n", (u64)rss>>20); |
Dmitry Vyukov | 92b5479 | 2013-10-03 17:14:35 +0000 | [diff] [blame] | 198 | } |
| 199 | last_rss = rss; |
| 200 | } |
Dmitry Vyukov | 4bebe7b | 2013-03-21 06:24:31 +0000 | [diff] [blame] | 201 | |
Dmitry Vyukov | a38e40f | 2013-03-21 07:02:36 +0000 | [diff] [blame] | 202 | // Write memory profile if requested. |
Dmitry Vyukov | 4bebe7b | 2013-03-21 06:24:31 +0000 | [diff] [blame] | 203 | if (mprof_fd != kInvalidFd) |
Dmitry Vyukov | a38e40f | 2013-03-21 07:02:36 +0000 | [diff] [blame] | 204 | MemoryProfiler(ctx, mprof_fd, i); |
| 205 | |
Dmitry Vyukov | f63dde3 | 2013-03-21 13:01:50 +0000 | [diff] [blame] | 206 | // Flush symbolizer cache if requested. |
| 207 | if (flags()->flush_symbolizer_ms > 0) { |
| 208 | u64 last = atomic_load(&ctx->last_symbolize_time_ns, |
| 209 | memory_order_relaxed); |
| 210 | if (last != 0 && last + flags()->flush_symbolizer_ms * kMs2Ns < now) { |
| 211 | Lock l(&ctx->report_mtx); |
Alexey Samsonov | 7ed46ff | 2013-04-05 07:30:29 +0000 | [diff] [blame] | 212 | SpinMutexLock l2(&CommonSanitizerReportMutex); |
Dmitry Vyukov | f63dde3 | 2013-03-21 13:01:50 +0000 | [diff] [blame] | 213 | SymbolizeFlush(); |
| 214 | atomic_store(&ctx->last_symbolize_time_ns, 0, memory_order_relaxed); |
| 215 | } |
Dmitry Vyukov | a38e40f | 2013-03-21 07:02:36 +0000 | [diff] [blame] | 216 | } |
Dmitry Vyukov | 4bebe7b | 2013-03-21 06:24:31 +0000 | [diff] [blame] | 217 | } |
Dmitry Vyukov | 2612773 | 2012-05-22 11:33:03 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 220 | static void StartBackgroundThread() { |
| 221 | ctx->background_thread = internal_start_thread(&BackgroundThread, 0); |
| 222 | } |
| 223 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 224 | #ifndef __mips__ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 225 | static void StopBackgroundThread() { |
| 226 | atomic_store(&ctx->stop_background_thread, 1, memory_order_relaxed); |
| 227 | internal_join_thread(ctx->background_thread); |
| 228 | ctx->background_thread = 0; |
| 229 | } |
| 230 | #endif |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 231 | #endif |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 232 | |
Dmitry Vyukov | 7ac33ac | 2013-03-18 15:49:07 +0000 | [diff] [blame] | 233 | void DontNeedShadowFor(uptr addr, uptr size) { |
| 234 | uptr shadow_beg = MemToShadow(addr); |
| 235 | uptr shadow_end = MemToShadow(addr + size); |
| 236 | FlushUnneededShadowMemory(shadow_beg, shadow_end - shadow_beg); |
| 237 | } |
| 238 | |
Dmitry Vyukov | a05fcc1 | 2012-11-06 16:00:16 +0000 | [diff] [blame] | 239 | void MapShadow(uptr addr, uptr size) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 240 | // Global data is not 64K aligned, but there are no adjacent mappings, |
| 241 | // so we can get away with unaligned mapping. |
| 242 | // CHECK_EQ(addr, addr & ~((64 << 10) - 1)); // windows wants 64K alignment |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 243 | MmapFixedNoReserve(MemToShadow(addr), size * kShadowMultiplier, "shadow"); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 244 | |
| 245 | // Meta shadow is 2:1, so tread carefully. |
| 246 | static bool data_mapped = false; |
| 247 | static uptr mapped_meta_end = 0; |
| 248 | uptr meta_begin = (uptr)MemToMeta(addr); |
| 249 | uptr meta_end = (uptr)MemToMeta(addr + size); |
| 250 | meta_begin = RoundDownTo(meta_begin, 64 << 10); |
| 251 | meta_end = RoundUpTo(meta_end, 64 << 10); |
| 252 | if (!data_mapped) { |
| 253 | // First call maps data+bss. |
| 254 | data_mapped = true; |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 255 | MmapFixedNoReserve(meta_begin, meta_end - meta_begin, "meta shadow"); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 256 | } else { |
| 257 | // Mapping continous heap. |
| 258 | // Windows wants 64K alignment. |
| 259 | meta_begin = RoundDownTo(meta_begin, 64 << 10); |
| 260 | meta_end = RoundUpTo(meta_end, 64 << 10); |
| 261 | if (meta_end <= mapped_meta_end) |
| 262 | return; |
| 263 | if (meta_begin < mapped_meta_end) |
| 264 | meta_begin = mapped_meta_end; |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 265 | MmapFixedNoReserve(meta_begin, meta_end - meta_begin, "meta shadow"); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 266 | mapped_meta_end = meta_end; |
| 267 | } |
| 268 | VPrintf(2, "mapped meta shadow for (%p-%p) at (%p-%p)\n", |
| 269 | addr, addr+size, meta_begin, meta_end); |
Dmitry Vyukov | a05fcc1 | 2012-11-06 16:00:16 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 272 | void MapThreadTrace(uptr addr, uptr size, const char *name) { |
Dmitry Vyukov | dae1251 | 2012-12-21 12:30:52 +0000 | [diff] [blame] | 273 | DPrintf("#0: Mapping trace at %p-%p(0x%zx)\n", addr, addr + size, size); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 274 | CHECK_GE(addr, kTraceMemBeg); |
| 275 | CHECK_LE(addr + size, kTraceMemEnd); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 276 | CHECK_EQ(addr, addr & ~((64 << 10) - 1)); // windows wants 64K alignment |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 277 | uptr addr1 = (uptr)MmapFixedNoReserve(addr, size, name); |
Dmitry Vyukov | 01a7ce8 | 2013-10-16 15:35:12 +0000 | [diff] [blame] | 278 | if (addr1 != addr) { |
| 279 | Printf("FATAL: ThreadSanitizer can not mmap thread trace (%p/%p->%p)\n", |
| 280 | addr, size, addr1); |
Dmitry Vyukov | 6535c31 | 2012-12-13 08:14:02 +0000 | [diff] [blame] | 281 | Die(); |
| 282 | } |
| 283 | } |
| 284 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 285 | static void CheckShadowMapping() { |
| 286 | for (uptr i = 0; i < ARRAY_SIZE(UserRegions); i += 2) { |
| 287 | const uptr beg = UserRegions[i]; |
| 288 | const uptr end = UserRegions[i + 1]; |
| 289 | VPrintf(3, "checking shadow region %p-%p\n", beg, end); |
| 290 | for (uptr p0 = beg; p0 <= end; p0 += (end - beg) / 4) { |
| 291 | for (int x = -1; x <= 1; x++) { |
| 292 | const uptr p = p0 + x; |
| 293 | if (p < beg || p >= end) |
| 294 | continue; |
| 295 | const uptr s = MemToShadow(p); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 296 | const uptr m = (uptr)MemToMeta(p); |
| 297 | VPrintf(3, " checking pointer %p: shadow=%p meta=%p\n", p, s, m); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 298 | CHECK(IsAppMem(p)); |
| 299 | CHECK(IsShadowMem(s)); |
| 300 | CHECK_EQ(p & ~(kShadowCell - 1), ShadowToMem(s)); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 301 | CHECK(IsMetaMem(m)); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 307 | void Initialize(ThreadState *thr) { |
| 308 | // Thread safe because done before all threads exist. |
| 309 | static bool is_initialized = false; |
| 310 | if (is_initialized) |
| 311 | return; |
| 312 | is_initialized = true; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 313 | // We are not ready to handle interceptors yet. |
| 314 | ScopedIgnoreInterceptors ignore; |
Kostya Serebryany | 859778a | 2013-01-31 14:11:21 +0000 | [diff] [blame] | 315 | SanitizerToolName = "ThreadSanitizer"; |
Alexey Samsonov | 591616d | 2012-09-11 09:44:48 +0000 | [diff] [blame] | 316 | // Install tool-specific callbacks in sanitizer_common. |
| 317 | SetCheckFailedCallback(TsanCheckFailed); |
| 318 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 319 | ctx = new(ctx_placeholder) Context; |
| 320 | const char *options = GetEnv(kTsanOptionsEnv); |
| 321 | InitializeFlags(&ctx->flags, options); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 322 | CacheBinaryName(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 323 | #ifndef SANITIZER_GO |
Dmitry Vyukov | 2e87051 | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 324 | InitializeAllocator(); |
Dmitry Vyukov | bbbb20b | 2012-08-16 19:36:45 +0000 | [diff] [blame] | 325 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 326 | InitializeInterceptors(); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 327 | CheckShadowMapping(); |
| 328 | InitializePlatform(); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 329 | InitializeMutex(); |
| 330 | InitializeDynamicAnnotations(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 331 | #ifndef SANITIZER_GO |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 332 | InitializeShadowMemory(); |
Dmitry Vyukov | a05fcc1 | 2012-11-06 16:00:16 +0000 | [diff] [blame] | 333 | #endif |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 334 | // Setup correct file descriptor for error reports. |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 335 | __sanitizer_set_report_path(common_flags()->log_path); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 336 | InitializeSuppressions(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 337 | #ifndef SANITIZER_GO |
Dmitry Vyukov | 4af0f21 | 2013-10-03 13:37:17 +0000 | [diff] [blame] | 338 | InitializeLibIgnore(); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 339 | Symbolizer::GetOrInit()->AddHooks(EnterSymbolizer, ExitSymbolizer); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 340 | // On MIPS, TSan initialization is run before |
| 341 | // __pthread_initialize_minimal_internal() is finished, so we can not spawn |
| 342 | // new threads. |
| 343 | #ifndef __mips__ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 344 | StartBackgroundThread(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 345 | SetSandboxingCallback(StopBackgroundThread); |
| 346 | #endif |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 347 | #endif |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 348 | if (common_flags()->detect_deadlocks) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 349 | ctx->dd = DDetector::Create(flags()); |
Alexey Samsonov | 8cc1f81 | 2012-09-06 08:48:43 +0000 | [diff] [blame] | 350 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 351 | VPrintf(1, "***** Running under ThreadSanitizer v2 (pid %d) *****\n", |
| 352 | (int)internal_getpid()); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 353 | |
| 354 | // Initialize thread 0. |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 355 | int tid = ThreadCreate(thr, 0, 0, true); |
| 356 | CHECK_EQ(tid, 0); |
Peter Collingbourne | 0b694fc | 2013-05-17 16:56:53 +0000 | [diff] [blame] | 357 | ThreadStart(thr, tid, internal_getpid()); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 358 | #if TSAN_CONTAINS_UBSAN |
| 359 | __ubsan::InitAsPlugin(); |
| 360 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 361 | ctx->initialized = true; |
| 362 | |
Dmitry Vyukov | adfb650 | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 363 | if (flags()->stop_on_start) { |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 364 | Printf("ThreadSanitizer is suspended at startup (pid %d)." |
Dmitry Vyukov | adfb650 | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 365 | " Call __tsan_resume().\n", |
Peter Collingbourne | 0b694fc | 2013-05-17 16:56:53 +0000 | [diff] [blame] | 366 | (int)internal_getpid()); |
Alexey Samsonov | ba5e996 | 2013-01-30 07:45:58 +0000 | [diff] [blame] | 367 | while (__tsan_resumed == 0) {} |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 368 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 369 | |
| 370 | OnInitialize(); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | int Finalize(ThreadState *thr) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 374 | bool failed = false; |
| 375 | |
Dmitry Vyukov | 54e0a9a | 2012-11-07 16:41:57 +0000 | [diff] [blame] | 376 | if (flags()->atexit_sleep_ms > 0 && ThreadCount(thr) > 1) |
| 377 | SleepForMillis(flags()->atexit_sleep_ms); |
| 378 | |
Dmitry Vyukov | d0a51c0 | 2012-10-02 12:07:16 +0000 | [diff] [blame] | 379 | // Wait for pending reports. |
| 380 | ctx->report_mtx.Lock(); |
Alexey Samsonov | 7ed46ff | 2013-04-05 07:30:29 +0000 | [diff] [blame] | 381 | CommonSanitizerReportMutex.Lock(); |
| 382 | CommonSanitizerReportMutex.Unlock(); |
Dmitry Vyukov | d0a51c0 | 2012-10-02 12:07:16 +0000 | [diff] [blame] | 383 | ctx->report_mtx.Unlock(); |
| 384 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 385 | #ifndef SANITIZER_GO |
| 386 | if (Verbosity()) AllocatorPrintStats(); |
Dmitry Vyukov | bdd844c | 2013-01-24 09:08:03 +0000 | [diff] [blame] | 387 | #endif |
| 388 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 389 | ThreadFinalize(thr); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 390 | |
| 391 | if (ctx->nreported) { |
| 392 | failed = true; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 393 | #ifndef SANITIZER_GO |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 394 | Printf("ThreadSanitizer: reported %d warnings\n", ctx->nreported); |
Dmitry Vyukov | b3b2123 | 2012-10-07 14:21:24 +0000 | [diff] [blame] | 395 | #else |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 396 | Printf("Found %d data race(s)\n", ctx->nreported); |
Dmitry Vyukov | b3b2123 | 2012-10-07 14:21:24 +0000 | [diff] [blame] | 397 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | if (ctx->nmissed_expected) { |
| 401 | failed = true; |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 402 | Printf("ThreadSanitizer: missed %d expected races\n", |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 403 | ctx->nmissed_expected); |
| 404 | } |
| 405 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 406 | if (common_flags()->print_suppressions) |
Dmitry Vyukov | f754eb5 | 2013-03-27 17:59:57 +0000 | [diff] [blame] | 407 | PrintMatchedSuppressions(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 408 | #ifndef SANITIZER_GO |
Dmitry Vyukov | 0fd908c | 2013-03-28 16:21:19 +0000 | [diff] [blame] | 409 | if (flags()->print_benign) |
| 410 | PrintMatchedBenignRaces(); |
| 411 | #endif |
Dmitry Vyukov | f754eb5 | 2013-03-27 17:59:57 +0000 | [diff] [blame] | 412 | |
Dmitry Vyukov | 22881ec | 2013-01-30 09:24:00 +0000 | [diff] [blame] | 413 | failed = OnFinalize(failed); |
Dmitry Vyukov | 22881ec | 2013-01-30 09:24:00 +0000 | [diff] [blame] | 414 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 415 | #if TSAN_COLLECT_STATS |
Dmitry Vyukov | 08adb18 | 2012-11-13 13:53:43 +0000 | [diff] [blame] | 416 | StatAggregate(ctx->stat, thr->stat); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 417 | StatOutput(ctx->stat); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 418 | #endif |
| 419 | |
Dmitry Vyukov | b7b6b1c | 2012-05-17 15:00:27 +0000 | [diff] [blame] | 420 | return failed ? flags()->exitcode : 0; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 423 | #ifndef SANITIZER_GO |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 424 | void ForkBefore(ThreadState *thr, uptr pc) { |
| 425 | ctx->thread_registry->Lock(); |
| 426 | ctx->report_mtx.Lock(); |
| 427 | } |
| 428 | |
| 429 | void ForkParentAfter(ThreadState *thr, uptr pc) { |
| 430 | ctx->report_mtx.Unlock(); |
| 431 | ctx->thread_registry->Unlock(); |
| 432 | } |
| 433 | |
| 434 | void ForkChildAfter(ThreadState *thr, uptr pc) { |
| 435 | ctx->report_mtx.Unlock(); |
| 436 | ctx->thread_registry->Unlock(); |
| 437 | |
| 438 | uptr nthread = 0; |
| 439 | ctx->thread_registry->GetNumberOfThreads(0, 0, &nthread /* alive threads */); |
| 440 | VPrintf(1, "ThreadSanitizer: forked new process with pid %d," |
| 441 | " parent had %d threads\n", (int)internal_getpid(), (int)nthread); |
| 442 | if (nthread == 1) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 443 | StartBackgroundThread(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 444 | } else { |
| 445 | // We've just forked a multi-threaded process. We cannot reasonably function |
| 446 | // after that (some mutexes may be locked before fork). So just enable |
| 447 | // ignores for everything in the hope that we will exec soon. |
| 448 | ctx->after_multithreaded_fork = true; |
| 449 | thr->ignore_interceptors++; |
| 450 | ThreadIgnoreBegin(thr, pc); |
| 451 | ThreadIgnoreSyncBegin(thr, pc); |
| 452 | } |
| 453 | } |
| 454 | #endif |
| 455 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 456 | #ifdef SANITIZER_GO |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 457 | NOINLINE |
| 458 | void GrowShadowStack(ThreadState *thr) { |
| 459 | const int sz = thr->shadow_stack_end - thr->shadow_stack; |
| 460 | const int newsz = 2 * sz; |
| 461 | uptr *newstack = (uptr*)internal_alloc(MBlockShadowStack, |
| 462 | newsz * sizeof(uptr)); |
| 463 | internal_memcpy(newstack, thr->shadow_stack, sz * sizeof(uptr)); |
| 464 | internal_free(thr->shadow_stack); |
| 465 | thr->shadow_stack = newstack; |
| 466 | thr->shadow_stack_pos = newstack + sz; |
| 467 | thr->shadow_stack_end = newstack + newsz; |
| 468 | } |
| 469 | #endif |
| 470 | |
Dmitry Vyukov | 8485311 | 2012-08-31 17:27:49 +0000 | [diff] [blame] | 471 | u32 CurrentStackId(ThreadState *thr, uptr pc) { |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 472 | if (!thr->is_inited) // May happen during bootstrap. |
Dmitry Vyukov | 8485311 | 2012-08-31 17:27:49 +0000 | [diff] [blame] | 473 | return 0; |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 474 | if (pc != 0) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 475 | #ifndef SANITIZER_GO |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 476 | DCHECK_LT(thr->shadow_stack_pos, thr->shadow_stack_end); |
| 477 | #else |
| 478 | if (thr->shadow_stack_pos == thr->shadow_stack_end) |
| 479 | GrowShadowStack(thr); |
| 480 | #endif |
Dmitry Vyukov | 8485311 | 2012-08-31 17:27:49 +0000 | [diff] [blame] | 481 | thr->shadow_stack_pos[0] = pc; |
| 482 | thr->shadow_stack_pos++; |
| 483 | } |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 484 | u32 id = StackDepotPut( |
| 485 | StackTrace(thr->shadow_stack, thr->shadow_stack_pos - thr->shadow_stack)); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 486 | if (pc != 0) |
Dmitry Vyukov | 8485311 | 2012-08-31 17:27:49 +0000 | [diff] [blame] | 487 | thr->shadow_stack_pos--; |
| 488 | return id; |
| 489 | } |
| 490 | |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 491 | void TraceSwitch(ThreadState *thr) { |
Dmitry Vyukov | 9ad7c32 | 2012-06-22 11:08:55 +0000 | [diff] [blame] | 492 | thr->nomalloc++; |
Dmitry Vyukov | 9743d74 | 2013-03-20 10:31:53 +0000 | [diff] [blame] | 493 | Trace *thr_trace = ThreadTrace(thr->tid); |
| 494 | Lock l(&thr_trace->mtx); |
Dmitry Vyukov | 0415ac0 | 2012-12-04 12:19:53 +0000 | [diff] [blame] | 495 | unsigned trace = (thr->fast_state.epoch() / kTracePartSize) % TraceParts(); |
Dmitry Vyukov | 9743d74 | 2013-03-20 10:31:53 +0000 | [diff] [blame] | 496 | TraceHeader *hdr = &thr_trace->headers[trace]; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 497 | hdr->epoch0 = thr->fast_state.epoch(); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 498 | ObtainCurrentStack(thr, 0, &hdr->stack0); |
Dmitry Vyukov | ad9da37 | 2012-12-06 12:16:15 +0000 | [diff] [blame] | 499 | hdr->mset0 = thr->mset; |
Dmitry Vyukov | 9ad7c32 | 2012-06-22 11:08:55 +0000 | [diff] [blame] | 500 | thr->nomalloc--; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Dmitry Vyukov | 9743d74 | 2013-03-20 10:31:53 +0000 | [diff] [blame] | 503 | Trace *ThreadTrace(int tid) { |
| 504 | return (Trace*)GetThreadTraceHeader(tid); |
| 505 | } |
| 506 | |
Dmitry Vyukov | 385542a | 2012-11-28 10:35:31 +0000 | [diff] [blame] | 507 | uptr TraceTopPC(ThreadState *thr) { |
| 508 | Event *events = (Event*)GetThreadTrace(thr->tid); |
Dmitry Vyukov | d698edc | 2012-11-28 12:19:50 +0000 | [diff] [blame] | 509 | uptr pc = events[thr->fast_state.GetTracePos()]; |
Dmitry Vyukov | 385542a | 2012-11-28 10:35:31 +0000 | [diff] [blame] | 510 | return pc; |
| 511 | } |
| 512 | |
Dmitry Vyukov | d698edc | 2012-11-28 12:19:50 +0000 | [diff] [blame] | 513 | uptr TraceSize() { |
| 514 | return (uptr)(1ull << (kTracePartSizeBits + flags()->history_size + 1)); |
| 515 | } |
| 516 | |
Dmitry Vyukov | 0415ac0 | 2012-12-04 12:19:53 +0000 | [diff] [blame] | 517 | uptr TraceParts() { |
| 518 | return TraceSize() / kTracePartSize; |
| 519 | } |
| 520 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 521 | #ifndef SANITIZER_GO |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 522 | extern "C" void __tsan_trace_switch() { |
| 523 | TraceSwitch(cur_thread()); |
| 524 | } |
| 525 | |
| 526 | extern "C" void __tsan_report_race() { |
| 527 | ReportRace(cur_thread()); |
| 528 | } |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 529 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 530 | |
| 531 | ALWAYS_INLINE |
Timur Iskhodzhanov | 43c36e4 | 2013-03-28 22:23:03 +0000 | [diff] [blame] | 532 | Shadow LoadShadow(u64 *p) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 533 | u64 raw = atomic_load((atomic_uint64_t*)p, memory_order_relaxed); |
| 534 | return Shadow(raw); |
| 535 | } |
| 536 | |
| 537 | ALWAYS_INLINE |
Timur Iskhodzhanov | 43c36e4 | 2013-03-28 22:23:03 +0000 | [diff] [blame] | 538 | void StoreShadow(u64 *sp, u64 s) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 539 | atomic_store((atomic_uint64_t*)sp, s, memory_order_relaxed); |
| 540 | } |
| 541 | |
| 542 | ALWAYS_INLINE |
Timur Iskhodzhanov | 43c36e4 | 2013-03-28 22:23:03 +0000 | [diff] [blame] | 543 | void StoreIfNotYetStored(u64 *sp, u64 *s) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 544 | StoreShadow(sp, *s); |
| 545 | *s = 0; |
| 546 | } |
| 547 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 548 | ALWAYS_INLINE |
| 549 | void HandleRace(ThreadState *thr, u64 *shadow_mem, |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 550 | Shadow cur, Shadow old) { |
| 551 | thr->racy_state[0] = cur.raw(); |
| 552 | thr->racy_state[1] = old.raw(); |
| 553 | thr->racy_shadow_addr = shadow_mem; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 554 | #ifndef SANITIZER_GO |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 555 | HACKY_CALL(__tsan_report_race); |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 556 | #else |
| 557 | ReportRace(thr); |
| 558 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 561 | static inline bool HappensBefore(Shadow old, ThreadState *thr) { |
Dmitry Vyukov | c8f0a00 | 2012-11-30 20:02:11 +0000 | [diff] [blame] | 562 | return thr->clock.get(old.TidWithIgnore()) >= old.epoch(); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 565 | ALWAYS_INLINE |
| 566 | void MemoryAccessImpl1(ThreadState *thr, uptr addr, |
Dmitry Vyukov | 334553e | 2013-02-01 09:42:06 +0000 | [diff] [blame] | 567 | int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic, |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 568 | u64 *shadow_mem, Shadow cur) { |
| 569 | StatInc(thr, StatMop); |
| 570 | StatInc(thr, kAccessIsWrite ? StatMopWrite : StatMopRead); |
| 571 | StatInc(thr, (StatType)(StatMop1 + kAccessSizeLog)); |
| 572 | |
| 573 | // This potentially can live in an MMX/SSE scratch register. |
| 574 | // The required intrinsics are: |
| 575 | // __m128i _mm_move_epi64(__m128i*); |
| 576 | // _mm_storel_epi64(u64*, __m128i); |
| 577 | u64 store_word = cur.raw(); |
| 578 | |
| 579 | // scan all the shadow values and dispatch to 4 categories: |
| 580 | // same, replace, candidate and race (see comments below). |
| 581 | // we consider only 3 cases regarding access sizes: |
| 582 | // equal, intersect and not intersect. initially I considered |
| 583 | // larger and smaller as well, it allowed to replace some |
| 584 | // 'candidates' with 'same' or 'replace', but I think |
| 585 | // it's just not worth it (performance- and complexity-wise). |
| 586 | |
Dmitry Vyukov | 286c914 | 2013-03-20 11:22:03 +0000 | [diff] [blame] | 587 | Shadow old(0); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 588 | |
| 589 | // It release mode we manually unroll the loop, |
| 590 | // because empirically gcc generates better code this way. |
| 591 | // However, we can't afford unrolling in debug mode, because the function |
| 592 | // consumes almost 4K of stack. Gtest gives only 4K of stack to death test |
| 593 | // threads, which is not enough for the unrolled loop. |
| 594 | #if SANITIZER_DEBUG |
| 595 | for (int idx = 0; idx < 4; idx++) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 596 | #include "tsan_update_shadow_word_inl.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 597 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 598 | #else |
| 599 | int idx = 0; |
| 600 | #include "tsan_update_shadow_word_inl.h" |
| 601 | idx = 1; |
| 602 | #include "tsan_update_shadow_word_inl.h" |
| 603 | idx = 2; |
| 604 | #include "tsan_update_shadow_word_inl.h" |
| 605 | idx = 3; |
| 606 | #include "tsan_update_shadow_word_inl.h" |
| 607 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 608 | |
| 609 | // we did not find any races and had already stored |
| 610 | // the current access info, so we are done |
| 611 | if (LIKELY(store_word == 0)) |
| 612 | return; |
| 613 | // choose a random candidate slot and replace it |
| 614 | StoreShadow(shadow_mem + (cur.epoch() % kShadowCnt), store_word); |
| 615 | StatInc(thr, StatShadowReplace); |
| 616 | return; |
| 617 | RACE: |
| 618 | HandleRace(thr, shadow_mem, cur, old); |
| 619 | return; |
| 620 | } |
| 621 | |
Dmitry Vyukov | 8ecd0e5 | 2013-04-30 11:56:56 +0000 | [diff] [blame] | 622 | void UnalignedMemoryAccess(ThreadState *thr, uptr pc, uptr addr, |
| 623 | int size, bool kAccessIsWrite, bool kIsAtomic) { |
| 624 | while (size) { |
| 625 | int size1 = 1; |
| 626 | int kAccessSizeLog = kSizeLog1; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 627 | if (size >= 8 && (addr & ~7) == ((addr + 7) & ~7)) { |
Dmitry Vyukov | 8ecd0e5 | 2013-04-30 11:56:56 +0000 | [diff] [blame] | 628 | size1 = 8; |
| 629 | kAccessSizeLog = kSizeLog8; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 630 | } else if (size >= 4 && (addr & ~7) == ((addr + 3) & ~7)) { |
Dmitry Vyukov | 8ecd0e5 | 2013-04-30 11:56:56 +0000 | [diff] [blame] | 631 | size1 = 4; |
| 632 | kAccessSizeLog = kSizeLog4; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 633 | } else if (size >= 2 && (addr & ~7) == ((addr + 1) & ~7)) { |
Dmitry Vyukov | 8ecd0e5 | 2013-04-30 11:56:56 +0000 | [diff] [blame] | 634 | size1 = 2; |
| 635 | kAccessSizeLog = kSizeLog2; |
| 636 | } |
| 637 | MemoryAccess(thr, pc, addr, kAccessSizeLog, kAccessIsWrite, kIsAtomic); |
| 638 | addr += size1; |
| 639 | size -= size1; |
| 640 | } |
| 641 | } |
| 642 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 643 | ALWAYS_INLINE |
| 644 | bool ContainsSameAccessSlow(u64 *s, u64 a, u64 sync_epoch, bool is_write) { |
| 645 | Shadow cur(a); |
| 646 | for (uptr i = 0; i < kShadowCnt; i++) { |
| 647 | Shadow old(LoadShadow(&s[i])); |
| 648 | if (Shadow::Addr0AndSizeAreEqual(cur, old) && |
| 649 | old.TidWithIgnore() == cur.TidWithIgnore() && |
| 650 | old.epoch() > sync_epoch && |
| 651 | old.IsAtomic() == cur.IsAtomic() && |
| 652 | old.IsRead() <= cur.IsRead()) |
| 653 | return true; |
| 654 | } |
| 655 | return false; |
| 656 | } |
| 657 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 658 | #if defined(__SSE3__) |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 659 | #define SHUF(v0, v1, i0, i1, i2, i3) _mm_castps_si128(_mm_shuffle_ps( \ |
| 660 | _mm_castsi128_ps(v0), _mm_castsi128_ps(v1), \ |
| 661 | (i0)*1 + (i1)*4 + (i2)*16 + (i3)*64)) |
| 662 | ALWAYS_INLINE |
| 663 | bool ContainsSameAccessFast(u64 *s, u64 a, u64 sync_epoch, bool is_write) { |
| 664 | // This is an optimized version of ContainsSameAccessSlow. |
| 665 | // load current access into access[0:63] |
| 666 | const m128 access = _mm_cvtsi64_si128(a); |
| 667 | // duplicate high part of access in addr0: |
| 668 | // addr0[0:31] = access[32:63] |
| 669 | // addr0[32:63] = access[32:63] |
| 670 | // addr0[64:95] = access[32:63] |
| 671 | // addr0[96:127] = access[32:63] |
| 672 | const m128 addr0 = SHUF(access, access, 1, 1, 1, 1); |
| 673 | // load 4 shadow slots |
| 674 | const m128 shadow0 = _mm_load_si128((__m128i*)s); |
| 675 | const m128 shadow1 = _mm_load_si128((__m128i*)s + 1); |
| 676 | // load high parts of 4 shadow slots into addr_vect: |
| 677 | // addr_vect[0:31] = shadow0[32:63] |
| 678 | // addr_vect[32:63] = shadow0[96:127] |
| 679 | // addr_vect[64:95] = shadow1[32:63] |
| 680 | // addr_vect[96:127] = shadow1[96:127] |
| 681 | m128 addr_vect = SHUF(shadow0, shadow1, 1, 3, 1, 3); |
| 682 | if (!is_write) { |
| 683 | // set IsRead bit in addr_vect |
| 684 | const m128 rw_mask1 = _mm_cvtsi64_si128(1<<15); |
| 685 | const m128 rw_mask = SHUF(rw_mask1, rw_mask1, 0, 0, 0, 0); |
| 686 | addr_vect = _mm_or_si128(addr_vect, rw_mask); |
| 687 | } |
| 688 | // addr0 == addr_vect? |
| 689 | const m128 addr_res = _mm_cmpeq_epi32(addr0, addr_vect); |
| 690 | // epoch1[0:63] = sync_epoch |
| 691 | const m128 epoch1 = _mm_cvtsi64_si128(sync_epoch); |
| 692 | // epoch[0:31] = sync_epoch[0:31] |
| 693 | // epoch[32:63] = sync_epoch[0:31] |
| 694 | // epoch[64:95] = sync_epoch[0:31] |
| 695 | // epoch[96:127] = sync_epoch[0:31] |
| 696 | const m128 epoch = SHUF(epoch1, epoch1, 0, 0, 0, 0); |
| 697 | // load low parts of shadow cell epochs into epoch_vect: |
| 698 | // epoch_vect[0:31] = shadow0[0:31] |
| 699 | // epoch_vect[32:63] = shadow0[64:95] |
| 700 | // epoch_vect[64:95] = shadow1[0:31] |
| 701 | // epoch_vect[96:127] = shadow1[64:95] |
| 702 | const m128 epoch_vect = SHUF(shadow0, shadow1, 0, 2, 0, 2); |
| 703 | // epoch_vect >= sync_epoch? |
| 704 | const m128 epoch_res = _mm_cmpgt_epi32(epoch_vect, epoch); |
| 705 | // addr_res & epoch_res |
| 706 | const m128 res = _mm_and_si128(addr_res, epoch_res); |
| 707 | // mask[0] = res[7] |
| 708 | // mask[1] = res[15] |
| 709 | // ... |
| 710 | // mask[15] = res[127] |
| 711 | const int mask = _mm_movemask_epi8(res); |
| 712 | return mask != 0; |
| 713 | } |
| 714 | #endif |
| 715 | |
| 716 | ALWAYS_INLINE |
| 717 | bool ContainsSameAccess(u64 *s, u64 a, u64 sync_epoch, bool is_write) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 718 | #if defined(__SSE3__) |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 719 | bool res = ContainsSameAccessFast(s, a, sync_epoch, is_write); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 720 | // NOTE: this check can fail if the shadow is concurrently mutated |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 721 | // by other threads. But it still can be useful if you modify |
| 722 | // ContainsSameAccessFast and want to ensure that it's not completely broken. |
| 723 | // DCHECK_EQ(res, ContainsSameAccessSlow(s, a, sync_epoch, is_write)); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 724 | return res; |
| 725 | #else |
| 726 | return ContainsSameAccessSlow(s, a, sync_epoch, is_write); |
| 727 | #endif |
| 728 | } |
| 729 | |
Kostya Serebryany | d475aa8 | 2013-03-29 09:44:16 +0000 | [diff] [blame] | 730 | ALWAYS_INLINE USED |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 731 | void MemoryAccess(ThreadState *thr, uptr pc, uptr addr, |
Dmitry Vyukov | 334553e | 2013-02-01 09:42:06 +0000 | [diff] [blame] | 732 | int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 733 | u64 *shadow_mem = (u64*)MemToShadow(addr); |
Dmitry Vyukov | 68230a1 | 2012-12-07 19:23:59 +0000 | [diff] [blame] | 734 | DPrintf2("#%d: MemoryAccess: @%p %p size=%d" |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 735 | " is_write=%d shadow_mem=%p {%zx, %zx, %zx, %zx}\n", |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 736 | (int)thr->fast_state.tid(), (void*)pc, (void*)addr, |
| 737 | (int)(1 << kAccessSizeLog), kAccessIsWrite, shadow_mem, |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 738 | (uptr)shadow_mem[0], (uptr)shadow_mem[1], |
| 739 | (uptr)shadow_mem[2], (uptr)shadow_mem[3]); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 740 | #if SANITIZER_DEBUG |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 741 | if (!IsAppMem(addr)) { |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 742 | Printf("Access to non app mem %zx\n", addr); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 743 | DCHECK(IsAppMem(addr)); |
| 744 | } |
| 745 | if (!IsShadowMem((uptr)shadow_mem)) { |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 746 | Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 747 | DCHECK(IsShadowMem((uptr)shadow_mem)); |
| 748 | } |
| 749 | #endif |
| 750 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 751 | if (kCppMode && *shadow_mem == kShadowRodata) { |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 752 | // Access to .rodata section, no races here. |
| 753 | // Measurements show that it can be 10-20% of all memory accesses. |
| 754 | StatInc(thr, StatMop); |
| 755 | StatInc(thr, kAccessIsWrite ? StatMopWrite : StatMopRead); |
| 756 | StatInc(thr, (StatType)(StatMop1 + kAccessSizeLog)); |
| 757 | StatInc(thr, StatMopRodata); |
| 758 | return; |
| 759 | } |
| 760 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 761 | FastState fast_state = thr->fast_state; |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 762 | if (fast_state.GetIgnoreBit()) { |
| 763 | StatInc(thr, StatMop); |
| 764 | StatInc(thr, kAccessIsWrite ? StatMopWrite : StatMopRead); |
| 765 | StatInc(thr, (StatType)(StatMop1 + kAccessSizeLog)); |
| 766 | StatInc(thr, StatMopIgnored); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 767 | return; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 770 | Shadow cur(fast_state); |
| 771 | cur.SetAddr0AndSizeLog(addr & 7, kAccessSizeLog); |
| 772 | cur.SetWrite(kAccessIsWrite); |
Dmitry Vyukov | 334553e | 2013-02-01 09:42:06 +0000 | [diff] [blame] | 773 | cur.SetAtomic(kIsAtomic); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 774 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 775 | if (LIKELY(ContainsSameAccess(shadow_mem, cur.raw(), |
| 776 | thr->fast_synch_epoch, kAccessIsWrite))) { |
| 777 | StatInc(thr, StatMop); |
| 778 | StatInc(thr, kAccessIsWrite ? StatMopWrite : StatMopRead); |
| 779 | StatInc(thr, (StatType)(StatMop1 + kAccessSizeLog)); |
| 780 | StatInc(thr, StatMopSame); |
| 781 | return; |
| 782 | } |
| 783 | |
| 784 | if (kCollectHistory) { |
| 785 | fast_state.IncrementEpoch(); |
| 786 | thr->fast_state = fast_state; |
| 787 | TraceAddEvent(thr, fast_state, EventTypeMop, pc); |
| 788 | cur.IncrementEpoch(); |
| 789 | } |
| 790 | |
| 791 | MemoryAccessImpl1(thr, addr, kAccessSizeLog, kAccessIsWrite, kIsAtomic, |
| 792 | shadow_mem, cur); |
| 793 | } |
| 794 | |
| 795 | // Called by MemoryAccessRange in tsan_rtl_thread.cc |
| 796 | ALWAYS_INLINE USED |
| 797 | void MemoryAccessImpl(ThreadState *thr, uptr addr, |
| 798 | int kAccessSizeLog, bool kAccessIsWrite, bool kIsAtomic, |
| 799 | u64 *shadow_mem, Shadow cur) { |
| 800 | if (LIKELY(ContainsSameAccess(shadow_mem, cur.raw(), |
| 801 | thr->fast_synch_epoch, kAccessIsWrite))) { |
| 802 | StatInc(thr, StatMop); |
| 803 | StatInc(thr, kAccessIsWrite ? StatMopWrite : StatMopRead); |
| 804 | StatInc(thr, (StatType)(StatMop1 + kAccessSizeLog)); |
| 805 | StatInc(thr, StatMopSame); |
| 806 | return; |
| 807 | } |
| 808 | |
| 809 | MemoryAccessImpl1(thr, addr, kAccessSizeLog, kAccessIsWrite, kIsAtomic, |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 810 | shadow_mem, cur); |
| 811 | } |
| 812 | |
| 813 | static void MemoryRangeSet(ThreadState *thr, uptr pc, uptr addr, uptr size, |
| 814 | u64 val) { |
Dmitry Vyukov | 74172de | 2013-03-18 16:56:48 +0000 | [diff] [blame] | 815 | (void)thr; |
| 816 | (void)pc; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 817 | if (size == 0) |
| 818 | return; |
| 819 | // FIXME: fix me. |
| 820 | uptr offset = addr % kShadowCell; |
| 821 | if (offset) { |
| 822 | offset = kShadowCell - offset; |
| 823 | if (size <= offset) |
| 824 | return; |
| 825 | addr += offset; |
| 826 | size -= offset; |
| 827 | } |
Dmitry Vyukov | aaac6e2 | 2012-09-02 12:04:51 +0000 | [diff] [blame] | 828 | DCHECK_EQ(addr % 8, 0); |
| 829 | // If a user passes some insane arguments (memset(0)), |
| 830 | // let it just crash as usual. |
| 831 | if (!IsAppMem(addr) || !IsAppMem(addr + size - 1)) |
| 832 | return; |
Dmitry Vyukov | 74172de | 2013-03-18 16:56:48 +0000 | [diff] [blame] | 833 | // Don't want to touch lots of shadow memory. |
| 834 | // If a program maps 10MB stack, there is no need reset the whole range. |
Dmitry Vyukov | 26af893 | 2012-08-15 16:52:19 +0000 | [diff] [blame] | 835 | size = (size + (kShadowCell - 1)) & ~(kShadowCell - 1); |
Dmitry Vyukov | 9c4d7a4 | 2013-06-13 10:15:44 +0000 | [diff] [blame] | 836 | // UnmapOrDie/MmapFixedNoReserve does not work on Windows, |
| 837 | // so we do it only for C/C++. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 838 | if (kGoMode || size < common_flags()->clear_shadow_mmap_threshold) { |
Dmitry Vyukov | 74172de | 2013-03-18 16:56:48 +0000 | [diff] [blame] | 839 | u64 *p = (u64*)MemToShadow(addr); |
| 840 | CHECK(IsShadowMem((uptr)p)); |
| 841 | CHECK(IsShadowMem((uptr)(p + size * kShadowCnt / kShadowCell - 1))); |
| 842 | // FIXME: may overwrite a part outside the region |
| 843 | for (uptr i = 0; i < size / kShadowCell * kShadowCnt;) { |
| 844 | p[i++] = val; |
| 845 | for (uptr j = 1; j < kShadowCnt; j++) |
| 846 | p[i++] = 0; |
| 847 | } |
| 848 | } else { |
| 849 | // The region is big, reset only beginning and end. |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 850 | const uptr kPageSize = GetPageSizeCached(); |
Dmitry Vyukov | 74172de | 2013-03-18 16:56:48 +0000 | [diff] [blame] | 851 | u64 *begin = (u64*)MemToShadow(addr); |
| 852 | u64 *end = begin + size / kShadowCell * kShadowCnt; |
| 853 | u64 *p = begin; |
| 854 | // Set at least first kPageSize/2 to page boundary. |
| 855 | while ((p < begin + kPageSize / kShadowSize / 2) || ((uptr)p % kPageSize)) { |
| 856 | *p++ = val; |
| 857 | for (uptr j = 1; j < kShadowCnt; j++) |
| 858 | *p++ = 0; |
| 859 | } |
| 860 | // Reset middle part. |
| 861 | u64 *p1 = p; |
| 862 | p = RoundDown(end, kPageSize); |
| 863 | UnmapOrDie((void*)p1, (uptr)p - (uptr)p1); |
| 864 | MmapFixedNoReserve((uptr)p1, (uptr)p - (uptr)p1); |
| 865 | // Set the ending. |
| 866 | while (p < end) { |
| 867 | *p++ = val; |
| 868 | for (uptr j = 1; j < kShadowCnt; j++) |
| 869 | *p++ = 0; |
| 870 | } |
Dmitry Vyukov | 26af893 | 2012-08-15 16:52:19 +0000 | [diff] [blame] | 871 | } |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size) { |
| 875 | MemoryRangeSet(thr, pc, addr, size, 0); |
| 876 | } |
| 877 | |
| 878 | void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size) { |
Dmitry Vyukov | 74172de | 2013-03-18 16:56:48 +0000 | [diff] [blame] | 879 | // Processing more than 1k (4k of shadow) is expensive, |
| 880 | // can cause excessive memory consumption (user does not necessary touch |
| 881 | // the whole range) and most likely unnecessary. |
| 882 | if (size > 1024) |
| 883 | size = 1024; |
Dmitry Vyukov | 3285866 | 2013-02-01 14:41:58 +0000 | [diff] [blame] | 884 | CHECK_EQ(thr->is_freeing, false); |
| 885 | thr->is_freeing = true; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 886 | MemoryAccessRange(thr, pc, addr, size, true); |
Dmitry Vyukov | 3285866 | 2013-02-01 14:41:58 +0000 | [diff] [blame] | 887 | thr->is_freeing = false; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 888 | if (kCollectHistory) { |
| 889 | thr->fast_state.IncrementEpoch(); |
| 890 | TraceAddEvent(thr, thr->fast_state, EventTypeMop, pc); |
| 891 | } |
Dmitry Vyukov | 069ce82 | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 892 | Shadow s(thr->fast_state); |
Dmitry Vyukov | 064c847 | 2012-11-30 06:39:01 +0000 | [diff] [blame] | 893 | s.ClearIgnoreBit(); |
Dmitry Vyukov | 069ce82 | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 894 | s.MarkAsFreed(); |
| 895 | s.SetWrite(true); |
| 896 | s.SetAddr0AndSizeLog(0, 3); |
| 897 | MemoryRangeSet(thr, pc, addr, size, s.raw()); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Dmitry Vyukov | 26af893 | 2012-08-15 16:52:19 +0000 | [diff] [blame] | 900 | void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 901 | if (kCollectHistory) { |
| 902 | thr->fast_state.IncrementEpoch(); |
| 903 | TraceAddEvent(thr, thr->fast_state, EventTypeMop, pc); |
| 904 | } |
Dmitry Vyukov | 26af893 | 2012-08-15 16:52:19 +0000 | [diff] [blame] | 905 | Shadow s(thr->fast_state); |
Dmitry Vyukov | 064c847 | 2012-11-30 06:39:01 +0000 | [diff] [blame] | 906 | s.ClearIgnoreBit(); |
Dmitry Vyukov | 26af893 | 2012-08-15 16:52:19 +0000 | [diff] [blame] | 907 | s.SetWrite(true); |
| 908 | s.SetAddr0AndSizeLog(0, 3); |
| 909 | MemoryRangeSet(thr, pc, addr, size, s.raw()); |
| 910 | } |
| 911 | |
Kostya Serebryany | d475aa8 | 2013-03-29 09:44:16 +0000 | [diff] [blame] | 912 | ALWAYS_INLINE USED |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 913 | void FuncEntry(ThreadState *thr, uptr pc) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 914 | StatInc(thr, StatFuncEnter); |
Dmitry Vyukov | 25d1c79 | 2012-07-16 16:44:47 +0000 | [diff] [blame] | 915 | DPrintf2("#%d: FuncEntry %p\n", (int)thr->fast_state.tid(), (void*)pc); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 916 | if (kCollectHistory) { |
| 917 | thr->fast_state.IncrementEpoch(); |
| 918 | TraceAddEvent(thr, thr->fast_state, EventTypeFuncEnter, pc); |
| 919 | } |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 920 | |
| 921 | // Shadow stack maintenance can be replaced with |
| 922 | // stack unwinding during trace switch (which presumably must be faster). |
Dmitry Vyukov | 01a7ce8 | 2013-10-16 15:35:12 +0000 | [diff] [blame] | 923 | DCHECK_GE(thr->shadow_stack_pos, thr->shadow_stack); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 924 | #ifndef SANITIZER_GO |
Dmitry Vyukov | 01a7ce8 | 2013-10-16 15:35:12 +0000 | [diff] [blame] | 925 | DCHECK_LT(thr->shadow_stack_pos, thr->shadow_stack_end); |
Dmitry Vyukov | 25d1c79 | 2012-07-16 16:44:47 +0000 | [diff] [blame] | 926 | #else |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 927 | if (thr->shadow_stack_pos == thr->shadow_stack_end) |
| 928 | GrowShadowStack(thr); |
Dmitry Vyukov | 25d1c79 | 2012-07-16 16:44:47 +0000 | [diff] [blame] | 929 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 930 | thr->shadow_stack_pos[0] = pc; |
| 931 | thr->shadow_stack_pos++; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Kostya Serebryany | d475aa8 | 2013-03-29 09:44:16 +0000 | [diff] [blame] | 934 | ALWAYS_INLINE USED |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 935 | void FuncExit(ThreadState *thr) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 936 | StatInc(thr, StatFuncExit); |
Dmitry Vyukov | 25d1c79 | 2012-07-16 16:44:47 +0000 | [diff] [blame] | 937 | DPrintf2("#%d: FuncExit\n", (int)thr->fast_state.tid()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 938 | if (kCollectHistory) { |
| 939 | thr->fast_state.IncrementEpoch(); |
| 940 | TraceAddEvent(thr, thr->fast_state, EventTypeFuncExit, 0); |
| 941 | } |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 942 | |
Dmitry Vyukov | 01a7ce8 | 2013-10-16 15:35:12 +0000 | [diff] [blame] | 943 | DCHECK_GT(thr->shadow_stack_pos, thr->shadow_stack); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 944 | #ifndef SANITIZER_GO |
Dmitry Vyukov | 01a7ce8 | 2013-10-16 15:35:12 +0000 | [diff] [blame] | 945 | DCHECK_LT(thr->shadow_stack_pos, thr->shadow_stack_end); |
Dmitry Vyukov | 25d1c79 | 2012-07-16 16:44:47 +0000 | [diff] [blame] | 946 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 947 | thr->shadow_stack_pos--; |
| 948 | } |
| 949 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 950 | void ThreadIgnoreBegin(ThreadState *thr, uptr pc) { |
Dmitry Vyukov | 652f78a | 2013-09-19 04:39:04 +0000 | [diff] [blame] | 951 | DPrintf("#%d: ThreadIgnoreBegin\n", thr->tid); |
| 952 | thr->ignore_reads_and_writes++; |
Dmitry Vyukov | e1ddbf9 | 2013-10-10 15:58:12 +0000 | [diff] [blame] | 953 | CHECK_GT(thr->ignore_reads_and_writes, 0); |
Dmitry Vyukov | 652f78a | 2013-09-19 04:39:04 +0000 | [diff] [blame] | 954 | thr->fast_state.SetIgnoreBit(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 955 | #ifndef SANITIZER_GO |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 956 | if (!ctx->after_multithreaded_fork) |
| 957 | thr->mop_ignore_set.Add(CurrentStackId(thr, pc)); |
| 958 | #endif |
Dmitry Vyukov | 652f78a | 2013-09-19 04:39:04 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 961 | void ThreadIgnoreEnd(ThreadState *thr, uptr pc) { |
Dmitry Vyukov | 652f78a | 2013-09-19 04:39:04 +0000 | [diff] [blame] | 962 | DPrintf("#%d: ThreadIgnoreEnd\n", thr->tid); |
| 963 | thr->ignore_reads_and_writes--; |
| 964 | CHECK_GE(thr->ignore_reads_and_writes, 0); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 965 | if (thr->ignore_reads_and_writes == 0) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 966 | thr->fast_state.ClearIgnoreBit(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 967 | #ifndef SANITIZER_GO |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 968 | thr->mop_ignore_set.Reset(); |
| 969 | #endif |
| 970 | } |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 973 | void ThreadIgnoreSyncBegin(ThreadState *thr, uptr pc) { |
Dmitry Vyukov | e1ddbf9 | 2013-10-10 15:58:12 +0000 | [diff] [blame] | 974 | DPrintf("#%d: ThreadIgnoreSyncBegin\n", thr->tid); |
| 975 | thr->ignore_sync++; |
| 976 | CHECK_GT(thr->ignore_sync, 0); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 977 | #ifndef SANITIZER_GO |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 978 | if (!ctx->after_multithreaded_fork) |
| 979 | thr->sync_ignore_set.Add(CurrentStackId(thr, pc)); |
| 980 | #endif |
Dmitry Vyukov | e1ddbf9 | 2013-10-10 15:58:12 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 983 | void ThreadIgnoreSyncEnd(ThreadState *thr, uptr pc) { |
Dmitry Vyukov | e1ddbf9 | 2013-10-10 15:58:12 +0000 | [diff] [blame] | 984 | DPrintf("#%d: ThreadIgnoreSyncEnd\n", thr->tid); |
| 985 | thr->ignore_sync--; |
| 986 | CHECK_GE(thr->ignore_sync, 0); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 987 | #ifndef SANITIZER_GO |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 988 | if (thr->ignore_sync == 0) |
| 989 | thr->sync_ignore_set.Reset(); |
| 990 | #endif |
Dmitry Vyukov | e1ddbf9 | 2013-10-10 15:58:12 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 993 | bool MD5Hash::operator==(const MD5Hash &other) const { |
| 994 | return hash[0] == other.hash[0] && hash[1] == other.hash[1]; |
| 995 | } |
| 996 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 997 | #if SANITIZER_DEBUG |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 998 | void build_consistency_debug() {} |
| 999 | #else |
| 1000 | void build_consistency_release() {} |
| 1001 | #endif |
| 1002 | |
| 1003 | #if TSAN_COLLECT_STATS |
| 1004 | void build_consistency_stats() {} |
| 1005 | #else |
| 1006 | void build_consistency_nostats() {} |
| 1007 | #endif |
| 1008 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 1009 | } // namespace __tsan |
| 1010 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 1011 | #ifndef SANITIZER_GO |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 1012 | // Must be included in this file to make sure everything is inlined. |
| 1013 | #include "tsan_interface_inl.h" |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 1014 | #endif |