blob: c2fcd7138482333dde37d40786e7b2572c8036b8 [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
Dmitry Vyukov49e462f2013-03-18 10:10:15 +000033#ifndef TSAN_GO
34ThreadContext::~ThreadContext() {
35}
36#endif
37
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000038void ThreadContext::OnDead() {
39 sync.Reset();
40}
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 Vyukov509dab32013-03-19 10:22:33 +000045 sync.Reset();
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#ifdef TSAN_GO
Dmitry Vyukov50160032013-03-18 08:52:46 +000063 creation_stack.ObtainCurrent(args->thr, args->pc);
Dmitry Vyukov7cd20252013-03-18 09:02:27 +000064#else
65 creation_stack_id = CurrentStackId(args->thr, args->pc);
66#endif
Dmitry Vyukov50160032013-03-18 08:52:46 +000067 if (reuse_count == 0)
68 StatInc(args->thr, StatThreadMaxTid);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000069}
70
Dmitry Vyukov4ecfa692013-03-19 12:25:48 +000071void ThreadContext::OnReset() {
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000072 sync.Reset();
Dmitry Vyukov79915de2013-03-20 10:31:53 +000073 FlushUnneededShadowMemory(GetThreadTrace(tid), TraceSize() * sizeof(Event));
74 //!!! FlushUnneededShadowMemory(GetThreadTraceHeader(tid), sizeof(Trace));
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000075}
76
77struct OnStartedArgs {
78 ThreadState *thr;
79 uptr stk_addr;
80 uptr stk_size;
81 uptr tls_addr;
82 uptr tls_size;
83};
84
85void ThreadContext::OnStarted(void *arg) {
86 OnStartedArgs *args = static_cast<OnStartedArgs*>(arg);
Dmitry Vyukov50160032013-03-18 08:52:46 +000087 thr = args->thr;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000088 // RoundUp so that one trace part does not contain events
89 // from different threads.
90 epoch0 = RoundUp(epoch1 + 1, kTracePartSize);
91 epoch1 = (u64)-1;
Dmitry Vyukov50160032013-03-18 08:52:46 +000092 new(thr) ThreadState(CTX(), tid, unique_id,
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000093 epoch0, args->stk_addr, args->stk_size, args->tls_addr, args->tls_size);
Dmitry Vyukov464ebbd2013-10-16 15:35:12 +000094#ifndef TSAN_GO
95 thr->shadow_stack = &ThreadTrace(thr->tid)->shadow_stack[0];
96 thr->shadow_stack_pos = thr->shadow_stack;
97 thr->shadow_stack_end = thr->shadow_stack + kShadowStackSize;
98#else
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +000099 // Setup dynamic shadow stack.
100 const int kInitStackSize = 8;
Dmitry Vyukov464ebbd2013-10-16 15:35:12 +0000101 thr->shadow_stack = (uptr*)internal_alloc(MBlockShadowStack,
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000102 kInitStackSize * sizeof(uptr));
Dmitry Vyukov464ebbd2013-10-16 15:35:12 +0000103 thr->shadow_stack_pos = thr->shadow_stack;
104 thr->shadow_stack_end = thr->shadow_stack + kInitStackSize;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000105#endif
106#ifndef TSAN_GO
Dmitry Vyukov464ebbd2013-10-16 15:35:12 +0000107 AllocatorThreadStart(thr);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000108#endif
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000109 thr->fast_synch_epoch = epoch0;
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000110 AcquireImpl(thr, 0, &sync);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000111 thr->fast_state.SetHistorySize(flags()->history_size);
112 const uptr trace = (epoch0 / kTracePartSize) % TraceParts();
Dmitry Vyukov79915de2013-03-20 10:31:53 +0000113 Trace *thr_trace = ThreadTrace(thr->tid);
114 thr_trace->headers[trace].epoch0 = epoch0;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000115 StatInc(thr, StatSyncAcquire);
Dmitry Vyukov79915de2013-03-20 10:31:53 +0000116 sync.Reset();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000117 DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx "
118 "tls_addr=%zx tls_size=%zx\n",
Alexey Samsonovb5d10f62013-03-18 09:45:22 +0000119 tid, (uptr)epoch0, args->stk_addr, args->stk_size,
120 args->tls_addr, args->tls_size);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000121 thr->is_alive = true;
122}
123
124void ThreadContext::OnFinished() {
125 if (!detached) {
126 thr->fast_state.IncrementEpoch();
127 // Can't increment epoch w/o writing to the trace as well.
128 TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000129 ReleaseImpl(thr, 0, &sync);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000130 }
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000131 epoch1 = thr->fast_state.epoch();
132
133#ifndef TSAN_GO
134 AllocatorThreadFinish(thr);
135#endif
136 thr->~ThreadState();
137 StatAggregate(CTX()->stat, thr->stat);
138 thr = 0;
139}
140
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000141#ifndef TSAN_GO
142struct ThreadLeak {
143 ThreadContext *tctx;
144 int count;
145};
146
147static void MaybeReportThreadLeak(ThreadContextBase *tctx_base, void *arg) {
148 Vector<ThreadLeak> &leaks = *(Vector<ThreadLeak>*)arg;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000149 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000150 if (tctx->detached || tctx->status != ThreadStatusFinished)
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000151 return;
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000152 for (uptr i = 0; i < leaks.Size(); i++) {
153 if (leaks[i].tctx->creation_stack_id == tctx->creation_stack_id) {
154 leaks[i].count++;
155 return;
156 }
157 }
158 ThreadLeak leak = {tctx, 1};
159 leaks.PushBack(leak);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000160}
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000161#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000162
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000163#ifndef TSAN_GO
164static void ReportIgnoresEnabled(ThreadContext *tctx, IgnoreSet *set) {
165 if (tctx->tid == 0) {
166 Printf("ThreadSanitizer: main thread finished with ignores enabled\n");
167 } else {
168 Printf("ThreadSanitizer: thread T%d %s finished with ignores enabled,"
169 " created at:\n", tctx->tid, tctx->name);
170 PrintStack(SymbolizeStackId(tctx->creation_stack_id));
Dmitry Vyukov536bff32013-05-21 08:12:35 +0000171 }
Dmitry Vyukovc0386862013-11-27 18:23:52 +0000172 Printf(" One of the following ignores was not ended"
173 " (in order of probability)\n");
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000174 for (uptr i = 0; i < set->Size(); i++) {
175 Printf(" Ignore was enabled at:\n");
176 PrintStack(SymbolizeStackId(set->At(i)));
Dmitry Vyukovfbb194f2013-10-10 15:58:12 +0000177 }
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000178 Die();
Dmitry Vyukov536bff32013-05-21 08:12:35 +0000179}
180
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000181static void ThreadCheckIgnore(ThreadState *thr) {
182 if (thr->ignore_reads_and_writes)
183 ReportIgnoresEnabled(thr->tctx, &thr->mop_ignore_set);
184 if (thr->ignore_sync)
185 ReportIgnoresEnabled(thr->tctx, &thr->sync_ignore_set);
186}
187#else
188static void ThreadCheckIgnore(ThreadState *thr) {}
189#endif
190
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000191void ThreadFinalize(ThreadState *thr) {
192 CHECK_GT(thr->in_rtl, 0);
Dmitry Vyukov536bff32013-05-21 08:12:35 +0000193 ThreadCheckIgnore(thr);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000194#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000195 if (!flags()->report_thread_leaks)
196 return;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000197 ThreadRegistryLock l(CTX()->thread_registry);
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000198 Vector<ThreadLeak> leaks(MBlockScopedBuf);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000199 CTX()->thread_registry->RunCallbackForEachThreadLocked(
Dmitry Vyukovebf63d02013-03-21 16:55:17 +0000200 MaybeReportThreadLeak, &leaks);
201 for (uptr i = 0; i < leaks.Size(); i++) {
202 ScopedReport rep(ReportTypeThreadLeak);
203 rep.AddThread(leaks[i].tctx);
204 rep.SetCount(leaks[i].count);
205 OutputReport(CTX(), rep);
206 }
207#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000208}
209
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000210int ThreadCount(ThreadState *thr) {
211 CHECK_GT(thr->in_rtl, 0);
212 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000213 uptr result;
214 ctx->thread_registry->GetNumberOfThreads(0, 0, &result);
215 return (int)result;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000216}
217
218int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
219 CHECK_GT(thr->in_rtl, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000220 StatInc(thr, StatThreadCreate);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000221 Context *ctx = CTX();
222 OnCreatedArgs args = { thr, pc };
223 int tid = ctx->thread_registry->CreateThread(uid, detached, thr->tid, &args);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000224 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid);
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000225 StatSet(thr, StatThreadMaxAlive, ctx->thread_registry->GetMaxAliveThreads());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000226 return tid;
227}
228
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000229void ThreadStart(ThreadState *thr, int tid, uptr os_id) {
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000230 Context *ctx = CTX();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000231 CHECK_GT(thr->in_rtl, 0);
232 uptr stk_addr = 0;
233 uptr stk_size = 0;
234 uptr tls_addr = 0;
235 uptr tls_size = 0;
Dmitry Vyukov7339eb12012-05-25 11:15:04 +0000236 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000237
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000238 if (tid) {
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000239 if (stk_addr && stk_size)
Dmitry Vyukovce26a0a2013-03-18 16:56:48 +0000240 MemoryRangeImitateWrite(thr, /*pc=*/ 1, stk_addr, stk_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000241
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000242 if (tls_addr && tls_size) {
243 // Check that the thr object is in tls;
244 const uptr thr_beg = (uptr)thr;
245 const uptr thr_end = (uptr)thr + sizeof(*thr);
246 CHECK_GE(thr_beg, tls_addr);
247 CHECK_LE(thr_beg, tls_addr + tls_size);
248 CHECK_GE(thr_end, tls_addr);
249 CHECK_LE(thr_end, tls_addr + tls_size);
250 // Since the thr object is huge, skip it.
Dmitry Vyukovce26a0a2013-03-18 16:56:48 +0000251 MemoryRangeImitateWrite(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
252 MemoryRangeImitateWrite(thr, /*pc=*/ 2,
253 thr_end, tls_addr + tls_size - thr_end);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000254 }
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000255 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000256
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000257 ThreadRegistry *tr = ctx->thread_registry;
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000258 OnStartedArgs args = { thr, stk_addr, stk_size, tls_addr, tls_size };
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000259 tr->StartThread(tid, os_id, &args);
260
261 tr->Lock();
262 thr->tctx = (ThreadContext*)tr->GetThreadLocked(tid);
263 tr->Unlock();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000264}
265
266void ThreadFinish(ThreadState *thr) {
267 CHECK_GT(thr->in_rtl, 0);
Dmitry Vyukov536bff32013-05-21 08:12:35 +0000268 ThreadCheckIgnore(thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000269 StatInc(thr, StatThreadFinish);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000270 if (thr->stk_addr && thr->stk_size)
Dmitry Vyukov2e7f29f2013-03-18 15:49:07 +0000271 DontNeedShadowFor(thr->stk_addr, thr->stk_size);
272 if (thr->tls_addr && thr->tls_size)
273 DontNeedShadowFor(thr->tls_addr, thr->tls_size);
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000274 thr->is_alive = false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000275 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000276 ctx->thread_registry->FinishThread(thr->tid);
277}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000278
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000279static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) {
280 uptr uid = (uptr)arg;
281 if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) {
282 tctx->user_id = 0;
283 return true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000284 }
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000285 return false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000286}
287
288int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
289 CHECK_GT(thr->in_rtl, 0);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000290 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000291 int res = ctx->thread_registry->FindThread(FindThreadByUid, (void*)uid);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000292 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000293 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000294}
295
296void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
297 CHECK_GT(thr->in_rtl, 0);
298 CHECK_GT(tid, 0);
299 CHECK_LT(tid, kMaxTid);
300 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
301 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000302 ctx->thread_registry->JoinThread(tid, thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000303}
304
305void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
306 CHECK_GT(thr->in_rtl, 0);
307 CHECK_GT(tid, 0);
308 CHECK_LT(tid, kMaxTid);
309 Context *ctx = CTX();
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000310 ctx->thread_registry->DetachThread(tid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000311}
312
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000313void ThreadSetName(ThreadState *thr, const char *name) {
Alexey Samsonov9aecdfe2013-03-15 13:48:44 +0000314 CHECK_GT(thr->in_rtl, 0);
315 CTX()->thread_registry->SetThreadName(thr->tid, name);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000316}
317
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000318void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
319 uptr size, bool is_write) {
320 if (size == 0)
321 return;
322
323 u64 *shadow_mem = (u64*)MemToShadow(addr);
324 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
325 thr->tid, (void*)pc, (void*)addr,
326 (int)size, is_write);
327
328#if TSAN_DEBUG
329 if (!IsAppMem(addr)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000330 Printf("Access to non app mem %zx\n", addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000331 DCHECK(IsAppMem(addr));
332 }
333 if (!IsAppMem(addr + size - 1)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000334 Printf("Access to non app mem %zx\n", addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000335 DCHECK(IsAppMem(addr + size - 1));
336 }
337 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000338 Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000339 DCHECK(IsShadowMem((uptr)shadow_mem));
340 }
341 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000342 Printf("Bad shadow addr %p (%zx)\n",
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000343 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000344 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
345 }
346#endif
347
348 StatInc(thr, StatMopRange);
349
Dmitry Vyukovb62c1582013-03-20 13:21:50 +0000350 if (*shadow_mem == kShadowRodata) {
351 // Access to .rodata section, no races here.
352 // Measurements show that it can be 10-20% of all memory accesses.
353 StatInc(thr, StatMopRangeRodata);
354 return;
355 }
356
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000357 FastState fast_state = thr->fast_state;
358 if (fast_state.GetIgnoreBit())
359 return;
360
361 fast_state.IncrementEpoch();
362 thr->fast_state = fast_state;
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000363 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000364
365 bool unaligned = (addr % kShadowCell) != 0;
366
367 // Handle unaligned beginning, if any.
368 for (; addr % kShadowCell && size; addr++, size--) {
369 int const kAccessSizeLog = 0;
370 Shadow cur(fast_state);
371 cur.SetWrite(is_write);
372 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000373 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000374 shadow_mem, cur);
375 }
376 if (unaligned)
377 shadow_mem += kShadowCnt;
378 // Handle middle part, if any.
379 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
380 int const kAccessSizeLog = 3;
381 Shadow cur(fast_state);
382 cur.SetWrite(is_write);
383 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000384 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000385 shadow_mem, cur);
386 shadow_mem += kShadowCnt;
387 }
388 // Handle ending, if any.
389 for (; size; addr++, size--) {
390 int const kAccessSizeLog = 0;
391 Shadow cur(fast_state);
392 cur.SetWrite(is_write);
393 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000394 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000395 shadow_mem, cur);
396 }
397}
Dmitry Vyukov3c2489e2013-02-13 13:05:36 +0000398
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000399} // namespace __tsan