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