blob: 66c78cfdd7c044ba05a138a8ad0b3d87150edd95 [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);
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 Vyukove1ddbf92013-10-10 15:58:12 +000061 ReleaseImpl(args->thr, 0, &sync);
Dmitry Vyukov2c5284e2013-03-18 09:02:27 +000062 creation_stack_id = CurrentStackId(args->thr, args->pc);
Dmitry Vyukov491852e2013-03-18 08:52:46 +000063 if (reuse_count == 0)
64 StatInc(args->thr, StatThreadMaxTid);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000065}
66
Dmitry Vyukovce85e032013-03-19 12:25:48 +000067void ThreadContext::OnReset() {
Stephen Hines6d186232014-11-26 17:56:19 -080068 CHECK_EQ(sync.size(), 0);
Dmitry Vyukov9743d742013-03-20 10:31:53 +000069 FlushUnneededShadowMemory(GetThreadTrace(tid), TraceSize() * sizeof(Event));
70 //!!! FlushUnneededShadowMemory(GetThreadTraceHeader(tid), sizeof(Trace));
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +000071}
72
Stephen Hines6d186232014-11-26 17:56:19 -080073void ThreadContext::OnDetached(void *arg) {
74 ThreadState *thr1 = static_cast<ThreadState*>(arg);
75 sync.Reset(&thr1->clock_cache);
76}
77
Alexey Samsonov2bbd8be2013-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 Vyukov491852e2013-03-18 08:52:46 +000088 thr = args->thr;
Alexey Samsonov2bbd8be2013-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;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070093 new(thr) ThreadState(ctx, tid, unique_id, epoch0, reuse_count,
94 args->stk_addr, args->stk_size, args->tls_addr, args->tls_size);
Stephen Hines86277eb2015-03-23 12:06:32 -070095#ifndef SANITIZER_GO
Dmitry Vyukov01a7ce82013-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 Samsonov2bbd8be2013-03-15 13:48:44 +0000100 // Setup dynamic shadow stack.
101 const int kInitStackSize = 8;
Dmitry Vyukov01a7ce82013-10-16 15:35:12 +0000102 thr->shadow_stack = (uptr*)internal_alloc(MBlockShadowStack,
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000103 kInitStackSize * sizeof(uptr));
Dmitry Vyukov01a7ce82013-10-16 15:35:12 +0000104 thr->shadow_stack_pos = thr->shadow_stack;
105 thr->shadow_stack_end = thr->shadow_stack + kInitStackSize;
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000106#endif
Stephen Hines86277eb2015-03-23 12:06:32 -0700107#ifndef SANITIZER_GO
Dmitry Vyukov01a7ce82013-10-16 15:35:12 +0000108 AllocatorThreadStart(thr);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000109#endif
Stephen Hines6d186232014-11-26 17:56:19 -0800110 if (common_flags()->detect_deadlocks) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700111 thr->dd_pt = ctx->dd->CreatePhysicalThread();
112 thr->dd_lt = ctx->dd->CreateLogicalThread(unique_id);
113 }
Stephen Hines86277eb2015-03-23 12:06:32 -0700114 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 Samsonov2bbd8be2013-03-15 13:48:44 +0000119 thr->fast_synch_epoch = epoch0;
Dmitry Vyukove1ddbf92013-10-10 15:58:12 +0000120 AcquireImpl(thr, 0, &sync);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000121 StatInc(thr, StatSyncAcquire);
Stephen Hines6d186232014-11-26 17:56:19 -0800122 sync.Reset(&thr->clock_cache);
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700123 thr->is_inited = true;
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000124 DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx "
125 "tls_addr=%zx tls_size=%zx\n",
Alexey Samsonov036f9332013-03-18 09:45:22 +0000126 tid, (uptr)epoch0, args->stk_addr, args->stk_size,
127 args->tls_addr, args->tls_size);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000128}
129
130void ThreadContext::OnFinished() {
131 if (!detached) {
132 thr->fast_state.IncrementEpoch();
133 // Can't increment epoch w/o writing to the trace as well.
134 TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
Dmitry Vyukove1ddbf92013-10-10 15:58:12 +0000135 ReleaseImpl(thr, 0, &sync);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000136 }
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000137 epoch1 = thr->fast_state.epoch();
138
Stephen Hines6d186232014-11-26 17:56:19 -0800139 if (common_flags()->detect_deadlocks) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700140 ctx->dd->DestroyPhysicalThread(thr->dd_pt);
141 ctx->dd->DestroyLogicalThread(thr->dd_lt);
142 }
Stephen Hines6d186232014-11-26 17:56:19 -0800143 ctx->clock_alloc.FlushCache(&thr->clock_cache);
Stephen Hines6a211c52014-07-21 00:49:56 -0700144 ctx->metamap.OnThreadIdle(thr);
Stephen Hines86277eb2015-03-23 12:06:32 -0700145#ifndef SANITIZER_GO
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000146 AllocatorThreadFinish(thr);
147#endif
148 thr->~ThreadState();
Stephen Hines86277eb2015-03-23 12:06:32 -0700149#if TSAN_COLLECT_STATS
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700150 StatAggregate(ctx->stat, thr->stat);
Stephen Hines86277eb2015-03-23 12:06:32 -0700151#endif
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000152 thr = 0;
153}
154
Stephen Hines86277eb2015-03-23 12:06:32 -0700155#ifndef SANITIZER_GO
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000156struct ThreadLeak {
157 ThreadContext *tctx;
158 int count;
159};
160
161static void MaybeReportThreadLeak(ThreadContextBase *tctx_base, void *arg) {
162 Vector<ThreadLeak> &leaks = *(Vector<ThreadLeak>*)arg;
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000163 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000164 if (tctx->detached || tctx->status != ThreadStatusFinished)
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000165 return;
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000166 for (uptr i = 0; i < leaks.Size(); i++) {
167 if (leaks[i].tctx->creation_stack_id == tctx->creation_stack_id) {
168 leaks[i].count++;
169 return;
170 }
171 }
172 ThreadLeak leak = {tctx, 1};
173 leaks.PushBack(leak);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000174}
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000175#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000176
Stephen Hines86277eb2015-03-23 12:06:32 -0700177#ifndef SANITIZER_GO
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700178static void ReportIgnoresEnabled(ThreadContext *tctx, IgnoreSet *set) {
179 if (tctx->tid == 0) {
180 Printf("ThreadSanitizer: main thread finished with ignores enabled\n");
181 } else {
182 Printf("ThreadSanitizer: thread T%d %s finished with ignores enabled,"
183 " created at:\n", tctx->tid, tctx->name);
184 PrintStack(SymbolizeStackId(tctx->creation_stack_id));
Dmitry Vyukovdc563c02013-05-21 08:12:35 +0000185 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700186 Printf(" One of the following ignores was not ended"
187 " (in order of probability)\n");
188 for (uptr i = 0; i < set->Size(); i++) {
189 Printf(" Ignore was enabled at:\n");
190 PrintStack(SymbolizeStackId(set->At(i)));
Dmitry Vyukove1ddbf92013-10-10 15:58:12 +0000191 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700192 Die();
Dmitry Vyukovdc563c02013-05-21 08:12:35 +0000193}
194
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700195static void ThreadCheckIgnore(ThreadState *thr) {
196 if (ctx->after_multithreaded_fork)
197 return;
198 if (thr->ignore_reads_and_writes)
199 ReportIgnoresEnabled(thr->tctx, &thr->mop_ignore_set);
200 if (thr->ignore_sync)
201 ReportIgnoresEnabled(thr->tctx, &thr->sync_ignore_set);
202}
203#else
204static void ThreadCheckIgnore(ThreadState *thr) {}
205#endif
206
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000207void ThreadFinalize(ThreadState *thr) {
Dmitry Vyukovdc563c02013-05-21 08:12:35 +0000208 ThreadCheckIgnore(thr);
Stephen Hines86277eb2015-03-23 12:06:32 -0700209#ifndef SANITIZER_GO
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000210 if (!flags()->report_thread_leaks)
211 return;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700212 ThreadRegistryLock l(ctx->thread_registry);
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000213 Vector<ThreadLeak> leaks(MBlockScopedBuf);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700214 ctx->thread_registry->RunCallbackForEachThreadLocked(
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000215 MaybeReportThreadLeak, &leaks);
216 for (uptr i = 0; i < leaks.Size(); i++) {
217 ScopedReport rep(ReportTypeThreadLeak);
Stephen Hines6a211c52014-07-21 00:49:56 -0700218 rep.AddThread(leaks[i].tctx, true);
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000219 rep.SetCount(leaks[i].count);
Stephen Hines6a211c52014-07-21 00:49:56 -0700220 OutputReport(thr, rep);
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000221 }
222#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000223}
224
Dmitry Vyukov54e0a9a2012-11-07 16:41:57 +0000225int ThreadCount(ThreadState *thr) {
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000226 uptr result;
227 ctx->thread_registry->GetNumberOfThreads(0, 0, &result);
228 return (int)result;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000229}
230
231int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000232 StatInc(thr, StatThreadCreate);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000233 OnCreatedArgs args = { thr, pc };
234 int tid = ctx->thread_registry->CreateThread(uid, detached, thr->tid, &args);
Alexey Samsonove9541012012-06-06 13:11:29 +0000235 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000236 StatSet(thr, StatThreadMaxAlive, ctx->thread_registry->GetMaxAliveThreads());
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000237 return tid;
238}
239
Dmitry Vyukove0023f72012-10-02 12:58:14 +0000240void ThreadStart(ThreadState *thr, int tid, uptr os_id) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000241 uptr stk_addr = 0;
242 uptr stk_size = 0;
243 uptr tls_addr = 0;
244 uptr tls_size = 0;
Stephen Hines86277eb2015-03-23 12:06:32 -0700245#ifndef SANITIZER_GO
Dmitry Vyukov789b6c52012-05-25 11:15:04 +0000246 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000247
Dmitry Vyukovaf154b82012-05-28 07:44:34 +0000248 if (tid) {
Dmitry Vyukov7ac33ac2013-03-18 15:49:07 +0000249 if (stk_addr && stk_size)
Dmitry Vyukov74172de2013-03-18 16:56:48 +0000250 MemoryRangeImitateWrite(thr, /*pc=*/ 1, stk_addr, stk_size);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000251
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000252 if (tls_addr && tls_size) {
253 // Check that the thr object is in tls;
254 const uptr thr_beg = (uptr)thr;
255 const uptr thr_end = (uptr)thr + sizeof(*thr);
256 CHECK_GE(thr_beg, tls_addr);
257 CHECK_LE(thr_beg, tls_addr + tls_size);
258 CHECK_GE(thr_end, tls_addr);
259 CHECK_LE(thr_end, tls_addr + tls_size);
260 // Since the thr object is huge, skip it.
Dmitry Vyukov74172de2013-03-18 16:56:48 +0000261 MemoryRangeImitateWrite(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
262 MemoryRangeImitateWrite(thr, /*pc=*/ 2,
263 thr_end, tls_addr + tls_size - thr_end);
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000264 }
Dmitry Vyukovaf154b82012-05-28 07:44:34 +0000265 }
Stephen Hines86277eb2015-03-23 12:06:32 -0700266#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000267
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700268 ThreadRegistry *tr = ctx->thread_registry;
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000269 OnStartedArgs args = { thr, stk_addr, stk_size, tls_addr, tls_size };
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700270 tr->StartThread(tid, os_id, &args);
271
272 tr->Lock();
273 thr->tctx = (ThreadContext*)tr->GetThreadLocked(tid);
274 tr->Unlock();
275
Stephen Hines86277eb2015-03-23 12:06:32 -0700276#ifndef SANITIZER_GO
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700277 if (ctx->after_multithreaded_fork) {
278 thr->ignore_interceptors++;
279 ThreadIgnoreBegin(thr, 0);
280 ThreadIgnoreSyncBegin(thr, 0);
281 }
282#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000283}
284
285void ThreadFinish(ThreadState *thr) {
Dmitry Vyukovdc563c02013-05-21 08:12:35 +0000286 ThreadCheckIgnore(thr);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000287 StatInc(thr, StatThreadFinish);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000288 if (thr->stk_addr && thr->stk_size)
Dmitry Vyukov7ac33ac2013-03-18 15:49:07 +0000289 DontNeedShadowFor(thr->stk_addr, thr->stk_size);
290 if (thr->tls_addr && thr->tls_size)
291 DontNeedShadowFor(thr->tls_addr, thr->tls_size);
Stephen Hines6d186232014-11-26 17:56:19 -0800292 thr->is_dead = true;
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000293 ctx->thread_registry->FinishThread(thr->tid);
294}
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000295
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000296static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) {
297 uptr uid = (uptr)arg;
298 if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) {
299 tctx->user_id = 0;
300 return true;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000301 }
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000302 return false;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000303}
304
305int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000306 int res = ctx->thread_registry->FindThread(FindThreadByUid, (void*)uid);
Alexey Samsonove9541012012-06-06 13:11:29 +0000307 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
Dmitry Vyukov411b2c92012-05-28 17:32:50 +0000308 return res;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000309}
310
311void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000312 CHECK_GT(tid, 0);
313 CHECK_LT(tid, kMaxTid);
314 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000315 ctx->thread_registry->JoinThread(tid, thr);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000316}
317
318void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000319 CHECK_GT(tid, 0);
320 CHECK_LT(tid, kMaxTid);
Stephen Hines6d186232014-11-26 17:56:19 -0800321 ctx->thread_registry->DetachThread(tid, thr);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000322}
323
Dmitry Vyukovaecf2e52012-12-04 15:46:05 +0000324void ThreadSetName(ThreadState *thr, const char *name) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700325 ctx->thread_registry->SetThreadName(thr->tid, name);
Dmitry Vyukovaecf2e52012-12-04 15:46:05 +0000326}
327
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000328void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
329 uptr size, bool is_write) {
330 if (size == 0)
331 return;
332
333 u64 *shadow_mem = (u64*)MemToShadow(addr);
334 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
335 thr->tid, (void*)pc, (void*)addr,
336 (int)size, is_write);
337
Stephen Hines86277eb2015-03-23 12:06:32 -0700338#if SANITIZER_DEBUG
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000339 if (!IsAppMem(addr)) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000340 Printf("Access to non app mem %zx\n", addr);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000341 DCHECK(IsAppMem(addr));
342 }
343 if (!IsAppMem(addr + size - 1)) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000344 Printf("Access to non app mem %zx\n", addr + size - 1);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000345 DCHECK(IsAppMem(addr + size - 1));
346 }
347 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000348 Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000349 DCHECK(IsShadowMem((uptr)shadow_mem));
350 }
351 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000352 Printf("Bad shadow addr %p (%zx)\n",
Alexey Samsonove9541012012-06-06 13:11:29 +0000353 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000354 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
355 }
356#endif
357
358 StatInc(thr, StatMopRange);
359
Dmitry Vyukov82dbc512013-03-20 13:21:50 +0000360 if (*shadow_mem == kShadowRodata) {
361 // Access to .rodata section, no races here.
362 // Measurements show that it can be 10-20% of all memory accesses.
363 StatInc(thr, StatMopRangeRodata);
364 return;
365 }
366
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000367 FastState fast_state = thr->fast_state;
368 if (fast_state.GetIgnoreBit())
369 return;
370
371 fast_state.IncrementEpoch();
372 thr->fast_state = fast_state;
Dmitry Vyukov385542a2012-11-28 10:35:31 +0000373 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000374
375 bool unaligned = (addr % kShadowCell) != 0;
376
377 // Handle unaligned beginning, if any.
378 for (; addr % kShadowCell && size; addr++, size--) {
379 int const kAccessSizeLog = 0;
380 Shadow cur(fast_state);
381 cur.SetWrite(is_write);
382 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000383 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000384 shadow_mem, cur);
385 }
386 if (unaligned)
387 shadow_mem += kShadowCnt;
388 // Handle middle part, if any.
389 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
390 int const kAccessSizeLog = 3;
391 Shadow cur(fast_state);
392 cur.SetWrite(is_write);
393 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000394 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000395 shadow_mem, cur);
396 shadow_mem += kShadowCnt;
397 }
398 // Handle ending, if any.
399 for (; size; addr++, size--) {
400 int const kAccessSizeLog = 0;
401 Shadow cur(fast_state);
402 cur.SetWrite(is_write);
403 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
Dmitry Vyukov334553e2013-02-01 09:42:06 +0000404 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000405 shadow_mem, cur);
406 }
407}
Dmitry Vyukoveaa01902013-02-13 13:05:36 +0000408
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000409} // namespace __tsan