Alexey Samsonov | 603c4be | 2012-06-04 13:55:19 +0000 | [diff] [blame] | 1 | //===-- tsan_interface_ann.cc ---------------------------------------------===// |
Kostya Serebryany | 7ac4148 | 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 | //===----------------------------------------------------------------------===// |
Alexey Samsonov | c0d78c1 | 2012-06-04 13:27:49 +0000 | [diff] [blame] | 13 | #include "sanitizer_common/sanitizer_libc.h" |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 14 | #include "sanitizer_common/sanitizer_internal_defs.h" |
Alexey Samsonov | 47b1634 | 2012-06-07 09:50:16 +0000 | [diff] [blame] | 15 | #include "sanitizer_common/sanitizer_placement_new.h" |
Dmitry Vyukov | e7718bc | 2013-06-17 19:57:03 +0000 | [diff] [blame] | 16 | #include "sanitizer_common/sanitizer_stacktrace.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 17 | #include "tsan_interface_ann.h" |
| 18 | #include "tsan_mutex.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 19 | #include "tsan_report.h" |
| 20 | #include "tsan_rtl.h" |
| 21 | #include "tsan_mman.h" |
| 22 | #include "tsan_flags.h" |
Dmitry Vyukov | 3fb4441 | 2012-06-14 21:40:35 +0000 | [diff] [blame] | 23 | #include "tsan_platform.h" |
Dmitry Vyukov | 0fd908c | 2013-03-28 16:21:19 +0000 | [diff] [blame] | 24 | #include "tsan_vector.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 25 | |
| 26 | #define CALLERPC ((uptr)__builtin_return_address(0)) |
| 27 | |
| 28 | using namespace __tsan; // NOLINT |
| 29 | |
| 30 | namespace __tsan { |
| 31 | |
| 32 | class ScopedAnnotation { |
| 33 | public: |
| 34 | ScopedAnnotation(ThreadState *thr, const char *aname, const char *f, int l, |
| 35 | uptr pc) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 36 | : thr_(thr) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 37 | FuncEntry(thr_, pc); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 38 | DPrintf("#%d: annotation %s() %s:%d\n", thr_->tid, aname, f, l); |
| 39 | } |
| 40 | |
| 41 | ~ScopedAnnotation() { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 42 | FuncExit(thr_); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 43 | CheckNoLocks(thr_); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 44 | } |
| 45 | private: |
| 46 | ThreadState *const thr_; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | #define SCOPED_ANNOTATION(typ) \ |
| 50 | if (!flags()->enable_annotations) \ |
| 51 | return; \ |
| 52 | ThreadState *thr = cur_thread(); \ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 53 | const uptr caller_pc = (uptr)__builtin_return_address(0); \ |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 54 | StatInc(thr, StatAnnotation); \ |
| 55 | StatInc(thr, Stat##typ); \ |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 56 | ScopedAnnotation sa(thr, __func__, f, l, caller_pc); \ |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 57 | const uptr pc = StackTrace::GetCurrentPc(); \ |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 58 | (void)pc; \ |
| 59 | /**/ |
| 60 | |
| 61 | static const int kMaxDescLen = 128; |
| 62 | |
| 63 | struct ExpectRace { |
| 64 | ExpectRace *next; |
| 65 | ExpectRace *prev; |
| 66 | int hitcount; |
Dmitry Vyukov | 0fd908c | 2013-03-28 16:21:19 +0000 | [diff] [blame] | 67 | int addcount; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 68 | uptr addr; |
| 69 | uptr size; |
| 70 | char *file; |
| 71 | int line; |
| 72 | char desc[kMaxDescLen]; |
| 73 | }; |
| 74 | |
| 75 | struct DynamicAnnContext { |
| 76 | Mutex mtx; |
| 77 | ExpectRace expect; |
| 78 | ExpectRace benign; |
| 79 | |
| 80 | DynamicAnnContext() |
| 81 | : mtx(MutexTypeAnnotations, StatMtxAnnotations) { |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | static DynamicAnnContext *dyn_ann_ctx; |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 86 | static char dyn_ann_ctx_placeholder[sizeof(DynamicAnnContext)] ALIGNED(64); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 87 | |
| 88 | static void AddExpectRace(ExpectRace *list, |
| 89 | char *f, int l, uptr addr, uptr size, char *desc) { |
| 90 | ExpectRace *race = list->next; |
| 91 | for (; race != list; race = race->next) { |
Dmitry Vyukov | 0fd908c | 2013-03-28 16:21:19 +0000 | [diff] [blame] | 92 | if (race->addr == addr && race->size == size) { |
| 93 | race->addcount++; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 94 | return; |
Dmitry Vyukov | 0fd908c | 2013-03-28 16:21:19 +0000 | [diff] [blame] | 95 | } |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 96 | } |
| 97 | race = (ExpectRace*)internal_alloc(MBlockExpectRace, sizeof(ExpectRace)); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 98 | race->addr = addr; |
| 99 | race->size = size; |
| 100 | race->file = f; |
| 101 | race->line = l; |
| 102 | race->desc[0] = 0; |
Dmitry Vyukov | 0fd908c | 2013-03-28 16:21:19 +0000 | [diff] [blame] | 103 | race->hitcount = 0; |
| 104 | race->addcount = 1; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 105 | if (desc) { |
| 106 | int i = 0; |
| 107 | for (; i < kMaxDescLen - 1 && desc[i]; i++) |
| 108 | race->desc[i] = desc[i]; |
| 109 | race->desc[i] = 0; |
| 110 | } |
| 111 | race->prev = list; |
| 112 | race->next = list->next; |
| 113 | race->next->prev = race; |
| 114 | list->next = race; |
| 115 | } |
| 116 | |
| 117 | static ExpectRace *FindRace(ExpectRace *list, uptr addr, uptr size) { |
| 118 | for (ExpectRace *race = list->next; race != list; race = race->next) { |
| 119 | uptr maxbegin = max(race->addr, addr); |
| 120 | uptr minend = min(race->addr + race->size, addr + size); |
| 121 | if (maxbegin < minend) |
| 122 | return race; |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static bool CheckContains(ExpectRace *list, uptr addr, uptr size) { |
| 128 | ExpectRace *race = FindRace(list, addr, size); |
| 129 | if (race == 0) |
| 130 | return false; |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 131 | DPrintf("Hit expected/benign race: %s addr=%zx:%d %s:%d\n", |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 132 | race->desc, race->addr, (int)race->size, race->file, race->line); |
| 133 | race->hitcount++; |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | static void InitList(ExpectRace *list) { |
| 138 | list->next = list; |
| 139 | list->prev = list; |
| 140 | } |
| 141 | |
| 142 | void InitializeDynamicAnnotations() { |
| 143 | dyn_ann_ctx = new(dyn_ann_ctx_placeholder) DynamicAnnContext; |
| 144 | InitList(&dyn_ann_ctx->expect); |
| 145 | InitList(&dyn_ann_ctx->benign); |
| 146 | } |
| 147 | |
| 148 | bool IsExpectedReport(uptr addr, uptr size) { |
| 149 | Lock lock(&dyn_ann_ctx->mtx); |
| 150 | if (CheckContains(&dyn_ann_ctx->expect, addr, size)) |
| 151 | return true; |
| 152 | if (CheckContains(&dyn_ann_ctx->benign, addr, size)) |
| 153 | return true; |
| 154 | return false; |
| 155 | } |
| 156 | |
Dmitry Vyukov | 0fd908c | 2013-03-28 16:21:19 +0000 | [diff] [blame] | 157 | static void CollectMatchedBenignRaces(Vector<ExpectRace> *matched, |
| 158 | int *unique_count, int *hit_count, int ExpectRace::*counter) { |
| 159 | ExpectRace *list = &dyn_ann_ctx->benign; |
| 160 | for (ExpectRace *race = list->next; race != list; race = race->next) { |
| 161 | (*unique_count)++; |
| 162 | if (race->*counter == 0) |
| 163 | continue; |
| 164 | (*hit_count) += race->*counter; |
| 165 | uptr i = 0; |
| 166 | for (; i < matched->Size(); i++) { |
| 167 | ExpectRace *race0 = &(*matched)[i]; |
| 168 | if (race->line == race0->line |
| 169 | && internal_strcmp(race->file, race0->file) == 0 |
| 170 | && internal_strcmp(race->desc, race0->desc) == 0) { |
| 171 | race0->*counter += race->*counter; |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | if (i == matched->Size()) |
| 176 | matched->PushBack(*race); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void PrintMatchedBenignRaces() { |
| 181 | Lock lock(&dyn_ann_ctx->mtx); |
| 182 | int unique_count = 0; |
| 183 | int hit_count = 0; |
| 184 | int add_count = 0; |
| 185 | Vector<ExpectRace> hit_matched(MBlockScopedBuf); |
| 186 | CollectMatchedBenignRaces(&hit_matched, &unique_count, &hit_count, |
| 187 | &ExpectRace::hitcount); |
| 188 | Vector<ExpectRace> add_matched(MBlockScopedBuf); |
| 189 | CollectMatchedBenignRaces(&add_matched, &unique_count, &add_count, |
| 190 | &ExpectRace::addcount); |
| 191 | if (hit_matched.Size()) { |
| 192 | Printf("ThreadSanitizer: Matched %d \"benign\" races (pid=%d):\n", |
Peter Collingbourne | 0b694fc | 2013-05-17 16:56:53 +0000 | [diff] [blame] | 193 | hit_count, (int)internal_getpid()); |
Dmitry Vyukov | 0fd908c | 2013-03-28 16:21:19 +0000 | [diff] [blame] | 194 | for (uptr i = 0; i < hit_matched.Size(); i++) { |
| 195 | Printf("%d %s:%d %s\n", |
| 196 | hit_matched[i].hitcount, hit_matched[i].file, |
| 197 | hit_matched[i].line, hit_matched[i].desc); |
| 198 | } |
| 199 | } |
| 200 | if (hit_matched.Size()) { |
| 201 | Printf("ThreadSanitizer: Annotated %d \"benign\" races, %d unique" |
| 202 | " (pid=%d):\n", |
Peter Collingbourne | 0b694fc | 2013-05-17 16:56:53 +0000 | [diff] [blame] | 203 | add_count, unique_count, (int)internal_getpid()); |
Dmitry Vyukov | 0fd908c | 2013-03-28 16:21:19 +0000 | [diff] [blame] | 204 | for (uptr i = 0; i < add_matched.Size(); i++) { |
| 205 | Printf("%d %s:%d %s\n", |
| 206 | add_matched[i].addcount, add_matched[i].file, |
| 207 | add_matched[i].line, add_matched[i].desc); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | static void ReportMissedExpectedRace(ExpectRace *race) { |
| 213 | Printf("==================\n"); |
| 214 | Printf("WARNING: ThreadSanitizer: missed expected data race\n"); |
| 215 | Printf(" %s addr=%zx %s:%d\n", |
| 216 | race->desc, race->addr, race->file, race->line); |
| 217 | Printf("==================\n"); |
| 218 | } |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 219 | } // namespace __tsan |
| 220 | |
| 221 | using namespace __tsan; // NOLINT |
| 222 | |
| 223 | extern "C" { |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 224 | void INTERFACE_ATTRIBUTE AnnotateHappensBefore(char *f, int l, uptr addr) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 225 | SCOPED_ANNOTATION(AnnotateHappensBefore); |
Dmitry Vyukov | 9c1e4f4 | 2013-09-19 04:42:25 +0000 | [diff] [blame] | 226 | Release(thr, pc, addr); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 229 | void INTERFACE_ATTRIBUTE AnnotateHappensAfter(char *f, int l, uptr addr) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 230 | SCOPED_ANNOTATION(AnnotateHappensAfter); |
Dmitry Vyukov | 9c1e4f4 | 2013-09-19 04:42:25 +0000 | [diff] [blame] | 231 | Acquire(thr, pc, addr); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 234 | void INTERFACE_ATTRIBUTE AnnotateCondVarSignal(char *f, int l, uptr cv) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 235 | SCOPED_ANNOTATION(AnnotateCondVarSignal); |
| 236 | } |
| 237 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 238 | void INTERFACE_ATTRIBUTE AnnotateCondVarSignalAll(char *f, int l, uptr cv) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 239 | SCOPED_ANNOTATION(AnnotateCondVarSignalAll); |
| 240 | } |
| 241 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 242 | void INTERFACE_ATTRIBUTE AnnotateMutexIsNotPHB(char *f, int l, uptr mu) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 243 | SCOPED_ANNOTATION(AnnotateMutexIsNotPHB); |
| 244 | } |
| 245 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 246 | void INTERFACE_ATTRIBUTE AnnotateCondVarWait(char *f, int l, uptr cv, |
| 247 | uptr lock) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 248 | SCOPED_ANNOTATION(AnnotateCondVarWait); |
| 249 | } |
| 250 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 251 | void INTERFACE_ATTRIBUTE AnnotateRWLockCreate(char *f, int l, uptr m) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 252 | SCOPED_ANNOTATION(AnnotateRWLockCreate); |
Dmitry Vyukov | c20e9ba | 2012-08-16 13:29:41 +0000 | [diff] [blame] | 253 | MutexCreate(thr, pc, m, true, true, false); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 256 | void INTERFACE_ATTRIBUTE AnnotateRWLockCreateStatic(char *f, int l, uptr m) { |
Dmitry Vyukov | c20e9ba | 2012-08-16 13:29:41 +0000 | [diff] [blame] | 257 | SCOPED_ANNOTATION(AnnotateRWLockCreateStatic); |
| 258 | MutexCreate(thr, pc, m, true, true, true); |
| 259 | } |
| 260 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 261 | void INTERFACE_ATTRIBUTE AnnotateRWLockDestroy(char *f, int l, uptr m) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 262 | SCOPED_ANNOTATION(AnnotateRWLockDestroy); |
Dmitry Vyukov | c20e9ba | 2012-08-16 13:29:41 +0000 | [diff] [blame] | 263 | MutexDestroy(thr, pc, m); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 266 | void INTERFACE_ATTRIBUTE AnnotateRWLockAcquired(char *f, int l, uptr m, |
| 267 | uptr is_w) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 268 | SCOPED_ANNOTATION(AnnotateRWLockAcquired); |
Dmitry Vyukov | c20e9ba | 2012-08-16 13:29:41 +0000 | [diff] [blame] | 269 | if (is_w) |
| 270 | MutexLock(thr, pc, m); |
| 271 | else |
| 272 | MutexReadLock(thr, pc, m); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 275 | void INTERFACE_ATTRIBUTE AnnotateRWLockReleased(char *f, int l, uptr m, |
| 276 | uptr is_w) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 277 | SCOPED_ANNOTATION(AnnotateRWLockReleased); |
Dmitry Vyukov | c20e9ba | 2012-08-16 13:29:41 +0000 | [diff] [blame] | 278 | if (is_w) |
| 279 | MutexUnlock(thr, pc, m); |
| 280 | else |
| 281 | MutexReadUnlock(thr, pc, m); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 284 | void INTERFACE_ATTRIBUTE AnnotateTraceMemory(char *f, int l, uptr mem) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 285 | SCOPED_ANNOTATION(AnnotateTraceMemory); |
| 286 | } |
| 287 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 288 | void INTERFACE_ATTRIBUTE AnnotateFlushState(char *f, int l) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 289 | SCOPED_ANNOTATION(AnnotateFlushState); |
| 290 | } |
| 291 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 292 | void INTERFACE_ATTRIBUTE AnnotateNewMemory(char *f, int l, uptr mem, |
| 293 | uptr size) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 294 | SCOPED_ANNOTATION(AnnotateNewMemory); |
| 295 | } |
| 296 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 297 | void INTERFACE_ATTRIBUTE AnnotateNoOp(char *f, int l, uptr mem) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 298 | SCOPED_ANNOTATION(AnnotateNoOp); |
| 299 | } |
| 300 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 301 | void INTERFACE_ATTRIBUTE AnnotateFlushExpectedRaces(char *f, int l) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 302 | SCOPED_ANNOTATION(AnnotateFlushExpectedRaces); |
| 303 | Lock lock(&dyn_ann_ctx->mtx); |
| 304 | while (dyn_ann_ctx->expect.next != &dyn_ann_ctx->expect) { |
| 305 | ExpectRace *race = dyn_ann_ctx->expect.next; |
| 306 | if (race->hitcount == 0) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 307 | ctx->nmissed_expected++; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 308 | ReportMissedExpectedRace(race); |
| 309 | } |
| 310 | race->prev->next = race->next; |
| 311 | race->next->prev = race->prev; |
| 312 | internal_free(race); |
| 313 | } |
| 314 | } |
| 315 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 316 | void INTERFACE_ATTRIBUTE AnnotateEnableRaceDetection( |
| 317 | char *f, int l, int enable) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 318 | SCOPED_ANNOTATION(AnnotateEnableRaceDetection); |
| 319 | // FIXME: Reconsider this functionality later. It may be irrelevant. |
| 320 | } |
| 321 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 322 | void INTERFACE_ATTRIBUTE AnnotateMutexIsUsedAsCondVar( |
| 323 | char *f, int l, uptr mu) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 324 | SCOPED_ANNOTATION(AnnotateMutexIsUsedAsCondVar); |
| 325 | } |
| 326 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 327 | void INTERFACE_ATTRIBUTE AnnotatePCQGet( |
| 328 | char *f, int l, uptr pcq) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 329 | SCOPED_ANNOTATION(AnnotatePCQGet); |
| 330 | } |
| 331 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 332 | void INTERFACE_ATTRIBUTE AnnotatePCQPut( |
| 333 | char *f, int l, uptr pcq) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 334 | SCOPED_ANNOTATION(AnnotatePCQPut); |
| 335 | } |
| 336 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 337 | void INTERFACE_ATTRIBUTE AnnotatePCQDestroy( |
| 338 | char *f, int l, uptr pcq) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 339 | SCOPED_ANNOTATION(AnnotatePCQDestroy); |
| 340 | } |
| 341 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 342 | void INTERFACE_ATTRIBUTE AnnotatePCQCreate( |
| 343 | char *f, int l, uptr pcq) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 344 | SCOPED_ANNOTATION(AnnotatePCQCreate); |
| 345 | } |
| 346 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 347 | void INTERFACE_ATTRIBUTE AnnotateExpectRace( |
| 348 | char *f, int l, uptr mem, char *desc) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 349 | SCOPED_ANNOTATION(AnnotateExpectRace); |
| 350 | Lock lock(&dyn_ann_ctx->mtx); |
| 351 | AddExpectRace(&dyn_ann_ctx->expect, |
| 352 | f, l, mem, 1, desc); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 353 | DPrintf("Add expected race: %s addr=%zx %s:%d\n", desc, mem, f, l); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 356 | static void BenignRaceImpl( |
| 357 | char *f, int l, uptr mem, uptr size, char *desc) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 358 | Lock lock(&dyn_ann_ctx->mtx); |
| 359 | AddExpectRace(&dyn_ann_ctx->benign, |
| 360 | f, l, mem, size, desc); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 361 | DPrintf("Add benign race: %s addr=%zx %s:%d\n", desc, mem, f, l); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // FIXME: Turn it off later. WTF is benign race?1?? Go talk to Hans Boehm. |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 365 | void INTERFACE_ATTRIBUTE AnnotateBenignRaceSized( |
| 366 | char *f, int l, uptr mem, uptr size, char *desc) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 367 | SCOPED_ANNOTATION(AnnotateBenignRaceSized); |
| 368 | BenignRaceImpl(f, l, mem, size, desc); |
| 369 | } |
| 370 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 371 | void INTERFACE_ATTRIBUTE AnnotateBenignRace( |
| 372 | char *f, int l, uptr mem, char *desc) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 373 | SCOPED_ANNOTATION(AnnotateBenignRace); |
| 374 | BenignRaceImpl(f, l, mem, 1, desc); |
| 375 | } |
| 376 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 377 | void INTERFACE_ATTRIBUTE AnnotateIgnoreReadsBegin(char *f, int l) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 378 | SCOPED_ANNOTATION(AnnotateIgnoreReadsBegin); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 379 | ThreadIgnoreBegin(thr, pc); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 382 | void INTERFACE_ATTRIBUTE AnnotateIgnoreReadsEnd(char *f, int l) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 383 | SCOPED_ANNOTATION(AnnotateIgnoreReadsEnd); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 384 | ThreadIgnoreEnd(thr, pc); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 387 | void INTERFACE_ATTRIBUTE AnnotateIgnoreWritesBegin(char *f, int l) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 388 | SCOPED_ANNOTATION(AnnotateIgnoreWritesBegin); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 389 | ThreadIgnoreBegin(thr, pc); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 392 | void INTERFACE_ATTRIBUTE AnnotateIgnoreWritesEnd(char *f, int l) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 393 | SCOPED_ANNOTATION(AnnotateIgnoreWritesEnd); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 394 | ThreadIgnoreEnd(thr, pc); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Dmitry Vyukov | e1ddbf9 | 2013-10-10 15:58:12 +0000 | [diff] [blame] | 397 | void INTERFACE_ATTRIBUTE AnnotateIgnoreSyncBegin(char *f, int l) { |
| 398 | SCOPED_ANNOTATION(AnnotateIgnoreSyncBegin); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 399 | ThreadIgnoreSyncBegin(thr, pc); |
Dmitry Vyukov | e1ddbf9 | 2013-10-10 15:58:12 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | void INTERFACE_ATTRIBUTE AnnotateIgnoreSyncEnd(char *f, int l) { |
| 403 | SCOPED_ANNOTATION(AnnotateIgnoreSyncEnd); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 404 | ThreadIgnoreSyncEnd(thr, pc); |
Dmitry Vyukov | e1ddbf9 | 2013-10-10 15:58:12 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 407 | void INTERFACE_ATTRIBUTE AnnotatePublishMemoryRange( |
| 408 | char *f, int l, uptr addr, uptr size) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 409 | SCOPED_ANNOTATION(AnnotatePublishMemoryRange); |
| 410 | } |
| 411 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 412 | void INTERFACE_ATTRIBUTE AnnotateUnpublishMemoryRange( |
| 413 | char *f, int l, uptr addr, uptr size) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 414 | SCOPED_ANNOTATION(AnnotateUnpublishMemoryRange); |
| 415 | } |
| 416 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 417 | void INTERFACE_ATTRIBUTE AnnotateThreadName( |
| 418 | char *f, int l, char *name) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 419 | SCOPED_ANNOTATION(AnnotateThreadName); |
Dmitry Vyukov | aecf2e5 | 2012-12-04 15:46:05 +0000 | [diff] [blame] | 420 | ThreadSetName(thr, name); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Alexander Potapenko | 743d89d | 2013-04-02 11:21:53 +0000 | [diff] [blame] | 423 | // We deliberately omit the implementation of WTFAnnotateHappensBefore() and |
| 424 | // WTFAnnotateHappensAfter(). Those are being used by Webkit to annotate |
| 425 | // atomic operations, which should be handled by ThreadSanitizer correctly. |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 426 | void INTERFACE_ATTRIBUTE WTFAnnotateHappensBefore(char *f, int l, uptr addr) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 427 | SCOPED_ANNOTATION(AnnotateHappensBefore); |
| 428 | } |
| 429 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 430 | void INTERFACE_ATTRIBUTE WTFAnnotateHappensAfter(char *f, int l, uptr addr) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 431 | SCOPED_ANNOTATION(AnnotateHappensAfter); |
| 432 | } |
| 433 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 434 | void INTERFACE_ATTRIBUTE WTFAnnotateBenignRaceSized( |
| 435 | char *f, int l, uptr mem, uptr sz, char *desc) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 436 | SCOPED_ANNOTATION(AnnotateBenignRaceSized); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 437 | BenignRaceImpl(f, l, mem, sz, desc); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 440 | int INTERFACE_ATTRIBUTE RunningOnValgrind() { |
Dmitry Vyukov | 24567d4 | 2012-05-24 09:24:45 +0000 | [diff] [blame] | 441 | return flags()->running_on_valgrind; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 444 | double __attribute__((weak)) INTERFACE_ATTRIBUTE ValgrindSlowdown(void) { |
Dmitry Vyukov | dc2f032 | 2012-05-17 08:04:41 +0000 | [diff] [blame] | 445 | return 10.0; |
| 446 | } |
| 447 | |
Dmitry Vyukov | 258c24b | 2012-12-04 14:01:21 +0000 | [diff] [blame] | 448 | const char INTERFACE_ATTRIBUTE* ThreadSanitizerQuery(const char *query) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 449 | if (internal_strcmp(query, "pure_happens_before") == 0) |
| 450 | return "1"; |
| 451 | else |
| 452 | return "0"; |
| 453 | } |
Evgeniy Stepanov | 2ec7318 | 2013-09-18 11:20:31 +0000 | [diff] [blame] | 454 | |
| 455 | void INTERFACE_ATTRIBUTE |
| 456 | AnnotateMemoryIsInitialized(char *f, int l, uptr mem, uptr sz) {} |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 457 | void INTERFACE_ATTRIBUTE |
| 458 | AnnotateMemoryIsUninitialized(char *f, int l, uptr mem, uptr sz) {} |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 459 | } // extern "C" |