blob: 0cb2fcbde007b5533660c986f9401accfd65037c [file] [log] [blame]
Alexey Samsonov3b2f9f42012-06-04 13:55:19 +00001//===-- tsan_rtl_thread.cc ------------------------------------------------===//
Kostya Serebryany4ad375f2012-05-10 13:48:04 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of ThreadSanitizer (TSan), a race detector.
11//
12//===----------------------------------------------------------------------===//
13
Alexey Samsonov8bd90982012-06-07 09:50:16 +000014#include "sanitizer_common/sanitizer_placement_new.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000015#include "tsan_rtl.h"
16#include "tsan_mman.h"
Kostya Serebryany4ad375f2012-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 Samsonov9aecdfe2013-03-15 13:48:44 +000023// ThreadContext implementation.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000024
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000025ThreadContext::ThreadContext(int tid)
26 : ThreadContextBase(tid)
27 , thr()
28 , sync()
29 , epoch0()
Dmitry Vyukov79915de2013-03-20 10:31:53 +000030 , epoch1() {
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000031}
32
Kostya Serebryany83ed8892014-12-09 01:31:14 +000033#ifndef SANITIZER_GO
Dmitry Vyukov49e462f2013-03-18 10:10:15 +000034ThreadContext::~ThreadContext() {
35}
36#endif
37
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000038void ThreadContext::OnDead() {
Dmitry Vyukov70db9d42014-08-05 18:45:02 +000039 CHECK_EQ(sync.size(), 0);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000040}
41
42void ThreadContext::OnJoined(void *arg) {
43 ThreadState *caller_thr = static_cast<ThreadState *>(arg);
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +000044 AcquireImpl(caller_thr, 0, &sync);
Dmitry Vyukov70db9d42014-08-05 18:45:02 +000045 sync.Reset(&caller_thr->clock_cache);
Alexey Samsonov9aecdfe2013-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 Vyukov50160032013-03-18 08:52:46 +000055 if (tid == 0)
56 return;
57 OnCreatedArgs *args = static_cast<OnCreatedArgs *>(arg);
58 args->thr->fast_state.IncrementEpoch();
59 // Can't increment epoch w/o writing to the trace as well.
60 TraceAddEvent(args->thr, args->thr->fast_state, EventTypeMop, 0);
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +000061 ReleaseImpl(args->thr, 0, &sync);
Dmitry Vyukov7cd20252013-03-18 09:02:27 +000062 creation_stack_id = CurrentStackId(args->thr, args->pc);
Dmitry Vyukov50160032013-03-18 08:52:46 +000063 if (reuse_count == 0)
64 StatInc(args->thr, StatThreadMaxTid);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000065}
66
Dmitry Vyukov4ecfa692013-03-19 12:25:48 +000067void ThreadContext::OnReset() {
Dmitry Vyukov70db9d42014-08-05 18:45:02 +000068 CHECK_EQ(sync.size(), 0);
Dmitry Vyukov79915de2013-03-20 10:31:53 +000069 FlushUnneededShadowMemory(GetThreadTrace(tid), TraceSize() * sizeof(Event));
70 //!!! FlushUnneededShadowMemory(GetThreadTraceHeader(tid), sizeof(Trace));
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000071}
72
Dmitry Vyukov70db9d42014-08-05 18:45:02 +000073void ThreadContext::OnDetached(void *arg) {
74 ThreadState *thr1 = static_cast<ThreadState*>(arg);
75 sync.Reset(&thr1->clock_cache);
76}
77
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000078struct OnStartedArgs {
79 ThreadState *thr;
80 uptr stk_addr;
81 uptr stk_size;
82 uptr tls_addr;
83 uptr tls_size;
84};
85
86void ThreadContext::OnStarted(void *arg) {
87 OnStartedArgs *args = static_cast<OnStartedArgs*>(arg);
Dmitry Vyukov50160032013-03-18 08:52:46 +000088 thr = args->thr;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000089 // RoundUp so that one trace part does not contain events
90 // from different threads.
91 epoch0 = RoundUp(epoch1 + 1, kTracePartSize);
92 epoch1 = (u64)-1;
Dmitry Vyukovb5eb8f02014-04-11 15:38:03 +000093 new(thr) ThreadState(ctx, tid, unique_id, epoch0, reuse_count,
94 args->stk_addr, args->stk_size, args->tls_addr, args->tls_size);
Kostya Serebryany83ed8892014-12-09 01:31:14 +000095#ifndef SANITIZER_GO
Dmitry Vyukov464ebbd2013-10-16 15:35:12 +000096 thr->shadow_stack = &ThreadTrace(thr->tid)->shadow_stack[0];
97 thr->shadow_stack_pos = thr->shadow_stack;
98 thr->shadow_stack_end = thr->shadow_stack + kShadowStackSize;
99#else
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000100 // Setup dynamic shadow stack.
101 const int kInitStackSize = 8;
Dmitry Vyukov464ebbd2013-10-16 15:35:12 +0000102 thr->shadow_stack = (uptr*)internal_alloc(MBlockShadowStack,
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000103 kInitStackSize * sizeof(uptr));
Dmitry Vyukov464ebbd2013-10-16 15:35:12 +0000104 thr->shadow_stack_pos = thr->shadow_stack;
105 thr->shadow_stack_end = thr->shadow_stack + kInitStackSize;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000106#endif
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000107#ifndef SANITIZER_GO
Dmitry Vyukov464ebbd2013-10-16 15:35:12 +0000108 AllocatorThreadStart(thr);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000109#endif
Alexey Samsonov5c825962014-09-10 23:08:06 +0000110 if (common_flags()->detect_deadlocks) {
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000111 thr->dd_pt = ctx->dd->CreatePhysicalThread();
112 thr->dd_lt = ctx->dd->CreateLogicalThread(unique_id);
113 }
Dmitry Vyukovbda65502014-12-25 10:32:25 +0000114 thr->fast_state.SetHistorySize(flags()->history_size);
115 // Commit switch to the new part of the trace.
116 // TraceAddEvent will reset stack0/mset0 in the new part for us.
117 TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
118
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000119 thr->fast_synch_epoch = epoch0;
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000120 AcquireImpl(thr, 0, &sync);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000121 StatInc(thr, StatSyncAcquire);
Dmitry Vyukov70db9d42014-08-05 18:45:02 +0000122 sync.Reset(&thr->clock_cache);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000123 DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx "
124 "tls_addr=%zx tls_size=%zx\n",
Alexey Samsonovb5d10f62013-03-18 09:45:22 +0000125 tid, (uptr)epoch0, args->stk_addr, args->stk_size,
126 args->tls_addr, args->tls_size);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000127}
128
129void ThreadContext::OnFinished() {
130 if (!detached) {
131 thr->fast_state.IncrementEpoch();
132 // Can't increment epoch w/o writing to the trace as well.
133 TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000134 ReleaseImpl(thr, 0, &sync);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000135 }
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000136 epoch1 = thr->fast_state.epoch();
137
Alexey Samsonov5c825962014-09-10 23:08:06 +0000138 if (common_flags()->detect_deadlocks) {
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000139 ctx->dd->DestroyPhysicalThread(thr->dd_pt);
140 ctx->dd->DestroyLogicalThread(thr->dd_lt);
141 }
Dmitry Vyukov70db9d42014-08-05 18:45:02 +0000142 ctx->clock_alloc.FlushCache(&thr->clock_cache);
Dmitry Vyukovff194da2014-06-06 15:52:10 +0000143 ctx->metamap.OnThreadIdle(thr);
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000144#ifndef SANITIZER_GO
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000145 AllocatorThreadFinish(thr);
146#endif
147 thr->~ThreadState();
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000148#ifdef TSAN_COLLECT_STATS
Dmitry Vyukov6cfab722014-02-28 10:48:13 +0000149 StatAggregate(ctx->stat, thr->stat);
Dmitry Vyukov41f4eba2015-02-13 15:25:47 +0000150#endif
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000151 thr = 0;
152}
153
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000154#ifndef SANITIZER_GO
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000155struct ThreadLeak {
156 ThreadContext *tctx;
157 int count;
158};
159
160static void MaybeReportThreadLeak(ThreadContextBase *tctx_base, void *arg) {
161 Vector<ThreadLeak> &leaks = *(Vector<ThreadLeak>*)arg;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000162 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000163 if (tctx->detached || tctx->status != ThreadStatusFinished)
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000164 return;
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000165 for (uptr i = 0; i < leaks.Size(); i++) {
166 if (leaks[i].tctx->creation_stack_id == tctx->creation_stack_id) {
167 leaks[i].count++;
168 return;
169 }
170 }
171 ThreadLeak leak = {tctx, 1};
172 leaks.PushBack(leak);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000173}
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000174#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000175
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000176#ifndef SANITIZER_GO
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000177static void ReportIgnoresEnabled(ThreadContext *tctx, IgnoreSet *set) {
178 if (tctx->tid == 0) {
179 Printf("ThreadSanitizer: main thread finished with ignores enabled\n");
180 } else {
181 Printf("ThreadSanitizer: thread T%d %s finished with ignores enabled,"
182 " created at:\n", tctx->tid, tctx->name);
183 PrintStack(SymbolizeStackId(tctx->creation_stack_id));
Dmitry Vyukov536bff32013-05-21 08:12:35 +0000184 }
Dmitry Vyukovc0386862013-11-27 18:23:52 +0000185 Printf(" One of the following ignores was not ended"
186 " (in order of probability)\n");
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000187 for (uptr i = 0; i < set->Size(); i++) {
188 Printf(" Ignore was enabled at:\n");
189 PrintStack(SymbolizeStackId(set->At(i)));
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000190 }
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000191 Die();
Dmitry Vyukov536bff32013-05-21 08:12:35 +0000192}
193
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000194static void ThreadCheckIgnore(ThreadState *thr) {
Dmitry Vyukovc9e12aa2014-03-20 10:36:20 +0000195 if (ctx->after_multithreaded_fork)
Dmitry Vyukov16e7a752014-01-24 12:33:35 +0000196 return;
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000197 if (thr->ignore_reads_and_writes)
198 ReportIgnoresEnabled(thr->tctx, &thr->mop_ignore_set);
199 if (thr->ignore_sync)
200 ReportIgnoresEnabled(thr->tctx, &thr->sync_ignore_set);
201}
202#else
203static void ThreadCheckIgnore(ThreadState *thr) {}
204#endif
205
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000206void ThreadFinalize(ThreadState *thr) {
Dmitry Vyukov536bff32013-05-21 08:12:35 +0000207 ThreadCheckIgnore(thr);
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000208#ifndef SANITIZER_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000209 if (!flags()->report_thread_leaks)
210 return;
Dmitry Vyukovc9e12aa2014-03-20 10:36:20 +0000211 ThreadRegistryLock l(ctx->thread_registry);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000212 Vector<ThreadLeak> leaks(MBlockScopedBuf);
Dmitry Vyukovc9e12aa2014-03-20 10:36:20 +0000213 ctx->thread_registry->RunCallbackForEachThreadLocked(
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000214 MaybeReportThreadLeak, &leaks);
215 for (uptr i = 0; i < leaks.Size(); i++) {
216 ScopedReport rep(ReportTypeThreadLeak);
Dmitry Vyukova43e98c2014-05-28 18:03:32 +0000217 rep.AddThread(leaks[i].tctx, true);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000218 rep.SetCount(leaks[i].count);
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000219 OutputReport(thr, rep);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000220 }
221#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000222}
223
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000224int ThreadCount(ThreadState *thr) {
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000225 uptr result;
226 ctx->thread_registry->GetNumberOfThreads(0, 0, &result);
227 return (int)result;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000228}
229
230int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000231 StatInc(thr, StatThreadCreate);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000232 OnCreatedArgs args = { thr, pc };
233 int tid = ctx->thread_registry->CreateThread(uid, detached, thr->tid, &args);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000234 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000235 StatSet(thr, StatThreadMaxAlive, ctx->thread_registry->GetMaxAliveThreads());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000236 return tid;
237}
238
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000239void ThreadStart(ThreadState *thr, int tid, uptr os_id) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000240 uptr stk_addr = 0;
241 uptr stk_size = 0;
242 uptr tls_addr = 0;
243 uptr tls_size = 0;
Dmitry Vyukov7339eb12012-05-25 11:15:04 +0000244 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000245
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000246 if (tid) {
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000247 if (stk_addr && stk_size)
Dmitry Vyukovce26a0a2013-03-18 16:56:48 +0000248 MemoryRangeImitateWrite(thr, /*pc=*/ 1, stk_addr, stk_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000249
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000250 if (tls_addr && tls_size) {
251 // Check that the thr object is in tls;
252 const uptr thr_beg = (uptr)thr;
253 const uptr thr_end = (uptr)thr + sizeof(*thr);
254 CHECK_GE(thr_beg, tls_addr);
255 CHECK_LE(thr_beg, tls_addr + tls_size);
256 CHECK_GE(thr_end, tls_addr);
257 CHECK_LE(thr_end, tls_addr + tls_size);
258 // Since the thr object is huge, skip it.
Dmitry Vyukovce26a0a2013-03-18 16:56:48 +0000259 MemoryRangeImitateWrite(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
260 MemoryRangeImitateWrite(thr, /*pc=*/ 2,
261 thr_end, tls_addr + tls_size - thr_end);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000262 }
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000263 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000264
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000265 ThreadRegistry *tr = ctx->thread_registry;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000266 OnStartedArgs args = { thr, stk_addr, stk_size, tls_addr, tls_size };
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000267 tr->StartThread(tid, os_id, &args);
268
269 tr->Lock();
270 thr->tctx = (ThreadContext*)tr->GetThreadLocked(tid);
271 tr->Unlock();
Dmitry Vyukov16e7a752014-01-24 12:33:35 +0000272
Kostya Serebryany83ed8892014-12-09 01:31:14 +0000273#ifndef SANITIZER_GO
Dmitry Vyukov16e7a752014-01-24 12:33:35 +0000274 if (ctx->after_multithreaded_fork) {
275 thr->ignore_interceptors++;
276 ThreadIgnoreBegin(thr, 0);
277 ThreadIgnoreSyncBegin(thr, 0);
278 }
279#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000280}
281
282void ThreadFinish(ThreadState *thr) {
Dmitry Vyukov536bff32013-05-21 08:12:35 +0000283 ThreadCheckIgnore(thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000284 StatInc(thr, StatThreadFinish);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000285 if (thr->stk_addr && thr->stk_size)
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000286 DontNeedShadowFor(thr->stk_addr, thr->stk_size);
287 if (thr->tls_addr && thr->tls_size)
288 DontNeedShadowFor(thr->tls_addr, thr->tls_size);
Dmitry Vyukovf8cfdd92014-09-03 12:25:22 +0000289 thr->is_dead = true;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000290 ctx->thread_registry->FinishThread(thr->tid);
291}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000292
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000293static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) {
294 uptr uid = (uptr)arg;
295 if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) {
296 tctx->user_id = 0;
297 return true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000298 }
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000299 return false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000300}
301
302int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000303 int res = ctx->thread_registry->FindThread(FindThreadByUid, (void*)uid);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000304 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000305 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000306}
307
308void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000309 CHECK_GT(tid, 0);
310 CHECK_LT(tid, kMaxTid);
311 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000312 ctx->thread_registry->JoinThread(tid, thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000313}
314
315void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000316 CHECK_GT(tid, 0);
317 CHECK_LT(tid, kMaxTid);
Dmitry Vyukov70db9d42014-08-05 18:45:02 +0000318 ctx->thread_registry->DetachThread(tid, thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000319}
320
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000321void ThreadSetName(ThreadState *thr, const char *name) {
Dmitry Vyukovc9e12aa2014-03-20 10:36:20 +0000322 ctx->thread_registry->SetThreadName(thr->tid, name);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000323}
324
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000325void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
326 uptr size, bool is_write) {
327 if (size == 0)
328 return;
329
330 u64 *shadow_mem = (u64*)MemToShadow(addr);
331 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
332 thr->tid, (void*)pc, (void*)addr,
333 (int)size, is_write);
334
Alexey Samsonovdf3aeb82015-01-03 04:29:12 +0000335#if SANITIZER_DEBUG
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000336 if (!IsAppMem(addr)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000337 Printf("Access to non app mem %zx\n", addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000338 DCHECK(IsAppMem(addr));
339 }
340 if (!IsAppMem(addr + size - 1)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000341 Printf("Access to non app mem %zx\n", addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000342 DCHECK(IsAppMem(addr + size - 1));
343 }
344 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000345 Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000346 DCHECK(IsShadowMem((uptr)shadow_mem));
347 }
348 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000349 Printf("Bad shadow addr %p (%zx)\n",
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000350 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000351 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
352 }
353#endif
354
355 StatInc(thr, StatMopRange);
356
Dmitry Vyukovb62c1582013-03-20 13:21:50 +0000357 if (*shadow_mem == kShadowRodata) {
358 // Access to .rodata section, no races here.
359 // Measurements show that it can be 10-20% of all memory accesses.
360 StatInc(thr, StatMopRangeRodata);
361 return;
362 }
363
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000364 FastState fast_state = thr->fast_state;
365 if (fast_state.GetIgnoreBit())
366 return;
367
368 fast_state.IncrementEpoch();
369 thr->fast_state = fast_state;
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000370 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000371
372 bool unaligned = (addr % kShadowCell) != 0;
373
374 // Handle unaligned beginning, if any.
375 for (; addr % kShadowCell && size; addr++, size--) {
376 int const kAccessSizeLog = 0;
377 Shadow cur(fast_state);
378 cur.SetWrite(is_write);
379 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000380 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000381 shadow_mem, cur);
382 }
383 if (unaligned)
384 shadow_mem += kShadowCnt;
385 // Handle middle part, if any.
386 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
387 int const kAccessSizeLog = 3;
388 Shadow cur(fast_state);
389 cur.SetWrite(is_write);
390 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000391 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000392 shadow_mem, cur);
393 shadow_mem += kShadowCnt;
394 }
395 // Handle ending, if any.
396 for (; size; addr++, size--) {
397 int const kAccessSizeLog = 0;
398 Shadow cur(fast_state);
399 cur.SetWrite(is_write);
400 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000401 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000402 shadow_mem, cur);
403 }
404}
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000405
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000406} // namespace __tsan