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