blob: 35977592783453ab49eaf07ea684ec72dfd46fd9 [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
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000212 tctx->thr = thr;
213 thr->fast_synch_epoch = tctx->epoch0;
214 thr->clock.set(tid, tctx->epoch0);
215 thr->clock.acquire(&tctx->sync);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000216 thr->fast_state.SetHistorySize(flags()->history_size);
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000217 const uptr trace = (tctx->epoch0 / kTracePartSize) % TraceParts();
218 thr->trace.headers[trace].epoch0 = tctx->epoch0;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000219 StatInc(thr, StatSyncAcquire);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000220 DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx "
221 "tls_addr=%zx tls_size=%zx\n",
222 tid, (uptr)tctx->epoch0, stk_addr, stk_size, tls_addr, tls_size);
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000223 thr->is_alive = true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000224}
225
226void ThreadFinish(ThreadState *thr) {
227 CHECK_GT(thr->in_rtl, 0);
228 StatInc(thr, StatThreadFinish);
229 // FIXME: Treat it as write.
230 if (thr->stk_addr && thr->stk_size)
231 MemoryResetRange(thr, /*pc=*/ 3, thr->stk_addr, thr->stk_size);
232 if (thr->tls_addr && thr->tls_size) {
233 const uptr thr_beg = (uptr)thr;
234 const uptr thr_end = (uptr)thr + sizeof(*thr);
235 // Since the thr object is huge, skip it.
236 MemoryResetRange(thr, /*pc=*/ 4, thr->tls_addr, thr_beg - thr->tls_addr);
237 MemoryResetRange(thr, /*pc=*/ 5,
238 thr_end, thr->tls_addr + thr->tls_size - thr_end);
239 }
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000240 thr->is_alive = false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000241 Context *ctx = CTX();
242 Lock l(&ctx->thread_mtx);
243 ThreadContext *tctx = ctx->threads[thr->tid];
244 CHECK_NE(tctx, 0);
245 CHECK_EQ(tctx->status, ThreadStatusRunning);
246 CHECK_GT(ctx->alive_threads, 0);
247 ctx->alive_threads--;
248 if (tctx->detached) {
249 ThreadDead(thr, tctx);
250 } else {
251 thr->fast_state.IncrementEpoch();
252 // Can't increment epoch w/o writing to the trace as well.
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000253 TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000254 thr->clock.set(thr->tid, thr->fast_state.epoch());
255 thr->fast_synch_epoch = thr->fast_state.epoch();
256 thr->clock.release(&tctx->sync);
257 StatInc(thr, StatSyncRelease);
258 tctx->status = ThreadStatusFinished;
259 }
260
261 // Save from info about the thread.
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000262 tctx->dead_info = new(internal_alloc(MBlockDeadInfo, sizeof(ThreadDeadInfo)))
263 ThreadDeadInfo();
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000264 for (uptr i = 0; i < TraceParts(); i++) {
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000265 tctx->dead_info->trace.headers[i].epoch0 = thr->trace.headers[i].epoch0;
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000266 tctx->dead_info->trace.headers[i].stack0.CopyFrom(
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000267 thr->trace.headers[i].stack0);
268 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000269 tctx->epoch1 = thr->fast_state.epoch();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000270
Dmitry Vyukov9f143c52012-08-16 19:36:45 +0000271#ifndef TSAN_GO
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000272 AlloctorThreadFinish(thr);
Dmitry Vyukov9f143c52012-08-16 19:36:45 +0000273#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000274 thr->~ThreadState();
275 StatAggregate(ctx->stat, thr->stat);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000276 tctx->thr = 0;
277}
278
279int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
280 CHECK_GT(thr->in_rtl, 0);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000281 Context *ctx = CTX();
282 Lock l(&ctx->thread_mtx);
283 int res = -1;
Kostya Serebryany07c48052012-05-11 14:42:24 +0000284 for (unsigned tid = 0; tid < kMaxTid; tid++) {
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000285 ThreadContext *tctx = ctx->threads[tid];
286 if (tctx != 0 && tctx->user_id == uid
287 && tctx->status != ThreadStatusInvalid) {
288 tctx->user_id = 0;
289 res = tid;
290 break;
291 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000292 }
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000293 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000294 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000295}
296
297void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
298 CHECK_GT(thr->in_rtl, 0);
299 CHECK_GT(tid, 0);
300 CHECK_LT(tid, kMaxTid);
301 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
302 Context *ctx = CTX();
303 Lock l(&ctx->thread_mtx);
304 ThreadContext *tctx = ctx->threads[tid];
305 if (tctx->status == ThreadStatusInvalid) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000306 Printf("ThreadSanitizer: join of non-existent thread\n");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000307 return;
308 }
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000309 // FIXME(dvyukov): print message and continue (it's user error).
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000310 CHECK_EQ(tctx->detached, false);
311 CHECK_EQ(tctx->status, ThreadStatusFinished);
312 thr->clock.acquire(&tctx->sync);
313 StatInc(thr, StatSyncAcquire);
314 ThreadDead(thr, tctx);
315}
316
317void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
318 CHECK_GT(thr->in_rtl, 0);
319 CHECK_GT(tid, 0);
320 CHECK_LT(tid, kMaxTid);
321 Context *ctx = CTX();
322 Lock l(&ctx->thread_mtx);
323 ThreadContext *tctx = ctx->threads[tid];
324 if (tctx->status == ThreadStatusInvalid) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000325 Printf("ThreadSanitizer: detach of non-existent thread\n");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000326 return;
327 }
328 if (tctx->status == ThreadStatusFinished) {
329 ThreadDead(thr, tctx);
330 } else {
331 tctx->detached = true;
332 }
333}
334
Dmitry Vyukov1b469932012-12-04 15:46:05 +0000335void ThreadSetName(ThreadState *thr, const char *name) {
336 Context *ctx = CTX();
337 Lock l(&ctx->thread_mtx);
338 ThreadContext *tctx = ctx->threads[thr->tid];
339 CHECK_NE(tctx, 0);
340 CHECK_EQ(tctx->status, ThreadStatusRunning);
341 if (tctx->name) {
342 internal_free(tctx->name);
343 tctx->name = 0;
344 }
345 if (name)
346 tctx->name = internal_strdup(name);
347}
348
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000349void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
350 uptr size, bool is_write) {
351 if (size == 0)
352 return;
353
354 u64 *shadow_mem = (u64*)MemToShadow(addr);
355 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
356 thr->tid, (void*)pc, (void*)addr,
357 (int)size, is_write);
358
359#if TSAN_DEBUG
360 if (!IsAppMem(addr)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000361 Printf("Access to non app mem %zx\n", addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000362 DCHECK(IsAppMem(addr));
363 }
364 if (!IsAppMem(addr + size - 1)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000365 Printf("Access to non app mem %zx\n", addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000366 DCHECK(IsAppMem(addr + size - 1));
367 }
368 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000369 Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000370 DCHECK(IsShadowMem((uptr)shadow_mem));
371 }
372 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000373 Printf("Bad shadow addr %p (%zx)\n",
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000374 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000375 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
376 }
377#endif
378
379 StatInc(thr, StatMopRange);
380
381 FastState fast_state = thr->fast_state;
382 if (fast_state.GetIgnoreBit())
383 return;
384
385 fast_state.IncrementEpoch();
386 thr->fast_state = fast_state;
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000387 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000388
389 bool unaligned = (addr % kShadowCell) != 0;
390
391 // Handle unaligned beginning, if any.
392 for (; addr % kShadowCell && size; addr++, size--) {
393 int const kAccessSizeLog = 0;
394 Shadow cur(fast_state);
395 cur.SetWrite(is_write);
396 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukov933c9882012-11-15 18:49:08 +0000397 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000398 shadow_mem, cur);
399 }
400 if (unaligned)
401 shadow_mem += kShadowCnt;
402 // Handle middle part, if any.
403 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
404 int const kAccessSizeLog = 3;
405 Shadow cur(fast_state);
406 cur.SetWrite(is_write);
407 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
Dmitry Vyukov933c9882012-11-15 18:49:08 +0000408 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000409 shadow_mem, cur);
410 shadow_mem += kShadowCnt;
411 }
412 // Handle ending, if any.
413 for (; size; addr++, size--) {
414 int const kAccessSizeLog = 0;
415 Shadow cur(fast_state);
416 cur.SetWrite(is_write);
417 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukov933c9882012-11-15 18:49:08 +0000418 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000419 shadow_mem, cur);
420 }
421}
422
423void MemoryRead1Byte(ThreadState *thr, uptr pc, uptr addr) {
424 MemoryAccess(thr, pc, addr, 0, 0);
425}
426
427void MemoryWrite1Byte(ThreadState *thr, uptr pc, uptr addr) {
428 MemoryAccess(thr, pc, addr, 0, 1);
429}
430
431void MemoryRead8Byte(ThreadState *thr, uptr pc, uptr addr) {
432 MemoryAccess(thr, pc, addr, 3, 0);
433}
434
435void MemoryWrite8Byte(ThreadState *thr, uptr pc, uptr addr) {
436 MemoryAccess(thr, pc, addr, 3, 1);
437}
438} // namespace __tsan