Alexey Samsonov | 3b2f9f4 | 2012-06-04 13:55:19 +0000 | [diff] [blame] | 1 | //===-- tsan_rtl_thread.cc ------------------------------------------------===// |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 2 | // |
| 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 Samsonov | 8bd9098 | 2012-06-07 09:50:16 +0000 | [diff] [blame] | 14 | #include "sanitizer_common/sanitizer_placement_new.h" |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 15 | #include "tsan_rtl.h" |
| 16 | #include "tsan_mman.h" |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 17 | #include "tsan_platform.h" |
| 18 | #include "tsan_report.h" |
| 19 | #include "tsan_sync.h" |
| 20 | |
| 21 | namespace __tsan { |
| 22 | |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 23 | // ThreadContext implementation. |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 24 | |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 25 | ThreadContext::ThreadContext(int tid) |
| 26 | : ThreadContextBase(tid) |
| 27 | , thr() |
| 28 | , sync() |
| 29 | , epoch0() |
| 30 | , epoch1() |
| 31 | , dead_info() { |
| 32 | } |
| 33 | |
| 34 | void ThreadContext::OnDead() { |
| 35 | sync.Reset(); |
| 36 | } |
| 37 | |
| 38 | void ThreadContext::OnJoined(void *arg) { |
| 39 | ThreadState *caller_thr = static_cast<ThreadState *>(arg); |
| 40 | caller_thr->clock.acquire(&sync); |
| 41 | StatInc(caller_thr, StatSyncAcquire); |
| 42 | } |
| 43 | |
| 44 | struct OnCreatedArgs { |
| 45 | ThreadState *thr; |
| 46 | uptr pc; |
| 47 | }; |
| 48 | |
| 49 | void ThreadContext::OnCreated(void *arg) { |
| 50 | thr = 0; |
Dmitry Vyukov | 5016003 | 2013-03-18 08:52:46 +0000 | [diff] [blame] | 51 | if (tid == 0) |
| 52 | return; |
| 53 | OnCreatedArgs *args = static_cast<OnCreatedArgs *>(arg); |
| 54 | args->thr->fast_state.IncrementEpoch(); |
| 55 | // Can't increment epoch w/o writing to the trace as well. |
| 56 | TraceAddEvent(args->thr, args->thr->fast_state, EventTypeMop, 0); |
| 57 | args->thr->clock.set(args->thr->tid, args->thr->fast_state.epoch()); |
| 58 | args->thr->fast_synch_epoch = args->thr->fast_state.epoch(); |
| 59 | args->thr->clock.release(&sync); |
| 60 | StatInc(args->thr, StatSyncRelease); |
Dmitry Vyukov | 7cd2025 | 2013-03-18 09:02:27 +0000 | [diff] [blame] | 61 | #ifdef TSAN_GO |
Dmitry Vyukov | 5016003 | 2013-03-18 08:52:46 +0000 | [diff] [blame] | 62 | creation_stack.ObtainCurrent(args->thr, args->pc); |
Dmitry Vyukov | 7cd2025 | 2013-03-18 09:02:27 +0000 | [diff] [blame] | 63 | #else |
| 64 | creation_stack_id = CurrentStackId(args->thr, args->pc); |
| 65 | #endif |
Dmitry Vyukov | 5016003 | 2013-03-18 08:52:46 +0000 | [diff] [blame] | 66 | if (reuse_count == 0) |
| 67 | StatInc(args->thr, StatThreadMaxTid); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void ThreadContext::OnReset(void *arg) { |
| 71 | OnCreatedArgs *args = static_cast<OnCreatedArgs *>(arg); |
| 72 | StatInc(args->thr, StatThreadReuse); |
| 73 | sync.Reset(); |
Dmitry Vyukov | a1bdd2d | 2013-03-18 09:09:41 +0000 | [diff] [blame] | 74 | DestroyAndFree(dead_info); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | struct OnStartedArgs { |
| 78 | ThreadState *thr; |
| 79 | uptr stk_addr; |
| 80 | uptr stk_size; |
| 81 | uptr tls_addr; |
| 82 | uptr tls_size; |
| 83 | }; |
| 84 | |
| 85 | void ThreadContext::OnStarted(void *arg) { |
| 86 | OnStartedArgs *args = static_cast<OnStartedArgs*>(arg); |
Dmitry Vyukov | 5016003 | 2013-03-18 08:52:46 +0000 | [diff] [blame] | 87 | thr = args->thr; |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 88 | // 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 Vyukov | 5016003 | 2013-03-18 08:52:46 +0000 | [diff] [blame] | 92 | new(thr) ThreadState(CTX(), tid, unique_id, |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 93 | epoch0, args->stk_addr, args->stk_size, args->tls_addr, args->tls_size); |
| 94 | #ifdef TSAN_GO |
| 95 | // Setup dynamic shadow stack. |
| 96 | const int kInitStackSize = 8; |
| 97 | args->thr->shadow_stack = (uptr*)internal_alloc(MBlockShadowStack, |
| 98 | kInitStackSize * sizeof(uptr)); |
| 99 | args->thr->shadow_stack_pos = thr->shadow_stack; |
| 100 | args->thr->shadow_stack_end = thr->shadow_stack + kInitStackSize; |
| 101 | #endif |
| 102 | #ifndef TSAN_GO |
| 103 | AllocatorThreadStart(args->thr); |
| 104 | #endif |
| 105 | thr = args->thr; |
| 106 | thr->fast_synch_epoch = epoch0; |
| 107 | thr->clock.set(tid, epoch0); |
| 108 | thr->clock.acquire(&sync); |
| 109 | thr->fast_state.SetHistorySize(flags()->history_size); |
| 110 | const uptr trace = (epoch0 / kTracePartSize) % TraceParts(); |
| 111 | thr->trace.headers[trace].epoch0 = epoch0; |
| 112 | StatInc(thr, StatSyncAcquire); |
| 113 | DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx " |
| 114 | "tls_addr=%zx tls_size=%zx\n", |
Alexey Samsonov | b5d10f6 | 2013-03-18 09:45:22 +0000 | [diff] [blame^] | 115 | tid, (uptr)epoch0, args->stk_addr, args->stk_size, |
| 116 | args->tls_addr, args->tls_size); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 117 | thr->is_alive = true; |
| 118 | } |
| 119 | |
| 120 | void ThreadContext::OnFinished() { |
| 121 | if (!detached) { |
| 122 | thr->fast_state.IncrementEpoch(); |
| 123 | // Can't increment epoch w/o writing to the trace as well. |
| 124 | TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0); |
| 125 | thr->clock.set(thr->tid, thr->fast_state.epoch()); |
| 126 | thr->fast_synch_epoch = thr->fast_state.epoch(); |
| 127 | thr->clock.release(&sync); |
| 128 | StatInc(thr, StatSyncRelease); |
| 129 | } |
| 130 | // Save from info about the thread. |
| 131 | dead_info = new(internal_alloc(MBlockDeadInfo, sizeof(ThreadDeadInfo))) |
| 132 | ThreadDeadInfo(); |
| 133 | for (uptr i = 0; i < TraceParts(); i++) { |
| 134 | dead_info->trace.headers[i].epoch0 = thr->trace.headers[i].epoch0; |
| 135 | dead_info->trace.headers[i].stack0.CopyFrom( |
| 136 | thr->trace.headers[i].stack0); |
| 137 | } |
| 138 | epoch1 = thr->fast_state.epoch(); |
| 139 | |
| 140 | #ifndef TSAN_GO |
| 141 | AllocatorThreadFinish(thr); |
| 142 | #endif |
| 143 | thr->~ThreadState(); |
| 144 | StatAggregate(CTX()->stat, thr->stat); |
| 145 | thr = 0; |
| 146 | } |
| 147 | |
| 148 | static void MaybeReportThreadLeak(ThreadContextBase *tctx_base, void *unused) { |
| 149 | ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 150 | if (tctx->detached) |
| 151 | return; |
| 152 | if (tctx->status != ThreadStatusCreated |
| 153 | && tctx->status != ThreadStatusRunning |
| 154 | && tctx->status != ThreadStatusFinished) |
| 155 | return; |
| 156 | ScopedReport rep(ReportTypeThreadLeak); |
| 157 | rep.AddThread(tctx); |
Dmitry Vyukov | 90c9cbf | 2012-10-05 15:51:32 +0000 | [diff] [blame] | 158 | OutputReport(CTX(), rep); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void ThreadFinalize(ThreadState *thr) { |
| 162 | CHECK_GT(thr->in_rtl, 0); |
| 163 | if (!flags()->report_thread_leaks) |
| 164 | return; |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 165 | ThreadRegistryLock l(CTX()->thread_registry); |
| 166 | CTX()->thread_registry->RunCallbackForEachThreadLocked( |
| 167 | MaybeReportThreadLeak, 0); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Dmitry Vyukov | 67dc570 | 2012-11-07 16:41:57 +0000 | [diff] [blame] | 170 | int ThreadCount(ThreadState *thr) { |
| 171 | CHECK_GT(thr->in_rtl, 0); |
| 172 | Context *ctx = CTX(); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 173 | uptr result; |
| 174 | ctx->thread_registry->GetNumberOfThreads(0, 0, &result); |
| 175 | return (int)result; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) { |
| 179 | CHECK_GT(thr->in_rtl, 0); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 180 | StatInc(thr, StatThreadCreate); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 181 | Context *ctx = CTX(); |
| 182 | OnCreatedArgs args = { thr, pc }; |
| 183 | int tid = ctx->thread_registry->CreateThread(uid, detached, thr->tid, &args); |
Alexey Samsonov | 51ae983 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 184 | DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 185 | StatSet(thr, StatThreadMaxAlive, ctx->thread_registry->GetMaxAliveThreads()); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 186 | return tid; |
| 187 | } |
| 188 | |
Dmitry Vyukov | 56faa55 | 2012-10-02 12:58:14 +0000 | [diff] [blame] | 189 | void ThreadStart(ThreadState *thr, int tid, uptr os_id) { |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 190 | CHECK_GT(thr->in_rtl, 0); |
| 191 | uptr stk_addr = 0; |
| 192 | uptr stk_size = 0; |
| 193 | uptr tls_addr = 0; |
| 194 | uptr tls_size = 0; |
Dmitry Vyukov | 7339eb1 | 2012-05-25 11:15:04 +0000 | [diff] [blame] | 195 | GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 196 | |
Dmitry Vyukov | 2d4e3c1 | 2012-05-28 07:44:34 +0000 | [diff] [blame] | 197 | if (tid) { |
Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 198 | if (stk_addr && stk_size) { |
| 199 | MemoryResetRange(thr, /*pc=*/ 1, stk_addr, stk_size); |
| 200 | } |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 201 | |
Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 202 | if (tls_addr && tls_size) { |
| 203 | // Check that the thr object is in tls; |
| 204 | const uptr thr_beg = (uptr)thr; |
| 205 | const uptr thr_end = (uptr)thr + sizeof(*thr); |
| 206 | CHECK_GE(thr_beg, tls_addr); |
| 207 | CHECK_LE(thr_beg, tls_addr + tls_size); |
| 208 | CHECK_GE(thr_end, tls_addr); |
| 209 | CHECK_LE(thr_end, tls_addr + tls_size); |
| 210 | // Since the thr object is huge, skip it. |
| 211 | MemoryResetRange(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr); |
| 212 | MemoryResetRange(thr, /*pc=*/ 2, thr_end, tls_addr + tls_size - thr_end); |
| 213 | } |
Dmitry Vyukov | 2d4e3c1 | 2012-05-28 07:44:34 +0000 | [diff] [blame] | 214 | } |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 215 | |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 216 | OnStartedArgs args = { thr, stk_addr, stk_size, tls_addr, tls_size }; |
| 217 | CTX()->thread_registry->StartThread(tid, os_id, &args); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void ThreadFinish(ThreadState *thr) { |
| 221 | CHECK_GT(thr->in_rtl, 0); |
| 222 | StatInc(thr, StatThreadFinish); |
| 223 | // FIXME: Treat it as write. |
| 224 | if (thr->stk_addr && thr->stk_size) |
| 225 | MemoryResetRange(thr, /*pc=*/ 3, thr->stk_addr, thr->stk_size); |
| 226 | if (thr->tls_addr && thr->tls_size) { |
| 227 | const uptr thr_beg = (uptr)thr; |
| 228 | const uptr thr_end = (uptr)thr + sizeof(*thr); |
| 229 | // Since the thr object is huge, skip it. |
| 230 | MemoryResetRange(thr, /*pc=*/ 4, thr->tls_addr, thr_beg - thr->tls_addr); |
| 231 | MemoryResetRange(thr, /*pc=*/ 5, |
| 232 | thr_end, thr->tls_addr + thr->tls_size - thr_end); |
| 233 | } |
Dmitry Vyukov | fa985a0 | 2012-06-28 18:07:46 +0000 | [diff] [blame] | 234 | thr->is_alive = false; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 235 | Context *ctx = CTX(); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 236 | ctx->thread_registry->FinishThread(thr->tid); |
| 237 | } |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 238 | |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 239 | static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) { |
| 240 | uptr uid = (uptr)arg; |
| 241 | if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) { |
| 242 | tctx->user_id = 0; |
| 243 | return true; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 244 | } |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 245 | return false; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | int ThreadTid(ThreadState *thr, uptr pc, uptr uid) { |
| 249 | CHECK_GT(thr->in_rtl, 0); |
Dmitry Vyukov | 880bb66 | 2012-05-28 17:32:50 +0000 | [diff] [blame] | 250 | Context *ctx = CTX(); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 251 | int res = ctx->thread_registry->FindThread(FindThreadByUid, (void*)uid); |
Alexey Samsonov | 51ae983 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 252 | DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res); |
Dmitry Vyukov | 880bb66 | 2012-05-28 17:32:50 +0000 | [diff] [blame] | 253 | return res; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void ThreadJoin(ThreadState *thr, uptr pc, int tid) { |
| 257 | CHECK_GT(thr->in_rtl, 0); |
| 258 | CHECK_GT(tid, 0); |
| 259 | CHECK_LT(tid, kMaxTid); |
| 260 | DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid); |
| 261 | Context *ctx = CTX(); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 262 | ctx->thread_registry->JoinThread(tid, thr); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void ThreadDetach(ThreadState *thr, uptr pc, int tid) { |
| 266 | CHECK_GT(thr->in_rtl, 0); |
| 267 | CHECK_GT(tid, 0); |
| 268 | CHECK_LT(tid, kMaxTid); |
| 269 | Context *ctx = CTX(); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 270 | ctx->thread_registry->DetachThread(tid); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Dmitry Vyukov | 1b46993 | 2012-12-04 15:46:05 +0000 | [diff] [blame] | 273 | void ThreadSetName(ThreadState *thr, const char *name) { |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 274 | CHECK_GT(thr->in_rtl, 0); |
| 275 | CTX()->thread_registry->SetThreadName(thr->tid, name); |
Dmitry Vyukov | 1b46993 | 2012-12-04 15:46:05 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 278 | void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr, |
| 279 | uptr size, bool is_write) { |
| 280 | if (size == 0) |
| 281 | return; |
| 282 | |
| 283 | u64 *shadow_mem = (u64*)MemToShadow(addr); |
| 284 | DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n", |
| 285 | thr->tid, (void*)pc, (void*)addr, |
| 286 | (int)size, is_write); |
| 287 | |
| 288 | #if TSAN_DEBUG |
| 289 | if (!IsAppMem(addr)) { |
Alexey Samsonov | ad9d65f | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 290 | Printf("Access to non app mem %zx\n", addr); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 291 | DCHECK(IsAppMem(addr)); |
| 292 | } |
| 293 | if (!IsAppMem(addr + size - 1)) { |
Alexey Samsonov | ad9d65f | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 294 | Printf("Access to non app mem %zx\n", addr + size - 1); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 295 | DCHECK(IsAppMem(addr + size - 1)); |
| 296 | } |
| 297 | if (!IsShadowMem((uptr)shadow_mem)) { |
Alexey Samsonov | ad9d65f | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 298 | Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 299 | DCHECK(IsShadowMem((uptr)shadow_mem)); |
| 300 | } |
| 301 | if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) { |
Alexey Samsonov | ad9d65f | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 302 | Printf("Bad shadow addr %p (%zx)\n", |
Alexey Samsonov | 51ae983 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 303 | shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 304 | DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))); |
| 305 | } |
| 306 | #endif |
| 307 | |
| 308 | StatInc(thr, StatMopRange); |
| 309 | |
| 310 | FastState fast_state = thr->fast_state; |
| 311 | if (fast_state.GetIgnoreBit()) |
| 312 | return; |
| 313 | |
| 314 | fast_state.IncrementEpoch(); |
| 315 | thr->fast_state = fast_state; |
Dmitry Vyukov | 2429b02 | 2012-11-28 10:35:31 +0000 | [diff] [blame] | 316 | TraceAddEvent(thr, fast_state, EventTypeMop, pc); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 317 | |
| 318 | bool unaligned = (addr % kShadowCell) != 0; |
| 319 | |
| 320 | // Handle unaligned beginning, if any. |
| 321 | for (; addr % kShadowCell && size; addr++, size--) { |
| 322 | int const kAccessSizeLog = 0; |
| 323 | Shadow cur(fast_state); |
| 324 | cur.SetWrite(is_write); |
| 325 | cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog); |
Dmitry Vyukov | ba42914 | 2013-02-01 09:42:06 +0000 | [diff] [blame] | 326 | MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false, |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 327 | shadow_mem, cur); |
| 328 | } |
| 329 | if (unaligned) |
| 330 | shadow_mem += kShadowCnt; |
| 331 | // Handle middle part, if any. |
| 332 | for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) { |
| 333 | int const kAccessSizeLog = 3; |
| 334 | Shadow cur(fast_state); |
| 335 | cur.SetWrite(is_write); |
| 336 | cur.SetAddr0AndSizeLog(0, kAccessSizeLog); |
Dmitry Vyukov | ba42914 | 2013-02-01 09:42:06 +0000 | [diff] [blame] | 337 | MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false, |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 338 | shadow_mem, cur); |
| 339 | shadow_mem += kShadowCnt; |
| 340 | } |
| 341 | // Handle ending, if any. |
| 342 | for (; size; addr++, size--) { |
| 343 | int const kAccessSizeLog = 0; |
| 344 | Shadow cur(fast_state); |
| 345 | cur.SetWrite(is_write); |
| 346 | cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog); |
Dmitry Vyukov | ba42914 | 2013-02-01 09:42:06 +0000 | [diff] [blame] | 347 | MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false, |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 348 | shadow_mem, cur); |
| 349 | } |
| 350 | } |
Dmitry Vyukov | 3c2489e | 2013-02-13 13:05:36 +0000 | [diff] [blame] | 351 | |
| 352 | void MemoryAccessRangeStep(ThreadState *thr, uptr pc, uptr addr, |
| 353 | uptr size, uptr step, bool is_write) { |
| 354 | if (size == 0) |
| 355 | return; |
| 356 | FastState fast_state = thr->fast_state; |
| 357 | if (fast_state.GetIgnoreBit()) |
| 358 | return; |
| 359 | StatInc(thr, StatMopRange); |
| 360 | fast_state.IncrementEpoch(); |
| 361 | thr->fast_state = fast_state; |
| 362 | TraceAddEvent(thr, fast_state, EventTypeMop, pc); |
| 363 | |
| 364 | for (uptr addr_end = addr + size; addr < addr_end; addr += step) { |
| 365 | u64 *shadow_mem = (u64*)MemToShadow(addr); |
| 366 | Shadow cur(fast_state); |
| 367 | cur.SetWrite(is_write); |
| 368 | cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kSizeLog1); |
| 369 | MemoryAccessImpl(thr, addr, kSizeLog1, is_write, false, |
| 370 | shadow_mem, cur); |
| 371 | } |
| 372 | } |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 373 | } // namespace __tsan |