blob: 3af42e48fabeb79408bdeac5f9938bde7ab77604 [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
Dmitry Vyukovdc36d692012-07-16 16:03:16 +000023#ifndef TSAN_GO
Dmitry Vyukovf6985e32012-05-22 14:34:43 +000024const int kThreadQuarantineSize = 16;
Dmitry Vyukovdc36d692012-07-16 16:03:16 +000025#else
26const int kThreadQuarantineSize = 64;
27#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000028
29static void MaybeReportThreadLeak(ThreadContext *tctx) {
30 if (tctx->detached)
31 return;
32 if (tctx->status != ThreadStatusCreated
33 && tctx->status != ThreadStatusRunning
34 && tctx->status != ThreadStatusFinished)
35 return;
36 ScopedReport rep(ReportTypeThreadLeak);
37 rep.AddThread(tctx);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +000038 OutputReport(CTX(), rep);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000039}
40
41void ThreadFinalize(ThreadState *thr) {
42 CHECK_GT(thr->in_rtl, 0);
43 if (!flags()->report_thread_leaks)
44 return;
45 Context *ctx = CTX();
46 Lock l(&ctx->thread_mtx);
Kostya Serebryany07c48052012-05-11 14:42:24 +000047 for (unsigned i = 0; i < kMaxTid; i++) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000048 ThreadContext *tctx = ctx->threads[i];
49 if (tctx == 0)
50 continue;
51 MaybeReportThreadLeak(tctx);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000052 }
53}
54
Dmitry Vyukov67dc5702012-11-07 16:41:57 +000055int ThreadCount(ThreadState *thr) {
56 CHECK_GT(thr->in_rtl, 0);
57 Context *ctx = CTX();
58 Lock l(&ctx->thread_mtx);
59 int cnt = 0;
60 for (unsigned i = 0; i < kMaxTid; i++) {
61 ThreadContext *tctx = ctx->threads[i];
62 if (tctx == 0)
63 continue;
64 if (tctx->status != ThreadStatusCreated
65 && tctx->status != ThreadStatusRunning)
66 continue;
67 cnt++;
68 }
69 return cnt;
70}
71
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000072static void ThreadDead(ThreadState *thr, ThreadContext *tctx) {
73 Context *ctx = CTX();
74 CHECK_GT(thr->in_rtl, 0);
75 CHECK(tctx->status == ThreadStatusRunning
76 || tctx->status == ThreadStatusFinished);
Alexey Samsonov51ae9832012-06-06 13:11:29 +000077 DPrintf("#%d: ThreadDead uid=%zu\n", thr->tid, tctx->user_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000078 tctx->status = ThreadStatusDead;
79 tctx->user_id = 0;
80 tctx->sync.Reset();
81
82 // Put to dead list.
83 tctx->dead_next = 0;
84 if (ctx->dead_list_size == 0)
85 ctx->dead_list_head = tctx;
86 else
87 ctx->dead_list_tail->dead_next = tctx;
88 ctx->dead_list_tail = tctx;
89 ctx->dead_list_size++;
90}
91
92int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
93 CHECK_GT(thr->in_rtl, 0);
94 Context *ctx = CTX();
95 Lock l(&ctx->thread_mtx);
96 StatInc(thr, StatThreadCreate);
97 int tid = -1;
98 ThreadContext *tctx = 0;
99 if (ctx->dead_list_size > kThreadQuarantineSize
100 || ctx->thread_seq >= kMaxTid) {
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000101 // Reusing old thread descriptor and tid.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000102 if (ctx->dead_list_size == 0) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000103 Printf("ThreadSanitizer: %d thread limit exceeded. Dying.\n",
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000104 kMaxTid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000105 Die();
106 }
107 StatInc(thr, StatThreadReuse);
108 tctx = ctx->dead_list_head;
109 ctx->dead_list_head = tctx->dead_next;
110 ctx->dead_list_size--;
111 if (ctx->dead_list_size == 0) {
112 CHECK_EQ(tctx->dead_next, 0);
113 ctx->dead_list_head = 0;
114 }
115 CHECK_EQ(tctx->status, ThreadStatusDead);
116 tctx->status = ThreadStatusInvalid;
117 tctx->reuse_count++;
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000118 tctx->sync.Reset();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000119 tid = tctx->tid;
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000120 DestroyAndFree(tctx->dead_info);
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000121 if (tctx->name) {
122 internal_free(tctx->name);
123 tctx->name = 0;
124 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000125 } else {
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000126 // Allocating new thread descriptor and tid.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000127 StatInc(thr, StatThreadMaxTid);
128 tid = ctx->thread_seq++;
129 void *mem = internal_alloc(MBlockThreadContex, sizeof(ThreadContext));
130 tctx = new(mem) ThreadContext(tid);
131 ctx->threads[tid] = tctx;
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000132 MapThreadTrace(GetThreadTrace(tid), TraceSize() * sizeof(Event));
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000133 }
134 CHECK_NE(tctx, 0);
135 CHECK_GE(tid, 0);
136 CHECK_LT(tid, kMaxTid);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000137 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000138 CHECK_EQ(tctx->status, ThreadStatusInvalid);
139 ctx->alive_threads++;
140 if (ctx->max_alive_threads < ctx->alive_threads) {
141 ctx->max_alive_threads++;
142 CHECK_EQ(ctx->max_alive_threads, ctx->alive_threads);
143 StatInc(thr, StatThreadMaxAlive);
144 }
145 tctx->status = ThreadStatusCreated;
146 tctx->thr = 0;
147 tctx->user_id = uid;
148 tctx->unique_id = ctx->unique_thread_seq++;
149 tctx->detached = detached;
150 if (tid) {
151 thr->fast_state.IncrementEpoch();
152 // Can't increment epoch w/o writing to the trace as well.
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000153 TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000154 thr->clock.set(thr->tid, thr->fast_state.epoch());
155 thr->fast_synch_epoch = thr->fast_state.epoch();
156 thr->clock.release(&tctx->sync);
157 StatInc(thr, StatSyncRelease);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000158 tctx->creation_stack.ObtainCurrent(thr, pc);
Dmitry Vyukov09b0dbf2012-12-17 16:28:15 +0000159 tctx->creation_tid = thr->tid;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000160 }
161 return tid;
162}
163
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000164void ThreadStart(ThreadState *thr, int tid, uptr os_id) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000165 CHECK_GT(thr->in_rtl, 0);
166 uptr stk_addr = 0;
167 uptr stk_size = 0;
168 uptr tls_addr = 0;
169 uptr tls_size = 0;
Dmitry Vyukov7339eb12012-05-25 11:15:04 +0000170 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000171
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000172 if (tid) {
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000173 if (stk_addr && stk_size) {
174 MemoryResetRange(thr, /*pc=*/ 1, stk_addr, stk_size);
175 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000176
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000177 if (tls_addr && tls_size) {
178 // Check that the thr object is in tls;
179 const uptr thr_beg = (uptr)thr;
180 const uptr thr_end = (uptr)thr + sizeof(*thr);
181 CHECK_GE(thr_beg, tls_addr);
182 CHECK_LE(thr_beg, tls_addr + tls_size);
183 CHECK_GE(thr_end, tls_addr);
184 CHECK_LE(thr_end, tls_addr + tls_size);
185 // Since the thr object is huge, skip it.
186 MemoryResetRange(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
187 MemoryResetRange(thr, /*pc=*/ 2, thr_end, tls_addr + tls_size - thr_end);
188 }
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000189 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000190
191 Lock l(&CTX()->thread_mtx);
192 ThreadContext *tctx = CTX()->threads[tid];
193 CHECK_NE(tctx, 0);
194 CHECK_EQ(tctx->status, ThreadStatusCreated);
195 tctx->status = ThreadStatusRunning;
Dmitry Vyukov27d5b372012-10-02 11:52:05 +0000196 tctx->os_id = os_id;
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000197 // RoundUp so that one trace part does not contain events
198 // from different threads.
199 tctx->epoch0 = RoundUp(tctx->epoch1 + 1, kTracePartSize);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000200 tctx->epoch1 = (u64)-1;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000201 new(thr) ThreadState(CTX(), tid, tctx->unique_id,
202 tctx->epoch0, stk_addr, stk_size,
203 tls_addr, tls_size);
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000204#ifdef TSAN_GO
205 // Setup dynamic shadow stack.
206 const int kInitStackSize = 8;
207 thr->shadow_stack = (uptr*)internal_alloc(MBlockShadowStack,
208 kInitStackSize * sizeof(uptr));
209 thr->shadow_stack_pos = thr->shadow_stack;
210 thr->shadow_stack_end = thr->shadow_stack + kInitStackSize;
211#endif
Dmitry Vyukov6e406cd2013-01-24 09:08:03 +0000212#ifndef TSAN_GO
213 AllocatorThreadStart(thr);
214#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000215 tctx->thr = thr;
216 thr->fast_synch_epoch = tctx->epoch0;
217 thr->clock.set(tid, tctx->epoch0);
218 thr->clock.acquire(&tctx->sync);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000219 thr->fast_state.SetHistorySize(flags()->history_size);
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000220 const uptr trace = (tctx->epoch0 / kTracePartSize) % TraceParts();
221 thr->trace.headers[trace].epoch0 = tctx->epoch0;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000222 StatInc(thr, StatSyncAcquire);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000223 DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx "
224 "tls_addr=%zx tls_size=%zx\n",
225 tid, (uptr)tctx->epoch0, stk_addr, stk_size, tls_addr, tls_size);
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000226 thr->is_alive = true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000227}
228
229void ThreadFinish(ThreadState *thr) {
230 CHECK_GT(thr->in_rtl, 0);
231 StatInc(thr, StatThreadFinish);
232 // FIXME: Treat it as write.
233 if (thr->stk_addr && thr->stk_size)
234 MemoryResetRange(thr, /*pc=*/ 3, thr->stk_addr, thr->stk_size);
235 if (thr->tls_addr && thr->tls_size) {
236 const uptr thr_beg = (uptr)thr;
237 const uptr thr_end = (uptr)thr + sizeof(*thr);
238 // Since the thr object is huge, skip it.
239 MemoryResetRange(thr, /*pc=*/ 4, thr->tls_addr, thr_beg - thr->tls_addr);
240 MemoryResetRange(thr, /*pc=*/ 5,
241 thr_end, thr->tls_addr + thr->tls_size - thr_end);
242 }
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000243 thr->is_alive = false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000244 Context *ctx = CTX();
245 Lock l(&ctx->thread_mtx);
246 ThreadContext *tctx = ctx->threads[thr->tid];
247 CHECK_NE(tctx, 0);
248 CHECK_EQ(tctx->status, ThreadStatusRunning);
249 CHECK_GT(ctx->alive_threads, 0);
250 ctx->alive_threads--;
251 if (tctx->detached) {
252 ThreadDead(thr, tctx);
253 } else {
254 thr->fast_state.IncrementEpoch();
255 // Can't increment epoch w/o writing to the trace as well.
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000256 TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000257 thr->clock.set(thr->tid, thr->fast_state.epoch());
258 thr->fast_synch_epoch = thr->fast_state.epoch();
259 thr->clock.release(&tctx->sync);
260 StatInc(thr, StatSyncRelease);
261 tctx->status = ThreadStatusFinished;
262 }
263
264 // Save from info about the thread.
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000265 tctx->dead_info = new(internal_alloc(MBlockDeadInfo, sizeof(ThreadDeadInfo)))
266 ThreadDeadInfo();
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000267 for (uptr i = 0; i < TraceParts(); i++) {
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000268 tctx->dead_info->trace.headers[i].epoch0 = thr->trace.headers[i].epoch0;
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000269 tctx->dead_info->trace.headers[i].stack0.CopyFrom(
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000270 thr->trace.headers[i].stack0);
271 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000272 tctx->epoch1 = thr->fast_state.epoch();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000273
Dmitry Vyukov9f143c52012-08-16 19:36:45 +0000274#ifndef TSAN_GO
Dmitry Vyukov6e406cd2013-01-24 09:08:03 +0000275 AllocatorThreadFinish(thr);
Dmitry Vyukov9f143c52012-08-16 19:36:45 +0000276#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000277 thr->~ThreadState();
278 StatAggregate(ctx->stat, thr->stat);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000279 tctx->thr = 0;
280}
281
282int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
283 CHECK_GT(thr->in_rtl, 0);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000284 Context *ctx = CTX();
285 Lock l(&ctx->thread_mtx);
286 int res = -1;
Kostya Serebryany07c48052012-05-11 14:42:24 +0000287 for (unsigned tid = 0; tid < kMaxTid; tid++) {
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000288 ThreadContext *tctx = ctx->threads[tid];
289 if (tctx != 0 && tctx->user_id == uid
290 && tctx->status != ThreadStatusInvalid) {
291 tctx->user_id = 0;
292 res = tid;
293 break;
294 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000295 }
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000296 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000297 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000298}
299
300void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
301 CHECK_GT(thr->in_rtl, 0);
302 CHECK_GT(tid, 0);
303 CHECK_LT(tid, kMaxTid);
304 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
305 Context *ctx = CTX();
306 Lock l(&ctx->thread_mtx);
307 ThreadContext *tctx = ctx->threads[tid];
308 if (tctx->status == ThreadStatusInvalid) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000309 Printf("ThreadSanitizer: join of non-existent thread\n");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000310 return;
311 }
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000312 // FIXME(dvyukov): print message and continue (it's user error).
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000313 CHECK_EQ(tctx->detached, false);
314 CHECK_EQ(tctx->status, ThreadStatusFinished);
315 thr->clock.acquire(&tctx->sync);
316 StatInc(thr, StatSyncAcquire);
317 ThreadDead(thr, tctx);
318}
319
320void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
321 CHECK_GT(thr->in_rtl, 0);
322 CHECK_GT(tid, 0);
323 CHECK_LT(tid, kMaxTid);
324 Context *ctx = CTX();
325 Lock l(&ctx->thread_mtx);
326 ThreadContext *tctx = ctx->threads[tid];
327 if (tctx->status == ThreadStatusInvalid) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000328 Printf("ThreadSanitizer: detach of non-existent thread\n");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000329 return;
330 }
331 if (tctx->status == ThreadStatusFinished) {
332 ThreadDead(thr, tctx);
333 } else {
334 tctx->detached = true;
335 }
336}
337
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000338void ThreadSetName(ThreadState *thr, const char *name) {
339 Context *ctx = CTX();
340 Lock l(&ctx->thread_mtx);
341 ThreadContext *tctx = ctx->threads[thr->tid];
342 CHECK_NE(tctx, 0);
343 CHECK_EQ(tctx->status, ThreadStatusRunning);
344 if (tctx->name) {
345 internal_free(tctx->name);
346 tctx->name = 0;
347 }
348 if (name)
349 tctx->name = internal_strdup(name);
350}
351
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000352void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
353 uptr size, bool is_write) {
354 if (size == 0)
355 return;
356
357 u64 *shadow_mem = (u64*)MemToShadow(addr);
358 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
359 thr->tid, (void*)pc, (void*)addr,
360 (int)size, is_write);
361
362#if TSAN_DEBUG
363 if (!IsAppMem(addr)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000364 Printf("Access to non app mem %zx\n", addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000365 DCHECK(IsAppMem(addr));
366 }
367 if (!IsAppMem(addr + size - 1)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000368 Printf("Access to non app mem %zx\n", addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000369 DCHECK(IsAppMem(addr + size - 1));
370 }
371 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000372 Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000373 DCHECK(IsShadowMem((uptr)shadow_mem));
374 }
375 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000376 Printf("Bad shadow addr %p (%zx)\n",
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000377 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000378 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
379 }
380#endif
381
382 StatInc(thr, StatMopRange);
383
384 FastState fast_state = thr->fast_state;
385 if (fast_state.GetIgnoreBit())
386 return;
387
388 fast_state.IncrementEpoch();
389 thr->fast_state = fast_state;
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000390 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000391
392 bool unaligned = (addr % kShadowCell) != 0;
393
394 // Handle unaligned beginning, if any.
395 for (; addr % kShadowCell && size; addr++, size--) {
396 int const kAccessSizeLog = 0;
397 Shadow cur(fast_state);
398 cur.SetWrite(is_write);
399 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000400 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000401 shadow_mem, cur);
402 }
403 if (unaligned)
404 shadow_mem += kShadowCnt;
405 // Handle middle part, if any.
406 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
407 int const kAccessSizeLog = 3;
408 Shadow cur(fast_state);
409 cur.SetWrite(is_write);
410 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000411 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000412 shadow_mem, cur);
413 shadow_mem += kShadowCnt;
414 }
415 // Handle ending, if any.
416 for (; size; addr++, size--) {
417 int const kAccessSizeLog = 0;
418 Shadow cur(fast_state);
419 cur.SetWrite(is_write);
420 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukovba429142013-02-01 09:42:06 +0000421 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000422 shadow_mem, cur);
423 }
424}
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000425} // namespace __tsan