blob: dcae255f76439f8fc346ad992c868aee51e1acec [file] [log] [blame]
Alexey Samsonov603c4be2012-06-04 13:55:19 +00001//===-- tsan_rtl_thread.cc ------------------------------------------------===//
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of ThreadSanitizer (TSan), a race detector.
11//
12//===----------------------------------------------------------------------===//
13
Alexey Samsonov47b16342012-06-07 09:50:16 +000014#include "sanitizer_common/sanitizer_placement_new.h"
Kostya Serebryany7ac41482012-05-10 13:48:04 +000015#include "tsan_rtl.h"
16#include "tsan_mman.h"
Kostya Serebryany7ac41482012-05-10 13:48:04 +000017#include "tsan_platform.h"
18#include "tsan_report.h"
19#include "tsan_sync.h"
20
21namespace __tsan {
22
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000023// ThreadContext implementation.
Kostya Serebryany7ac41482012-05-10 13:48:04 +000024
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000025ThreadContext::ThreadContext(int tid)
26 : ThreadContextBase(tid)
27 , thr()
28 , sync()
29 , epoch0()
Dmitry Vyukov9743d742013-03-20 10:31:53 +000030 , epoch1() {
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000031}
32
Stephen Hines86277eb2015-03-23 12:06:32 -070033#ifndef SANITIZER_GO
Dmitry Vyukov6af642e2013-03-18 10:10:15 +000034ThreadContext::~ThreadContext() {
35}
36#endif
37
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000038void ThreadContext::OnDead() {
Stephen Hines6d186232014-11-26 17:56:19 -080039 CHECK_EQ(sync.size(), 0);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000040}
41
42void ThreadContext::OnJoined(void *arg) {
43 ThreadState *caller_thr = static_cast<ThreadState *>(arg);
Dmitry Vyukove1ddbf92013-10-10 15:58:12 +000044 AcquireImpl(caller_thr, 0, &sync);
Stephen Hines6d186232014-11-26 17:56:19 -080045 sync.Reset(&caller_thr->clock_cache);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000046}
47
48struct OnCreatedArgs {
49 ThreadState *thr;
50 uptr pc;
51};
52
53void ThreadContext::OnCreated(void *arg) {
54 thr = 0;
Dmitry Vyukov491852e2013-03-18 08:52:46 +000055 if (tid == 0)
56 return;
57 OnCreatedArgs *args = static_cast<OnCreatedArgs *>(arg);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080058 if (!args->thr) // GCD workers don't have a parent thread.
59 return;
Dmitry Vyukov491852e2013-03-18 08:52:46 +000060 args->thr->fast_state.IncrementEpoch();
61 // Can't increment epoch w/o writing to the trace as well.
62 TraceAddEvent(args->thr, args->thr->fast_state, EventTypeMop, 0);
Dmitry Vyukove1ddbf92013-10-10 15:58:12 +000063 ReleaseImpl(args->thr, 0, &sync);
Dmitry Vyukov2c5284e2013-03-18 09:02:27 +000064 creation_stack_id = CurrentStackId(args->thr, args->pc);
Dmitry Vyukov491852e2013-03-18 08:52:46 +000065 if (reuse_count == 0)
66 StatInc(args->thr, StatThreadMaxTid);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000067}
68
Dmitry Vyukovce85e032013-03-19 12:25:48 +000069void ThreadContext::OnReset() {
Stephen Hines6d186232014-11-26 17:56:19 -080070 CHECK_EQ(sync.size(), 0);
Dmitry Vyukov9743d742013-03-20 10:31:53 +000071 FlushUnneededShadowMemory(GetThreadTrace(tid), TraceSize() * sizeof(Event));
72 //!!! FlushUnneededShadowMemory(GetThreadTraceHeader(tid), sizeof(Trace));
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000073}
74
Stephen Hines6d186232014-11-26 17:56:19 -080075void ThreadContext::OnDetached(void *arg) {
76 ThreadState *thr1 = static_cast<ThreadState*>(arg);
77 sync.Reset(&thr1->clock_cache);
78}
79
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000080struct OnStartedArgs {
81 ThreadState *thr;
82 uptr stk_addr;
83 uptr stk_size;
84 uptr tls_addr;
85 uptr tls_size;
86};
87
88void ThreadContext::OnStarted(void *arg) {
89 OnStartedArgs *args = static_cast<OnStartedArgs*>(arg);
Dmitry Vyukov491852e2013-03-18 08:52:46 +000090 thr = args->thr;
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000091 // RoundUp so that one trace part does not contain events
92 // from different threads.
93 epoch0 = RoundUp(epoch1 + 1, kTracePartSize);
94 epoch1 = (u64)-1;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070095 new(thr) ThreadState(ctx, tid, unique_id, epoch0, reuse_count,
96 args->stk_addr, args->stk_size, args->tls_addr, args->tls_size);
Stephen Hines86277eb2015-03-23 12:06:32 -070097#ifndef SANITIZER_GO
Dmitry Vyukov01a7ce82013-10-16 15:35:12 +000098 thr->shadow_stack = &ThreadTrace(thr->tid)->shadow_stack[0];
99 thr->shadow_stack_pos = thr->shadow_stack;
100 thr->shadow_stack_end = thr->shadow_stack + kShadowStackSize;
101#else
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000102 // Setup dynamic shadow stack.
103 const int kInitStackSize = 8;
Dmitry Vyukov01a7ce82013-10-16 15:35:12 +0000104 thr->shadow_stack = (uptr*)internal_alloc(MBlockShadowStack,
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000105 kInitStackSize * sizeof(uptr));
Dmitry Vyukov01a7ce82013-10-16 15:35:12 +0000106 thr->shadow_stack_pos = thr->shadow_stack;
107 thr->shadow_stack_end = thr->shadow_stack + kInitStackSize;
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000108#endif
Stephen Hines86277eb2015-03-23 12:06:32 -0700109#ifndef SANITIZER_GO
Dmitry Vyukov01a7ce82013-10-16 15:35:12 +0000110 AllocatorThreadStart(thr);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000111#endif
Stephen Hines6d186232014-11-26 17:56:19 -0800112 if (common_flags()->detect_deadlocks) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700113 thr->dd_pt = ctx->dd->CreatePhysicalThread();
114 thr->dd_lt = ctx->dd->CreateLogicalThread(unique_id);
115 }
Stephen Hines86277eb2015-03-23 12:06:32 -0700116 thr->fast_state.SetHistorySize(flags()->history_size);
117 // Commit switch to the new part of the trace.
118 // TraceAddEvent will reset stack0/mset0 in the new part for us.
119 TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
120
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000121 thr->fast_synch_epoch = epoch0;
Dmitry Vyukove1ddbf92013-10-10 15:58:12 +0000122 AcquireImpl(thr, 0, &sync);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000123 StatInc(thr, StatSyncAcquire);
Stephen Hines6d186232014-11-26 17:56:19 -0800124 sync.Reset(&thr->clock_cache);
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700125 thr->is_inited = true;
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000126 DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx "
127 "tls_addr=%zx tls_size=%zx\n",
Alexey Samsonov036f9332013-03-18 09:45:22 +0000128 tid, (uptr)epoch0, args->stk_addr, args->stk_size,
129 args->tls_addr, args->tls_size);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000130}
131
132void ThreadContext::OnFinished() {
133 if (!detached) {
134 thr->fast_state.IncrementEpoch();
135 // Can't increment epoch w/o writing to the trace as well.
136 TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
Dmitry Vyukove1ddbf92013-10-10 15:58:12 +0000137 ReleaseImpl(thr, 0, &sync);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000138 }
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000139 epoch1 = thr->fast_state.epoch();
140
Stephen Hines6d186232014-11-26 17:56:19 -0800141 if (common_flags()->detect_deadlocks) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700142 ctx->dd->DestroyPhysicalThread(thr->dd_pt);
143 ctx->dd->DestroyLogicalThread(thr->dd_lt);
144 }
Stephen Hines6d186232014-11-26 17:56:19 -0800145 ctx->clock_alloc.FlushCache(&thr->clock_cache);
Stephen Hines6a211c52014-07-21 00:49:56 -0700146 ctx->metamap.OnThreadIdle(thr);
Stephen Hines86277eb2015-03-23 12:06:32 -0700147#ifndef SANITIZER_GO
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000148 AllocatorThreadFinish(thr);
149#endif
150 thr->~ThreadState();
Stephen Hines86277eb2015-03-23 12:06:32 -0700151#if TSAN_COLLECT_STATS
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700152 StatAggregate(ctx->stat, thr->stat);
Stephen Hines86277eb2015-03-23 12:06:32 -0700153#endif
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000154 thr = 0;
155}
156
Stephen Hines86277eb2015-03-23 12:06:32 -0700157#ifndef SANITIZER_GO
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000158struct ThreadLeak {
159 ThreadContext *tctx;
160 int count;
161};
162
163static void MaybeReportThreadLeak(ThreadContextBase *tctx_base, void *arg) {
164 Vector<ThreadLeak> &leaks = *(Vector<ThreadLeak>*)arg;
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000165 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000166 if (tctx->detached || tctx->status != ThreadStatusFinished)
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000167 return;
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000168 for (uptr i = 0; i < leaks.Size(); i++) {
169 if (leaks[i].tctx->creation_stack_id == tctx->creation_stack_id) {
170 leaks[i].count++;
171 return;
172 }
173 }
174 ThreadLeak leak = {tctx, 1};
175 leaks.PushBack(leak);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000176}
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000177#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000178
Stephen Hines86277eb2015-03-23 12:06:32 -0700179#ifndef SANITIZER_GO
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700180static void ReportIgnoresEnabled(ThreadContext *tctx, IgnoreSet *set) {
181 if (tctx->tid == 0) {
182 Printf("ThreadSanitizer: main thread finished with ignores enabled\n");
183 } else {
184 Printf("ThreadSanitizer: thread T%d %s finished with ignores enabled,"
185 " created at:\n", tctx->tid, tctx->name);
186 PrintStack(SymbolizeStackId(tctx->creation_stack_id));
Dmitry Vyukovdc563c02013-05-21 08:12:35 +0000187 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700188 Printf(" One of the following ignores was not ended"
189 " (in order of probability)\n");
190 for (uptr i = 0; i < set->Size(); i++) {
191 Printf(" Ignore was enabled at:\n");
192 PrintStack(SymbolizeStackId(set->At(i)));
Dmitry Vyukove1ddbf92013-10-10 15:58:12 +0000193 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700194 Die();
Dmitry Vyukovdc563c02013-05-21 08:12:35 +0000195}
196
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700197static void ThreadCheckIgnore(ThreadState *thr) {
198 if (ctx->after_multithreaded_fork)
199 return;
200 if (thr->ignore_reads_and_writes)
201 ReportIgnoresEnabled(thr->tctx, &thr->mop_ignore_set);
202 if (thr->ignore_sync)
203 ReportIgnoresEnabled(thr->tctx, &thr->sync_ignore_set);
204}
205#else
206static void ThreadCheckIgnore(ThreadState *thr) {}
207#endif
208
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000209void ThreadFinalize(ThreadState *thr) {
Dmitry Vyukovdc563c02013-05-21 08:12:35 +0000210 ThreadCheckIgnore(thr);
Stephen Hines86277eb2015-03-23 12:06:32 -0700211#ifndef SANITIZER_GO
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000212 if (!flags()->report_thread_leaks)
213 return;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700214 ThreadRegistryLock l(ctx->thread_registry);
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000215 Vector<ThreadLeak> leaks(MBlockScopedBuf);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700216 ctx->thread_registry->RunCallbackForEachThreadLocked(
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000217 MaybeReportThreadLeak, &leaks);
218 for (uptr i = 0; i < leaks.Size(); i++) {
219 ScopedReport rep(ReportTypeThreadLeak);
Stephen Hines6a211c52014-07-21 00:49:56 -0700220 rep.AddThread(leaks[i].tctx, true);
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000221 rep.SetCount(leaks[i].count);
Stephen Hines6a211c52014-07-21 00:49:56 -0700222 OutputReport(thr, rep);
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000223 }
224#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000225}
226
Dmitry Vyukov54e0a9a2012-11-07 16:41:57 +0000227int ThreadCount(ThreadState *thr) {
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000228 uptr result;
229 ctx->thread_registry->GetNumberOfThreads(0, 0, &result);
230 return (int)result;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000231}
232
233int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000234 StatInc(thr, StatThreadCreate);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000235 OnCreatedArgs args = { thr, pc };
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800236 u32 parent_tid = thr ? thr->tid : kInvalidTid; // No parent for GCD workers.
237 int tid =
238 ctx->thread_registry->CreateThread(uid, detached, parent_tid, &args);
239 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", parent_tid, tid, uid);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000240 StatSet(thr, StatThreadMaxAlive, ctx->thread_registry->GetMaxAliveThreads());
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000241 return tid;
242}
243
Dmitry Vyukove0023f72012-10-02 12:58:14 +0000244void ThreadStart(ThreadState *thr, int tid, uptr os_id) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000245 uptr stk_addr = 0;
246 uptr stk_size = 0;
247 uptr tls_addr = 0;
248 uptr tls_size = 0;
Stephen Hines86277eb2015-03-23 12:06:32 -0700249#ifndef SANITIZER_GO
Dmitry Vyukov789b6c52012-05-25 11:15:04 +0000250 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000251
Dmitry Vyukovaf154b82012-05-28 07:44:34 +0000252 if (tid) {
Dmitry Vyukov7ac33ac2013-03-18 15:49:07 +0000253 if (stk_addr && stk_size)
Dmitry Vyukov74172de2013-03-18 16:56:48 +0000254 MemoryRangeImitateWrite(thr, /*pc=*/ 1, stk_addr, stk_size);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000255
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000256 if (tls_addr && tls_size) {
257 // Check that the thr object is in tls;
258 const uptr thr_beg = (uptr)thr;
259 const uptr thr_end = (uptr)thr + sizeof(*thr);
260 CHECK_GE(thr_beg, tls_addr);
261 CHECK_LE(thr_beg, tls_addr + tls_size);
262 CHECK_GE(thr_end, tls_addr);
263 CHECK_LE(thr_end, tls_addr + tls_size);
264 // Since the thr object is huge, skip it.
Dmitry Vyukov74172de2013-03-18 16:56:48 +0000265 MemoryRangeImitateWrite(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
266 MemoryRangeImitateWrite(thr, /*pc=*/ 2,
267 thr_end, tls_addr + tls_size - thr_end);
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000268 }
Dmitry Vyukovaf154b82012-05-28 07:44:34 +0000269 }
Stephen Hines86277eb2015-03-23 12:06:32 -0700270#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000271
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700272 ThreadRegistry *tr = ctx->thread_registry;
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000273 OnStartedArgs args = { thr, stk_addr, stk_size, tls_addr, tls_size };
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700274 tr->StartThread(tid, os_id, &args);
275
276 tr->Lock();
277 thr->tctx = (ThreadContext*)tr->GetThreadLocked(tid);
278 tr->Unlock();
279
Stephen Hines86277eb2015-03-23 12:06:32 -0700280#ifndef SANITIZER_GO
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700281 if (ctx->after_multithreaded_fork) {
282 thr->ignore_interceptors++;
283 ThreadIgnoreBegin(thr, 0);
284 ThreadIgnoreSyncBegin(thr, 0);
285 }
286#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000287}
288
289void ThreadFinish(ThreadState *thr) {
Dmitry Vyukovdc563c02013-05-21 08:12:35 +0000290 ThreadCheckIgnore(thr);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000291 StatInc(thr, StatThreadFinish);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000292 if (thr->stk_addr && thr->stk_size)
Dmitry Vyukov7ac33ac2013-03-18 15:49:07 +0000293 DontNeedShadowFor(thr->stk_addr, thr->stk_size);
294 if (thr->tls_addr && thr->tls_size)
295 DontNeedShadowFor(thr->tls_addr, thr->tls_size);
Stephen Hines6d186232014-11-26 17:56:19 -0800296 thr->is_dead = true;
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000297 ctx->thread_registry->FinishThread(thr->tid);
298}
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000299
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000300static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) {
301 uptr uid = (uptr)arg;
302 if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) {
303 tctx->user_id = 0;
304 return true;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000305 }
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000306 return false;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000307}
308
309int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000310 int res = ctx->thread_registry->FindThread(FindThreadByUid, (void*)uid);
Alexey Samsonove9541012012-06-06 13:11:29 +0000311 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
Dmitry Vyukov411b2c92012-05-28 17:32:50 +0000312 return res;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000313}
314
315void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000316 CHECK_GT(tid, 0);
317 CHECK_LT(tid, kMaxTid);
318 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000319 ctx->thread_registry->JoinThread(tid, thr);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000320}
321
322void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000323 CHECK_GT(tid, 0);
324 CHECK_LT(tid, kMaxTid);
Stephen Hines6d186232014-11-26 17:56:19 -0800325 ctx->thread_registry->DetachThread(tid, thr);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000326}
327
Dmitry Vyukovaecf2e52012-12-04 15:46:05 +0000328void ThreadSetName(ThreadState *thr, const char *name) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700329 ctx->thread_registry->SetThreadName(thr->tid, name);
Dmitry Vyukovaecf2e52012-12-04 15:46:05 +0000330}
331
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000332void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
333 uptr size, bool is_write) {
334 if (size == 0)
335 return;
336
337 u64 *shadow_mem = (u64*)MemToShadow(addr);
338 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
339 thr->tid, (void*)pc, (void*)addr,
340 (int)size, is_write);
341
Stephen Hines86277eb2015-03-23 12:06:32 -0700342#if SANITIZER_DEBUG
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000343 if (!IsAppMem(addr)) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000344 Printf("Access to non app mem %zx\n", addr);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000345 DCHECK(IsAppMem(addr));
346 }
347 if (!IsAppMem(addr + size - 1)) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000348 Printf("Access to non app mem %zx\n", addr + size - 1);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000349 DCHECK(IsAppMem(addr + size - 1));
350 }
351 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000352 Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000353 DCHECK(IsShadowMem((uptr)shadow_mem));
354 }
355 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000356 Printf("Bad shadow addr %p (%zx)\n",
Alexey Samsonove9541012012-06-06 13:11:29 +0000357 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000358 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
359 }
360#endif
361
362 StatInc(thr, StatMopRange);
363
Dmitry Vyukov82dbc512013-03-20 13:21:50 +0000364 if (*shadow_mem == kShadowRodata) {
365 // Access to .rodata section, no races here.
366 // Measurements show that it can be 10-20% of all memory accesses.
367 StatInc(thr, StatMopRangeRodata);
368 return;
369 }
370
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000371 FastState fast_state = thr->fast_state;
372 if (fast_state.GetIgnoreBit())
373 return;
374
375 fast_state.IncrementEpoch();
376 thr->fast_state = fast_state;
Dmitry Vyukov385542a2012-11-28 10:35:31 +0000377 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000378
379 bool unaligned = (addr % kShadowCell) != 0;
380
381 // Handle unaligned beginning, if any.
382 for (; addr % kShadowCell && size; addr++, size--) {
383 int const kAccessSizeLog = 0;
384 Shadow cur(fast_state);
385 cur.SetWrite(is_write);
386 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000387 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000388 shadow_mem, cur);
389 }
390 if (unaligned)
391 shadow_mem += kShadowCnt;
392 // Handle middle part, if any.
393 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
394 int const kAccessSizeLog = 3;
395 Shadow cur(fast_state);
396 cur.SetWrite(is_write);
397 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000398 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000399 shadow_mem, cur);
400 shadow_mem += kShadowCnt;
401 }
402 // Handle ending, if any.
403 for (; size; addr++, size--) {
404 int const kAccessSizeLog = 0;
405 Shadow cur(fast_state);
406 cur.SetWrite(is_write);
407 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000408 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000409 shadow_mem, cur);
410 }
411}
Dmitry Vyukoveaa01902013-02-13 13:05:36 +0000412
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000413} // namespace __tsan