1 //===-- tsan_interceptors.cc ----------------------------------------------===// 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 // FIXME: move as many interceptors as possible into 13 // sanitizer_common/sanitizer_common_interceptors.h 14 //===----------------------------------------------------------------------===// 15 16 #include "sanitizer_common/sanitizer_atomic.h" 17 #include "sanitizer_common/sanitizer_libc.h" 18 #include "sanitizer_common/sanitizer_platform_limits_posix.h" 19 #include "sanitizer_common/sanitizer_placement_new.h" 20 #include "sanitizer_common/sanitizer_stacktrace.h" 21 #include "interception/interception.h" 22 #include "tsan_interface.h" 23 #include "tsan_platform.h" 24 #include "tsan_rtl.h" 25 #include "tsan_mman.h" 26 #include "tsan_fd.h" 27 28 using namespace __tsan; // NOLINT 29 30 const int kSigCount = 128; 31 32 struct my_siginfo_t { 33 int opaque[128]; 34 }; 35 36 struct sigset_t { 37 u64 val[1024 / 8 / sizeof(u64)]; 38 }; 39 40 struct ucontext_t { 41 uptr opaque[117]; 42 }; 43 44 extern "C" int pthread_attr_init(void *attr); 45 extern "C" int pthread_attr_destroy(void *attr); 46 extern "C" int pthread_attr_getdetachstate(void *attr, int *v); 47 extern "C" int pthread_attr_setstacksize(void *attr, uptr stacksize); 48 extern "C" int pthread_attr_getstacksize(void *attr, uptr *stacksize); 49 extern "C" int pthread_key_create(unsigned *key, void (*destructor)(void* v)); 50 extern "C" int pthread_setspecific(unsigned key, const void *v); 51 extern "C" int pthread_mutexattr_gettype(void *a, int *type); 52 extern "C" int pthread_yield(); 53 extern "C" int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset); 54 extern "C" int sigfillset(sigset_t *set); 55 extern "C" void *pthread_self(); 56 extern "C" void _exit(int status); 57 extern "C" int *__errno_location(); 58 extern "C" int fileno_unlocked(void *stream); 59 extern "C" void *__libc_malloc(uptr size); 60 extern "C" void *__libc_calloc(uptr size, uptr n); 61 extern "C" void *__libc_realloc(void *ptr, uptr size); 62 extern "C" void __libc_free(void *ptr); 63 const int PTHREAD_MUTEX_RECURSIVE = 1; 64 const int PTHREAD_MUTEX_RECURSIVE_NP = 1; 65 const int kPthreadAttrSize = 56; 66 const int EINVAL = 22; 67 const int EBUSY = 16; 68 const int EPOLL_CTL_ADD = 1; 69 const int SIGILL = 4; 70 const int SIGABRT = 6; 71 const int SIGFPE = 8; 72 const int SIGSEGV = 11; 73 const int SIGPIPE = 13; 74 const int SIGBUS = 7; 75 void *const MAP_FAILED = (void*)-1; 76 const int PTHREAD_BARRIER_SERIAL_THREAD = -1; 77 const int MAP_FIXED = 0x10; 78 typedef long long_t; // NOLINT 79 80 // From /usr/include/unistd.h 81 # define F_ULOCK 0 /* Unlock a previously locked region. */ 82 # define F_LOCK 1 /* Lock a region for exclusive use. */ 83 # define F_TLOCK 2 /* Test and lock a region for exclusive use. */ 84 # define F_TEST 3 /* Test a region for other processes locks. */ 85 86 typedef void (*sighandler_t)(int sig); 87 88 #define errno (*__errno_location()) 89 90 struct sigaction_t { 91 union { 92 sighandler_t sa_handler; 93 void (*sa_sigaction)(int sig, my_siginfo_t *siginfo, void *uctx); 94 }; 95 sigset_t sa_mask; 96 int sa_flags; 97 void (*sa_restorer)(); 98 }; 99 100 const sighandler_t SIG_DFL = (sighandler_t)0; 101 const sighandler_t SIG_IGN = (sighandler_t)1; 102 const sighandler_t SIG_ERR = (sighandler_t)-1; 103 const int SA_SIGINFO = 4; 104 const int SIG_SETMASK = 2; 105 106 namespace std { 107 struct nothrow_t {}; 108 } // namespace std 109 110 static sigaction_t sigactions[kSigCount]; 111 112 namespace __tsan { 113 struct SignalDesc { 114 bool armed; 115 bool sigaction; 116 my_siginfo_t siginfo; 117 ucontext_t ctx; 118 }; 119 120 struct SignalContext { 121 int in_blocking_func; 122 int int_signal_send; 123 int pending_signal_count; 124 SignalDesc pending_signals[kSigCount]; 125 }; 126 } // namespace __tsan 127 128 static SignalContext *SigCtx(ThreadState *thr) { 129 SignalContext *ctx = (SignalContext*)thr->signal_ctx; 130 if (ctx == 0 && thr->is_alive) { 131 ScopedInRtl in_rtl; 132 ctx = (SignalContext*)MmapOrDie(sizeof(*ctx), "SignalContext"); 133 MemoryResetRange(thr, (uptr)&SigCtx, (uptr)ctx, sizeof(*ctx)); 134 thr->signal_ctx = ctx; 135 } 136 return ctx; 137 } 138 139 static unsigned g_thread_finalize_key; 140 141 class ScopedInterceptor { 142 public: 143 ScopedInterceptor(ThreadState *thr, const char *fname, uptr pc); 144 ~ScopedInterceptor(); 145 private: 146 ThreadState *const thr_; 147 const int in_rtl_; 148 }; 149 150 ScopedInterceptor::ScopedInterceptor(ThreadState *thr, const char *fname, 151 uptr pc) 152 : thr_(thr) 153 , in_rtl_(thr->in_rtl) { 154 if (thr_->in_rtl == 0) { 155 Initialize(thr); 156 FuncEntry(thr, pc); 157 thr_->in_rtl++; 158 DPrintf("#%d: intercept %s()\n", thr_->tid, fname); 159 } else { 160 thr_->in_rtl++; 161 } 162 } 163 164 ScopedInterceptor::~ScopedInterceptor() { 165 thr_->in_rtl--; 166 if (thr_->in_rtl == 0) { 167 FuncExit(thr_); 168 ProcessPendingSignals(thr_); 169 } 170 CHECK_EQ(in_rtl_, thr_->in_rtl); 171 } 172 173 #define SCOPED_INTERCEPTOR_RAW(func, ...) \ 174 ThreadState *thr = cur_thread(); \ 175 StatInc(thr, StatInterceptor); \ 176 StatInc(thr, StatInt_##func); \ 177 const uptr caller_pc = GET_CALLER_PC(); \ 178 ScopedInterceptor si(thr, #func, caller_pc); \ 179 const uptr pc = __sanitizer::StackTrace::GetPreviousInstructionPc( \ 180 __sanitizer::StackTrace::GetCurrentPc()); \ 181 (void)pc; \ 182 /**/ 183 184 #define SCOPED_TSAN_INTERCEPTOR(func, ...) \ 185 SCOPED_INTERCEPTOR_RAW(func, __VA_ARGS__); \ 186 if (REAL(func) == 0) { \ 187 Printf("FATAL: ThreadSanitizer: failed to intercept %s\n", #func); \ 188 Die(); \ 189 } \ 190 if (thr->in_rtl > 1) \ 191 return REAL(func)(__VA_ARGS__); \ 192 /**/ 193 194 #define TSAN_INTERCEPTOR(ret, func, ...) INTERCEPTOR(ret, func, __VA_ARGS__) 195 #define TSAN_INTERCEPT(func) INTERCEPT_FUNCTION(func) 196 197 #define BLOCK_REAL(name) (BlockingCall(thr), REAL(name)) 198 199 struct BlockingCall { 200 explicit BlockingCall(ThreadState *thr) 201 : ctx(SigCtx(thr)) { 202 ctx->in_blocking_func++; 203 } 204 205 ~BlockingCall() { 206 ctx->in_blocking_func--; 207 } 208 209 SignalContext *ctx; 210 }; 211 212 TSAN_INTERCEPTOR(unsigned, sleep, unsigned sec) { 213 SCOPED_TSAN_INTERCEPTOR(sleep, sec); 214 unsigned res = BLOCK_REAL(sleep)(sec); 215 AfterSleep(thr, pc); 216 return res; 217 } 218 219 TSAN_INTERCEPTOR(int, usleep, long_t usec) { 220 SCOPED_TSAN_INTERCEPTOR(usleep, usec); 221 int res = BLOCK_REAL(usleep)(usec); 222 AfterSleep(thr, pc); 223 return res; 224 } 225 226 TSAN_INTERCEPTOR(int, nanosleep, void *req, void *rem) { 227 SCOPED_TSAN_INTERCEPTOR(nanosleep, req, rem); 228 int res = BLOCK_REAL(nanosleep)(req, rem); 229 AfterSleep(thr, pc); 230 return res; 231 } 232 233 class AtExitContext { 234 public: 235 AtExitContext() 236 : mtx_(MutexTypeAtExit, StatMtxAtExit) 237 , pos_() { 238 } 239 240 typedef void(*atexit_t)(); 241 242 int atexit(ThreadState *thr, uptr pc, bool is_on_exit, 243 atexit_t f, void *arg) { 244 Lock l(&mtx_); 245 if (pos_ == kMaxAtExit) 246 return 1; 247 Release(thr, pc, (uptr)this); 248 stack_[pos_] = f; 249 args_[pos_] = arg; 250 is_on_exits_[pos_] = is_on_exit; 251 pos_++; 252 return 0; 253 } 254 255 void exit(ThreadState *thr, uptr pc) { 256 CHECK_EQ(thr->in_rtl, 0); 257 for (;;) { 258 atexit_t f = 0; 259 void *arg = 0; 260 bool is_on_exit = false; 261 { 262 Lock l(&mtx_); 263 if (pos_) { 264 pos_--; 265 f = stack_[pos_]; 266 arg = args_[pos_]; 267 is_on_exit = is_on_exits_[pos_]; 268 ScopedInRtl in_rtl; 269 Acquire(thr, pc, (uptr)this); 270 } 271 } 272 if (f == 0) 273 break; 274 DPrintf("#%d: executing atexit func %p\n", thr->tid, f); 275 CHECK_EQ(thr->in_rtl, 0); 276 if (is_on_exit) 277 ((void(*)(int status, void *arg))f)(0, arg); 278 else 279 ((void(*)(void *arg, void *dso))f)(arg, 0); 280 } 281 } 282 283 private: 284 static const int kMaxAtExit = 128; 285 Mutex mtx_; 286 atexit_t stack_[kMaxAtExit]; 287 void *args_[kMaxAtExit]; 288 bool is_on_exits_[kMaxAtExit]; 289 int pos_; 290 }; 291 292 static AtExitContext *atexit_ctx; 293 294 static void finalize(void *arg) { 295 ThreadState * thr = cur_thread(); 296 uptr pc = 0; 297 atexit_ctx->exit(thr, pc); 298 int status = Finalize(cur_thread()); 299 if (status) 300 _exit(status); 301 } 302 303 TSAN_INTERCEPTOR(int, atexit, void (*f)()) { 304 if (cur_thread()->in_symbolizer) 305 return 0; 306 SCOPED_TSAN_INTERCEPTOR(atexit, f); 307 return atexit_ctx->atexit(thr, pc, false, (void(*)())f, 0); 308 } 309 310 TSAN_INTERCEPTOR(int, on_exit, void(*f)(int, void*), void *arg) { 311 if (cur_thread()->in_symbolizer) 312 return 0; 313 SCOPED_TSAN_INTERCEPTOR(on_exit, f, arg); 314 return atexit_ctx->atexit(thr, pc, true, (void(*)())f, arg); 315 } 316 317 TSAN_INTERCEPTOR(int, __cxa_atexit, void (*f)(void *a), void *arg, void *dso) { 318 if (cur_thread()->in_symbolizer) 319 return 0; 320 SCOPED_TSAN_INTERCEPTOR(__cxa_atexit, f, arg, dso); 321 if (dso) 322 return REAL(__cxa_atexit)(f, arg, dso); 323 return atexit_ctx->atexit(thr, pc, false, (void(*)())f, arg); 324 } 325 326 TSAN_INTERCEPTOR(void, longjmp, void *env, int val) { 327 SCOPED_TSAN_INTERCEPTOR(longjmp, env, val); 328 Printf("ThreadSanitizer: longjmp() is not supported\n"); 329 Die(); 330 } 331 332 TSAN_INTERCEPTOR(void, siglongjmp, void *env, int val) { 333 SCOPED_TSAN_INTERCEPTOR(siglongjmp, env, val); 334 Printf("ThreadSanitizer: siglongjmp() is not supported\n"); 335 Die(); 336 } 337 338 TSAN_INTERCEPTOR(void*, malloc, uptr size) { 339 if (cur_thread()->in_symbolizer) 340 return __libc_malloc(size); 341 void *p = 0; 342 { 343 SCOPED_INTERCEPTOR_RAW(malloc, size); 344 p = user_alloc(thr, pc, size); 345 } 346 invoke_malloc_hook(p, size); 347 return p; 348 } 349 350 TSAN_INTERCEPTOR(void*, __libc_memalign, uptr align, uptr sz) { 351 SCOPED_TSAN_INTERCEPTOR(__libc_memalign, align, sz); 352 return user_alloc(thr, pc, sz, align); 353 } 354 355 TSAN_INTERCEPTOR(void*, calloc, uptr size, uptr n) { 356 if (cur_thread()->in_symbolizer) 357 return __libc_calloc(size, n); 358 if (__sanitizer::CallocShouldReturnNullDueToOverflow(size, n)) return 0; 359 void *p = 0; 360 { 361 SCOPED_INTERCEPTOR_RAW(calloc, size, n); 362 p = user_alloc(thr, pc, n * size); 363 if (p) internal_memset(p, 0, n * size); 364 } 365 invoke_malloc_hook(p, n * size); 366 return p; 367 } 368 369 TSAN_INTERCEPTOR(void*, realloc, void *p, uptr size) { 370 if (cur_thread()->in_symbolizer) 371 return __libc_realloc(p, size); 372 if (p) 373 invoke_free_hook(p); 374 { 375 SCOPED_INTERCEPTOR_RAW(realloc, p, size); 376 p = user_realloc(thr, pc, p, size); 377 } 378 invoke_malloc_hook(p, size); 379 return p; 380 } 381 382 TSAN_INTERCEPTOR(void, free, void *p) { 383 if (p == 0) 384 return; 385 if (cur_thread()->in_symbolizer) 386 return __libc_free(p); 387 invoke_free_hook(p); 388 SCOPED_INTERCEPTOR_RAW(free, p); 389 user_free(thr, pc, p); 390 } 391 392 TSAN_INTERCEPTOR(void, cfree, void *p) { 393 if (p == 0) 394 return; 395 if (cur_thread()->in_symbolizer) 396 return __libc_free(p); 397 invoke_free_hook(p); 398 SCOPED_INTERCEPTOR_RAW(cfree, p); 399 user_free(thr, pc, p); 400 } 401 402 TSAN_INTERCEPTOR(uptr, malloc_usable_size, void *p) { 403 SCOPED_INTERCEPTOR_RAW(malloc_usable_size, p); 404 return user_alloc_usable_size(thr, pc, p); 405 } 406 407 #define OPERATOR_NEW_BODY(mangled_name) \ 408 if (cur_thread()->in_symbolizer) \ 409 return __libc_malloc(size); \ 410 void *p = 0; \ 411 { \ 412 SCOPED_INTERCEPTOR_RAW(mangled_name, size); \ 413 p = user_alloc(thr, pc, size); \ 414 } \ 415 invoke_malloc_hook(p, size); \ 416 return p; 417 418 void *operator new(__sanitizer::uptr size) { 419 OPERATOR_NEW_BODY(_Znwm); 420 } 421 void *operator new[](__sanitizer::uptr size) { 422 OPERATOR_NEW_BODY(_Znam); 423 } 424 void *operator new(__sanitizer::uptr size, std::nothrow_t const&) { 425 OPERATOR_NEW_BODY(_ZnwmRKSt9nothrow_t); 426 } 427 void *operator new[](__sanitizer::uptr size, std::nothrow_t const&) { 428 OPERATOR_NEW_BODY(_ZnamRKSt9nothrow_t); 429 } 430 431 #define OPERATOR_DELETE_BODY(mangled_name) \ 432 if (ptr == 0) return; \ 433 if (cur_thread()->in_symbolizer) \ 434 return __libc_free(ptr); \ 435 invoke_free_hook(ptr); \ 436 SCOPED_INTERCEPTOR_RAW(mangled_name, ptr); \ 437 user_free(thr, pc, ptr); 438 439 void operator delete(void *ptr) { 440 OPERATOR_DELETE_BODY(_ZdlPv); 441 } 442 void operator delete[](void *ptr) { 443 OPERATOR_DELETE_BODY(_ZdlPvRKSt9nothrow_t); 444 } 445 void operator delete(void *ptr, std::nothrow_t const&) { 446 OPERATOR_DELETE_BODY(_ZdaPv); 447 } 448 void operator delete[](void *ptr, std::nothrow_t const&) { 449 OPERATOR_DELETE_BODY(_ZdaPvRKSt9nothrow_t); 450 } 451 452 TSAN_INTERCEPTOR(uptr, strlen, const char *s) { 453 SCOPED_TSAN_INTERCEPTOR(strlen, s); 454 uptr len = internal_strlen(s); 455 MemoryAccessRange(thr, pc, (uptr)s, len + 1, false); 456 return len; 457 } 458 459 TSAN_INTERCEPTOR(void*, memset, void *dst, int v, uptr size) { 460 SCOPED_TSAN_INTERCEPTOR(memset, dst, v, size); 461 MemoryAccessRange(thr, pc, (uptr)dst, size, true); 462 return internal_memset(dst, v, size); 463 } 464 465 TSAN_INTERCEPTOR(void*, memcpy, void *dst, const void *src, uptr size) { 466 SCOPED_TSAN_INTERCEPTOR(memcpy, dst, src, size); 467 MemoryAccessRange(thr, pc, (uptr)dst, size, true); 468 MemoryAccessRange(thr, pc, (uptr)src, size, false); 469 return internal_memcpy(dst, src, size); 470 } 471 472 TSAN_INTERCEPTOR(int, memcmp, const void *s1, const void *s2, uptr n) { 473 SCOPED_TSAN_INTERCEPTOR(memcmp, s1, s2, n); 474 int res = 0; 475 uptr len = 0; 476 for (; len < n; len++) { 477 if ((res = ((unsigned char*)s1)[len] - ((unsigned char*)s2)[len])) 478 break; 479 } 480 MemoryAccessRange(thr, pc, (uptr)s1, len < n ? len + 1 : n, false); 481 MemoryAccessRange(thr, pc, (uptr)s2, len < n ? len + 1 : n, false); 482 return res; 483 } 484 485 TSAN_INTERCEPTOR(int, strcmp, const char *s1, const char *s2) { 486 SCOPED_TSAN_INTERCEPTOR(strcmp, s1, s2); 487 uptr len = 0; 488 for (; s1[len] && s2[len]; len++) { 489 if (s1[len] != s2[len]) 490 break; 491 } 492 MemoryAccessRange(thr, pc, (uptr)s1, len + 1, false); 493 MemoryAccessRange(thr, pc, (uptr)s2, len + 1, false); 494 return s1[len] - s2[len]; 495 } 496 497 TSAN_INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr n) { 498 SCOPED_TSAN_INTERCEPTOR(strncmp, s1, s2, n); 499 uptr len = 0; 500 for (; len < n && s1[len] && s2[len]; len++) { 501 if (s1[len] != s2[len]) 502 break; 503 } 504 MemoryAccessRange(thr, pc, (uptr)s1, len < n ? len + 1 : n, false); 505 MemoryAccessRange(thr, pc, (uptr)s2, len < n ? len + 1 : n, false); 506 return len == n ? 0 : s1[len] - s2[len]; 507 } 508 509 TSAN_INTERCEPTOR(void*, memchr, void *s, int c, uptr n) { 510 SCOPED_TSAN_INTERCEPTOR(memchr, s, c, n); 511 void *res = REAL(memchr)(s, c, n); 512 uptr len = res ? (char*)res - (char*)s + 1 : n; 513 MemoryAccessRange(thr, pc, (uptr)s, len, false); 514 return res; 515 } 516 517 TSAN_INTERCEPTOR(void*, memrchr, char *s, int c, uptr n) { 518 SCOPED_TSAN_INTERCEPTOR(memrchr, s, c, n); 519 MemoryAccessRange(thr, pc, (uptr)s, n, false); 520 return REAL(memrchr)(s, c, n); 521 } 522 523 TSAN_INTERCEPTOR(void*, memmove, void *dst, void *src, uptr n) { 524 SCOPED_TSAN_INTERCEPTOR(memmove, dst, src, n); 525 MemoryAccessRange(thr, pc, (uptr)dst, n, true); 526 MemoryAccessRange(thr, pc, (uptr)src, n, false); 527 return REAL(memmove)(dst, src, n); 528 } 529 530 TSAN_INTERCEPTOR(char*, strchr, char *s, int c) { 531 SCOPED_TSAN_INTERCEPTOR(strchr, s, c); 532 char *res = REAL(strchr)(s, c); 533 uptr len = res ? (char*)res - (char*)s + 1 : internal_strlen(s) + 1; 534 MemoryAccessRange(thr, pc, (uptr)s, len, false); 535 return res; 536 } 537 538 TSAN_INTERCEPTOR(char*, strchrnul, char *s, int c) { 539 SCOPED_TSAN_INTERCEPTOR(strchrnul, s, c); 540 char *res = REAL(strchrnul)(s, c); 541 uptr len = (char*)res - (char*)s + 1; 542 MemoryAccessRange(thr, pc, (uptr)s, len, false); 543 return res; 544 } 545 546 TSAN_INTERCEPTOR(char*, strrchr, char *s, int c) { 547 SCOPED_TSAN_INTERCEPTOR(strrchr, s, c); 548 MemoryAccessRange(thr, pc, (uptr)s, internal_strlen(s) + 1, false); 549 return REAL(strrchr)(s, c); 550 } 551 552 TSAN_INTERCEPTOR(char*, strcpy, char *dst, const char *src) { // NOLINT 553 SCOPED_TSAN_INTERCEPTOR(strcpy, dst, src); // NOLINT 554 uptr srclen = internal_strlen(src); 555 MemoryAccessRange(thr, pc, (uptr)dst, srclen + 1, true); 556 MemoryAccessRange(thr, pc, (uptr)src, srclen + 1, false); 557 return REAL(strcpy)(dst, src); // NOLINT 558 } 559 560 TSAN_INTERCEPTOR(char*, strncpy, char *dst, char *src, uptr n) { 561 SCOPED_TSAN_INTERCEPTOR(strncpy, dst, src, n); 562 uptr srclen = internal_strnlen(src, n); 563 MemoryAccessRange(thr, pc, (uptr)dst, n, true); 564 MemoryAccessRange(thr, pc, (uptr)src, min(srclen + 1, n), false); 565 return REAL(strncpy)(dst, src, n); 566 } 567 568 TSAN_INTERCEPTOR(const char*, strstr, const char *s1, const char *s2) { 569 SCOPED_TSAN_INTERCEPTOR(strstr, s1, s2); 570 const char *res = REAL(strstr)(s1, s2); 571 uptr len1 = internal_strlen(s1); 572 uptr len2 = internal_strlen(s2); 573 MemoryAccessRange(thr, pc, (uptr)s1, len1 + 1, false); 574 MemoryAccessRange(thr, pc, (uptr)s2, len2 + 1, false); 575 return res; 576 } 577 578 static bool fix_mmap_addr(void **addr, long_t sz, int flags) { 579 if (*addr) { 580 if (!IsAppMem((uptr)*addr) || !IsAppMem((uptr)*addr + sz - 1)) { 581 if (flags & MAP_FIXED) { 582 errno = EINVAL; 583 return false; 584 } else { 585 *addr = 0; 586 } 587 } 588 } 589 return true; 590 } 591 592 TSAN_INTERCEPTOR(void*, mmap, void *addr, long_t sz, int prot, 593 int flags, int fd, unsigned off) { 594 SCOPED_TSAN_INTERCEPTOR(mmap, addr, sz, prot, flags, fd, off); 595 if (!fix_mmap_addr(&addr, sz, flags)) 596 return MAP_FAILED; 597 void *res = REAL(mmap)(addr, sz, prot, flags, fd, off); 598 if (res != MAP_FAILED) { 599 if (fd > 0) 600 FdAccess(thr, pc, fd); 601 MemoryRangeImitateWrite(thr, pc, (uptr)res, sz); 602 } 603 return res; 604 } 605 606 TSAN_INTERCEPTOR(void*, mmap64, void *addr, long_t sz, int prot, 607 int flags, int fd, u64 off) { 608 SCOPED_TSAN_INTERCEPTOR(mmap64, addr, sz, prot, flags, fd, off); 609 if (!fix_mmap_addr(&addr, sz, flags)) 610 return MAP_FAILED; 611 void *res = REAL(mmap64)(addr, sz, prot, flags, fd, off); 612 if (res != MAP_FAILED) { 613 if (fd > 0) 614 FdAccess(thr, pc, fd); 615 MemoryRangeImitateWrite(thr, pc, (uptr)res, sz); 616 } 617 return res; 618 } 619 620 TSAN_INTERCEPTOR(int, munmap, void *addr, long_t sz) { 621 SCOPED_TSAN_INTERCEPTOR(munmap, addr, sz); 622 DontNeedShadowFor((uptr)addr, sz); 623 int res = REAL(munmap)(addr, sz); 624 return res; 625 } 626 627 TSAN_INTERCEPTOR(void*, memalign, uptr align, uptr sz) { 628 SCOPED_TSAN_INTERCEPTOR(memalign, align, sz); 629 return user_alloc(thr, pc, sz, align); 630 } 631 632 TSAN_INTERCEPTOR(void*, valloc, uptr sz) { 633 SCOPED_TSAN_INTERCEPTOR(valloc, sz); 634 return user_alloc(thr, pc, sz, GetPageSizeCached()); 635 } 636 637 TSAN_INTERCEPTOR(void*, pvalloc, uptr sz) { 638 SCOPED_TSAN_INTERCEPTOR(pvalloc, sz); 639 sz = RoundUp(sz, GetPageSizeCached()); 640 return user_alloc(thr, pc, sz, GetPageSizeCached()); 641 } 642 643 TSAN_INTERCEPTOR(int, posix_memalign, void **memptr, uptr align, uptr sz) { 644 SCOPED_TSAN_INTERCEPTOR(posix_memalign, memptr, align, sz); 645 *memptr = user_alloc(thr, pc, sz, align); 646 return 0; 647 } 648 649 // Used in thread-safe function static initialization. 650 extern "C" int INTERFACE_ATTRIBUTE __cxa_guard_acquire(atomic_uint32_t *g) { 651 SCOPED_INTERCEPTOR_RAW(__cxa_guard_acquire, g); 652 for (;;) { 653 u32 cmp = atomic_load(g, memory_order_acquire); 654 if (cmp == 0) { 655 if (atomic_compare_exchange_strong(g, &cmp, 1<<16, memory_order_relaxed)) 656 return 1; 657 } else if (cmp == 1) { 658 Acquire(thr, pc, (uptr)g); 659 return 0; 660 } else { 661 internal_sched_yield(); 662 } 663 } 664 } 665 666 extern "C" void INTERFACE_ATTRIBUTE __cxa_guard_release(atomic_uint32_t *g) { 667 SCOPED_INTERCEPTOR_RAW(__cxa_guard_release, g); 668 Release(thr, pc, (uptr)g); 669 atomic_store(g, 1, memory_order_release); 670 } 671 672 extern "C" void INTERFACE_ATTRIBUTE __cxa_guard_abort(atomic_uint32_t *g) { 673 SCOPED_INTERCEPTOR_RAW(__cxa_guard_abort, g); 674 atomic_store(g, 0, memory_order_relaxed); 675 } 676 677 static void thread_finalize(void *v) { 678 uptr iter = (uptr)v; 679 if (iter > 1) { 680 if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) { 681 Printf("ThreadSanitizer: failed to set thread key\n"); 682 Die(); 683 } 684 return; 685 } 686 { 687 ScopedInRtl in_rtl; 688 ThreadState *thr = cur_thread(); 689 ThreadFinish(thr); 690 SignalContext *sctx = thr->signal_ctx; 691 if (sctx) { 692 thr->signal_ctx = 0; 693 UnmapOrDie(sctx, sizeof(*sctx)); 694 } 695 } 696 } 697 698 699 struct ThreadParam { 700 void* (*callback)(void *arg); 701 void *param; 702 atomic_uintptr_t tid; 703 }; 704 705 extern "C" void *__tsan_thread_start_func(void *arg) { 706 ThreadParam *p = (ThreadParam*)arg; 707 void* (*callback)(void *arg) = p->callback; 708 void *param = p->param; 709 int tid = 0; 710 { 711 ThreadState *thr = cur_thread(); 712 ScopedInRtl in_rtl; 713 if (pthread_setspecific(g_thread_finalize_key, (void*)4)) { 714 Printf("ThreadSanitizer: failed to set thread key\n"); 715 Die(); 716 } 717 while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0) 718 pthread_yield(); 719 atomic_store(&p->tid, 0, memory_order_release); 720 ThreadStart(thr, tid, GetTid()); 721 CHECK_EQ(thr->in_rtl, 1); 722 } 723 void *res = callback(param); 724 // Prevent the callback from being tail called, 725 // it mixes up stack traces. 726 volatile int foo = 42; 727 foo++; 728 return res; 729 } 730 731 TSAN_INTERCEPTOR(int, pthread_create, 732 void *th, void *attr, void *(*callback)(void*), void * param) { 733 SCOPED_TSAN_INTERCEPTOR(pthread_create, th, attr, callback, param); 734 __sanitizer_pthread_attr_t myattr; 735 if (attr == 0) { 736 pthread_attr_init(&myattr); 737 attr = &myattr; 738 } 739 int detached = 0; 740 pthread_attr_getdetachstate(attr, &detached); 741 uptr stacksize = 0; 742 pthread_attr_getstacksize(attr, &stacksize); 743 // We place the huge ThreadState object into TLS, account for that. 744 const uptr minstacksize = GetTlsSize() + 128*1024; 745 if (stacksize < minstacksize) { 746 DPrintf("ThreadSanitizer: stacksize %zu->%zu\n", stacksize, minstacksize); 747 pthread_attr_setstacksize(attr, minstacksize); 748 } 749 ThreadParam p; 750 p.callback = callback; 751 p.param = param; 752 atomic_store(&p.tid, 0, memory_order_relaxed); 753 int res = REAL(pthread_create)(th, attr, __tsan_thread_start_func, &p); 754 if (res == 0) { 755 int tid = ThreadCreate(thr, pc, *(uptr*)th, detached); 756 CHECK_NE(tid, 0); 757 atomic_store(&p.tid, tid, memory_order_release); 758 while (atomic_load(&p.tid, memory_order_acquire) != 0) 759 pthread_yield(); 760 } 761 if (attr == &myattr) 762 pthread_attr_destroy(&myattr); 763 return res; 764 } 765 766 TSAN_INTERCEPTOR(int, pthread_join, void *th, void **ret) { 767 SCOPED_TSAN_INTERCEPTOR(pthread_join, th, ret); 768 int tid = ThreadTid(thr, pc, (uptr)th); 769 int res = BLOCK_REAL(pthread_join)(th, ret); 770 if (res == 0) { 771 ThreadJoin(thr, pc, tid); 772 } 773 return res; 774 } 775 776 TSAN_INTERCEPTOR(int, pthread_detach, void *th) { 777 SCOPED_TSAN_INTERCEPTOR(pthread_detach, th); 778 int tid = ThreadTid(thr, pc, (uptr)th); 779 int res = REAL(pthread_detach)(th); 780 if (res == 0) { 781 ThreadDetach(thr, pc, tid); 782 } 783 return res; 784 } 785 786 TSAN_INTERCEPTOR(int, pthread_mutex_init, void *m, void *a) { 787 SCOPED_TSAN_INTERCEPTOR(pthread_mutex_init, m, a); 788 int res = REAL(pthread_mutex_init)(m, a); 789 if (res == 0) { 790 bool recursive = false; 791 if (a) { 792 int type = 0; 793 if (pthread_mutexattr_gettype(a, &type) == 0) 794 recursive = (type == PTHREAD_MUTEX_RECURSIVE 795 || type == PTHREAD_MUTEX_RECURSIVE_NP); 796 } 797 MutexCreate(thr, pc, (uptr)m, false, recursive, false); 798 } 799 return res; 800 } 801 802 TSAN_INTERCEPTOR(int, pthread_mutex_destroy, void *m) { 803 SCOPED_TSAN_INTERCEPTOR(pthread_mutex_destroy, m); 804 int res = REAL(pthread_mutex_destroy)(m); 805 if (res == 0 || res == EBUSY) { 806 MutexDestroy(thr, pc, (uptr)m); 807 } 808 return res; 809 } 810 811 TSAN_INTERCEPTOR(int, pthread_mutex_lock, void *m) { 812 SCOPED_TSAN_INTERCEPTOR(pthread_mutex_lock, m); 813 int res = REAL(pthread_mutex_lock)(m); 814 if (res == 0) { 815 MutexLock(thr, pc, (uptr)m); 816 } 817 return res; 818 } 819 820 TSAN_INTERCEPTOR(int, pthread_mutex_trylock, void *m) { 821 SCOPED_TSAN_INTERCEPTOR(pthread_mutex_trylock, m); 822 int res = REAL(pthread_mutex_trylock)(m); 823 if (res == 0) { 824 MutexLock(thr, pc, (uptr)m); 825 } 826 return res; 827 } 828 829 TSAN_INTERCEPTOR(int, pthread_mutex_timedlock, void *m, void *abstime) { 830 SCOPED_TSAN_INTERCEPTOR(pthread_mutex_timedlock, m, abstime); 831 int res = REAL(pthread_mutex_timedlock)(m, abstime); 832 if (res == 0) { 833 MutexLock(thr, pc, (uptr)m); 834 } 835 return res; 836 } 837 838 TSAN_INTERCEPTOR(int, pthread_mutex_unlock, void *m) { 839 SCOPED_TSAN_INTERCEPTOR(pthread_mutex_unlock, m); 840 MutexUnlock(thr, pc, (uptr)m); 841 int res = REAL(pthread_mutex_unlock)(m); 842 return res; 843 } 844 845 TSAN_INTERCEPTOR(int, pthread_spin_init, void *m, int pshared) { 846 SCOPED_TSAN_INTERCEPTOR(pthread_spin_init, m, pshared); 847 int res = REAL(pthread_spin_init)(m, pshared); 848 if (res == 0) { 849 MutexCreate(thr, pc, (uptr)m, false, false, false); 850 } 851 return res; 852 } 853 854 TSAN_INTERCEPTOR(int, pthread_spin_destroy, void *m) { 855 SCOPED_TSAN_INTERCEPTOR(pthread_spin_destroy, m); 856 int res = REAL(pthread_spin_destroy)(m); 857 if (res == 0) { 858 MutexDestroy(thr, pc, (uptr)m); 859 } 860 return res; 861 } 862 863 TSAN_INTERCEPTOR(int, pthread_spin_lock, void *m) { 864 SCOPED_TSAN_INTERCEPTOR(pthread_spin_lock, m); 865 int res = REAL(pthread_spin_lock)(m); 866 if (res == 0) { 867 MutexLock(thr, pc, (uptr)m); 868 } 869 return res; 870 } 871 872 TSAN_INTERCEPTOR(int, pthread_spin_trylock, void *m) { 873 SCOPED_TSAN_INTERCEPTOR(pthread_spin_trylock, m); 874 int res = REAL(pthread_spin_trylock)(m); 875 if (res == 0) { 876 MutexLock(thr, pc, (uptr)m); 877 } 878 return res; 879 } 880 881 TSAN_INTERCEPTOR(int, pthread_spin_unlock, void *m) { 882 SCOPED_TSAN_INTERCEPTOR(pthread_spin_unlock, m); 883 MutexUnlock(thr, pc, (uptr)m); 884 int res = REAL(pthread_spin_unlock)(m); 885 return res; 886 } 887 888 TSAN_INTERCEPTOR(int, pthread_rwlock_init, void *m, void *a) { 889 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_init, m, a); 890 int res = REAL(pthread_rwlock_init)(m, a); 891 if (res == 0) { 892 MutexCreate(thr, pc, (uptr)m, true, false, false); 893 } 894 return res; 895 } 896 897 TSAN_INTERCEPTOR(int, pthread_rwlock_destroy, void *m) { 898 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_destroy, m); 899 int res = REAL(pthread_rwlock_destroy)(m); 900 if (res == 0) { 901 MutexDestroy(thr, pc, (uptr)m); 902 } 903 return res; 904 } 905 906 TSAN_INTERCEPTOR(int, pthread_rwlock_rdlock, void *m) { 907 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_rdlock, m); 908 int res = REAL(pthread_rwlock_rdlock)(m); 909 if (res == 0) { 910 MutexReadLock(thr, pc, (uptr)m); 911 } 912 return res; 913 } 914 915 TSAN_INTERCEPTOR(int, pthread_rwlock_tryrdlock, void *m) { 916 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_tryrdlock, m); 917 int res = REAL(pthread_rwlock_tryrdlock)(m); 918 if (res == 0) { 919 MutexReadLock(thr, pc, (uptr)m); 920 } 921 return res; 922 } 923 924 TSAN_INTERCEPTOR(int, pthread_rwlock_timedrdlock, void *m, void *abstime) { 925 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_timedrdlock, m, abstime); 926 int res = REAL(pthread_rwlock_timedrdlock)(m, abstime); 927 if (res == 0) { 928 MutexReadLock(thr, pc, (uptr)m); 929 } 930 return res; 931 } 932 933 TSAN_INTERCEPTOR(int, pthread_rwlock_wrlock, void *m) { 934 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_wrlock, m); 935 int res = REAL(pthread_rwlock_wrlock)(m); 936 if (res == 0) { 937 MutexLock(thr, pc, (uptr)m); 938 } 939 return res; 940 } 941 942 TSAN_INTERCEPTOR(int, pthread_rwlock_trywrlock, void *m) { 943 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_trywrlock, m); 944 int res = REAL(pthread_rwlock_trywrlock)(m); 945 if (res == 0) { 946 MutexLock(thr, pc, (uptr)m); 947 } 948 return res; 949 } 950 951 TSAN_INTERCEPTOR(int, pthread_rwlock_timedwrlock, void *m, void *abstime) { 952 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_timedwrlock, m, abstime); 953 int res = REAL(pthread_rwlock_timedwrlock)(m, abstime); 954 if (res == 0) { 955 MutexLock(thr, pc, (uptr)m); 956 } 957 return res; 958 } 959 960 TSAN_INTERCEPTOR(int, pthread_rwlock_unlock, void *m) { 961 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_unlock, m); 962 MutexReadOrWriteUnlock(thr, pc, (uptr)m); 963 int res = REAL(pthread_rwlock_unlock)(m); 964 return res; 965 } 966 967 // libpthread.so contains several versions of pthread_cond_init symbol. 968 // When we just dlsym() it, we get the wrong (old) version. 969 /* 970 TSAN_INTERCEPTOR(int, pthread_cond_init, void *c, void *a) { 971 SCOPED_TSAN_INTERCEPTOR(pthread_cond_init, c, a); 972 int res = REAL(pthread_cond_init)(c, a); 973 return res; 974 } 975 */ 976 977 TSAN_INTERCEPTOR(int, pthread_cond_destroy, void *c) { 978 SCOPED_TSAN_INTERCEPTOR(pthread_cond_destroy, c); 979 int res = REAL(pthread_cond_destroy)(c); 980 return res; 981 } 982 983 TSAN_INTERCEPTOR(int, pthread_cond_signal, void *c) { 984 SCOPED_TSAN_INTERCEPTOR(pthread_cond_signal, c); 985 int res = REAL(pthread_cond_signal)(c); 986 return res; 987 } 988 989 TSAN_INTERCEPTOR(int, pthread_cond_broadcast, void *c) { 990 SCOPED_TSAN_INTERCEPTOR(pthread_cond_broadcast, c); 991 int res = REAL(pthread_cond_broadcast)(c); 992 return res; 993 } 994 995 TSAN_INTERCEPTOR(int, pthread_cond_wait, void *c, void *m) { 996 SCOPED_TSAN_INTERCEPTOR(pthread_cond_wait, c, m); 997 MutexUnlock(thr, pc, (uptr)m); 998 int res = REAL(pthread_cond_wait)(c, m); 999 MutexLock(thr, pc, (uptr)m); 1000 return res; 1001 } 1002 1003 TSAN_INTERCEPTOR(int, pthread_cond_timedwait, void *c, void *m, void *abstime) { 1004 SCOPED_TSAN_INTERCEPTOR(pthread_cond_timedwait, c, m, abstime); 1005 MutexUnlock(thr, pc, (uptr)m); 1006 int res = REAL(pthread_cond_timedwait)(c, m, abstime); 1007 MutexLock(thr, pc, (uptr)m); 1008 return res; 1009 } 1010 1011 TSAN_INTERCEPTOR(int, pthread_barrier_init, void *b, void *a, unsigned count) { 1012 SCOPED_TSAN_INTERCEPTOR(pthread_barrier_init, b, a, count); 1013 MemoryWrite(thr, pc, (uptr)b, kSizeLog1); 1014 int res = REAL(pthread_barrier_init)(b, a, count); 1015 return res; 1016 } 1017 1018 TSAN_INTERCEPTOR(int, pthread_barrier_destroy, void *b) { 1019 SCOPED_TSAN_INTERCEPTOR(pthread_barrier_destroy, b); 1020 MemoryWrite(thr, pc, (uptr)b, kSizeLog1); 1021 int res = REAL(pthread_barrier_destroy)(b); 1022 return res; 1023 } 1024 1025 TSAN_INTERCEPTOR(int, pthread_barrier_wait, void *b) { 1026 SCOPED_TSAN_INTERCEPTOR(pthread_barrier_wait, b); 1027 Release(thr, pc, (uptr)b); 1028 MemoryRead(thr, pc, (uptr)b, kSizeLog1); 1029 int res = REAL(pthread_barrier_wait)(b); 1030 MemoryRead(thr, pc, (uptr)b, kSizeLog1); 1031 if (res == 0 || res == PTHREAD_BARRIER_SERIAL_THREAD) { 1032 Acquire(thr, pc, (uptr)b); 1033 } 1034 return res; 1035 } 1036 1037 TSAN_INTERCEPTOR(int, pthread_once, void *o, void (*f)()) { 1038 SCOPED_TSAN_INTERCEPTOR(pthread_once, o, f); 1039 if (o == 0 || f == 0) 1040 return EINVAL; 1041 atomic_uint32_t *a = static_cast<atomic_uint32_t*>(o); 1042 u32 v = atomic_load(a, memory_order_acquire); 1043 if (v == 0 && atomic_compare_exchange_strong(a, &v, 1, 1044 memory_order_relaxed)) { 1045 const int old_in_rtl = thr->in_rtl; 1046 thr->in_rtl = 0; 1047 (*f)(); 1048 CHECK_EQ(thr->in_rtl, 0); 1049 thr->in_rtl = old_in_rtl; 1050 Release(thr, pc, (uptr)o); 1051 atomic_store(a, 2, memory_order_release); 1052 } else { 1053 while (v != 2) { 1054 pthread_yield(); 1055 v = atomic_load(a, memory_order_acquire); 1056 } 1057 Acquire(thr, pc, (uptr)o); 1058 } 1059 return 0; 1060 } 1061 1062 TSAN_INTERCEPTOR(int, sem_init, void *s, int pshared, unsigned value) { 1063 SCOPED_TSAN_INTERCEPTOR(sem_init, s, pshared, value); 1064 int res = REAL(sem_init)(s, pshared, value); 1065 return res; 1066 } 1067 1068 TSAN_INTERCEPTOR(int, sem_destroy, void *s) { 1069 SCOPED_TSAN_INTERCEPTOR(sem_destroy, s); 1070 int res = REAL(sem_destroy)(s); 1071 return res; 1072 } 1073 1074 TSAN_INTERCEPTOR(int, sem_wait, void *s) { 1075 SCOPED_TSAN_INTERCEPTOR(sem_wait, s); 1076 int res = BLOCK_REAL(sem_wait)(s); 1077 if (res == 0) { 1078 Acquire(thr, pc, (uptr)s); 1079 } 1080 return res; 1081 } 1082 1083 TSAN_INTERCEPTOR(int, sem_trywait, void *s) { 1084 SCOPED_TSAN_INTERCEPTOR(sem_trywait, s); 1085 int res = BLOCK_REAL(sem_trywait)(s); 1086 if (res == 0) { 1087 Acquire(thr, pc, (uptr)s); 1088 } 1089 return res; 1090 } 1091 1092 TSAN_INTERCEPTOR(int, sem_timedwait, void *s, void *abstime) { 1093 SCOPED_TSAN_INTERCEPTOR(sem_timedwait, s, abstime); 1094 int res = BLOCK_REAL(sem_timedwait)(s, abstime); 1095 if (res == 0) { 1096 Acquire(thr, pc, (uptr)s); 1097 } 1098 return res; 1099 } 1100 1101 TSAN_INTERCEPTOR(int, sem_post, void *s) { 1102 SCOPED_TSAN_INTERCEPTOR(sem_post, s); 1103 Release(thr, pc, (uptr)s); 1104 int res = REAL(sem_post)(s); 1105 return res; 1106 } 1107 1108 TSAN_INTERCEPTOR(int, sem_getvalue, void *s, int *sval) { 1109 SCOPED_TSAN_INTERCEPTOR(sem_getvalue, s, sval); 1110 int res = REAL(sem_getvalue)(s, sval); 1111 if (res == 0) { 1112 Acquire(thr, pc, (uptr)s); 1113 } 1114 return res; 1115 } 1116 1117 TSAN_INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) { 1118 SCOPED_TSAN_INTERCEPTOR(__xstat, version, path, buf); 1119 return REAL(__xstat)(version, path, buf); 1120 } 1121 1122 TSAN_INTERCEPTOR(int, stat, const char *path, void *buf) { 1123 SCOPED_TSAN_INTERCEPTOR(__xstat, 0, path, buf); 1124 return REAL(__xstat)(0, path, buf); 1125 } 1126 1127 TSAN_INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) { 1128 SCOPED_TSAN_INTERCEPTOR(__xstat64, version, path, buf); 1129 return REAL(__xstat64)(version, path, buf); 1130 } 1131 1132 TSAN_INTERCEPTOR(int, stat64, const char *path, void *buf) { 1133 SCOPED_TSAN_INTERCEPTOR(__xstat64, 0, path, buf); 1134 return REAL(__xstat64)(0, path, buf); 1135 } 1136 1137 TSAN_INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) { 1138 SCOPED_TSAN_INTERCEPTOR(__lxstat, version, path, buf); 1139 return REAL(__lxstat)(version, path, buf); 1140 } 1141 1142 TSAN_INTERCEPTOR(int, lstat, const char *path, void *buf) { 1143 SCOPED_TSAN_INTERCEPTOR(__lxstat, 0, path, buf); 1144 return REAL(__lxstat)(0, path, buf); 1145 } 1146 1147 TSAN_INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) { 1148 SCOPED_TSAN_INTERCEPTOR(__lxstat64, version, path, buf); 1149 return REAL(__lxstat64)(version, path, buf); 1150 } 1151 1152 TSAN_INTERCEPTOR(int, lstat64, const char *path, void *buf) { 1153 SCOPED_TSAN_INTERCEPTOR(__lxstat64, 0, path, buf); 1154 return REAL(__lxstat64)(0, path, buf); 1155 } 1156 1157 TSAN_INTERCEPTOR(int, __fxstat, int version, int fd, void *buf) { 1158 SCOPED_TSAN_INTERCEPTOR(__fxstat, version, fd, buf); 1159 if (fd > 0) 1160 FdAccess(thr, pc, fd); 1161 return REAL(__fxstat)(version, fd, buf); 1162 } 1163 1164 TSAN_INTERCEPTOR(int, fstat, int fd, void *buf) { 1165 SCOPED_TSAN_INTERCEPTOR(__fxstat, 0, fd, buf); 1166 if (fd > 0) 1167 FdAccess(thr, pc, fd); 1168 return REAL(__fxstat)(0, fd, buf); 1169 } 1170 1171 TSAN_INTERCEPTOR(int, __fxstat64, int version, int fd, void *buf) { 1172 SCOPED_TSAN_INTERCEPTOR(__fxstat64, version, fd, buf); 1173 if (fd > 0) 1174 FdAccess(thr, pc, fd); 1175 return REAL(__fxstat64)(version, fd, buf); 1176 } 1177 1178 TSAN_INTERCEPTOR(int, fstat64, int fd, void *buf) { 1179 SCOPED_TSAN_INTERCEPTOR(__fxstat64, 0, fd, buf); 1180 if (fd > 0) 1181 FdAccess(thr, pc, fd); 1182 return REAL(__fxstat64)(0, fd, buf); 1183 } 1184 1185 TSAN_INTERCEPTOR(int, open, const char *name, int flags, int mode) { 1186 SCOPED_TSAN_INTERCEPTOR(open, name, flags, mode); 1187 int fd = REAL(open)(name, flags, mode); 1188 if (fd >= 0) 1189 FdFileCreate(thr, pc, fd); 1190 return fd; 1191 } 1192 1193 TSAN_INTERCEPTOR(int, open64, const char *name, int flags, int mode) { 1194 SCOPED_TSAN_INTERCEPTOR(open64, name, flags, mode); 1195 int fd = REAL(open64)(name, flags, mode); 1196 if (fd >= 0) 1197 FdFileCreate(thr, pc, fd); 1198 return fd; 1199 } 1200 1201 TSAN_INTERCEPTOR(int, creat, const char *name, int mode) { 1202 SCOPED_TSAN_INTERCEPTOR(creat, name, mode); 1203 int fd = REAL(creat)(name, mode); 1204 if (fd >= 0) 1205 FdFileCreate(thr, pc, fd); 1206 return fd; 1207 } 1208 1209 TSAN_INTERCEPTOR(int, creat64, const char *name, int mode) { 1210 SCOPED_TSAN_INTERCEPTOR(creat64, name, mode); 1211 int fd = REAL(creat64)(name, mode); 1212 if (fd >= 0) 1213 FdFileCreate(thr, pc, fd); 1214 return fd; 1215 } 1216 1217 TSAN_INTERCEPTOR(int, dup, int oldfd) { 1218 SCOPED_TSAN_INTERCEPTOR(dup, oldfd); 1219 int newfd = REAL(dup)(oldfd); 1220 if (oldfd >= 0 && newfd >= 0 && newfd != oldfd) 1221 FdDup(thr, pc, oldfd, newfd); 1222 return newfd; 1223 } 1224 1225 TSAN_INTERCEPTOR(int, dup2, int oldfd, int newfd) { 1226 SCOPED_TSAN_INTERCEPTOR(dup2, oldfd, newfd); 1227 int newfd2 = REAL(dup2)(oldfd, newfd); 1228 if (oldfd >= 0 && newfd2 >= 0 && newfd2 != oldfd) 1229 FdDup(thr, pc, oldfd, newfd2); 1230 return newfd2; 1231 } 1232 1233 TSAN_INTERCEPTOR(int, dup3, int oldfd, int newfd, int flags) { 1234 SCOPED_TSAN_INTERCEPTOR(dup3, oldfd, newfd, flags); 1235 int newfd2 = REAL(dup3)(oldfd, newfd, flags); 1236 if (oldfd >= 0 && newfd2 >= 0 && newfd2 != oldfd) 1237 FdDup(thr, pc, oldfd, newfd2); 1238 return newfd2; 1239 } 1240 1241 TSAN_INTERCEPTOR(int, eventfd, unsigned initval, int flags) { 1242 SCOPED_TSAN_INTERCEPTOR(eventfd, initval, flags); 1243 int fd = REAL(eventfd)(initval, flags); 1244 if (fd >= 0) 1245 FdEventCreate(thr, pc, fd); 1246 return fd; 1247 } 1248 1249 TSAN_INTERCEPTOR(int, signalfd, int fd, void *mask, int flags) { 1250 SCOPED_TSAN_INTERCEPTOR(signalfd, fd, mask, flags); 1251 if (fd >= 0) 1252 FdClose(thr, pc, fd); 1253 fd = REAL(signalfd)(fd, mask, flags); 1254 if (fd >= 0) 1255 FdSignalCreate(thr, pc, fd); 1256 return fd; 1257 } 1258 1259 TSAN_INTERCEPTOR(int, inotify_init, int fake) { 1260 SCOPED_TSAN_INTERCEPTOR(inotify_init, fake); 1261 int fd = REAL(inotify_init)(fake); 1262 if (fd >= 0) 1263 FdInotifyCreate(thr, pc, fd); 1264 return fd; 1265 } 1266 1267 TSAN_INTERCEPTOR(int, inotify_init1, int flags) { 1268 SCOPED_TSAN_INTERCEPTOR(inotify_init1, flags); 1269 int fd = REAL(inotify_init1)(flags); 1270 if (fd >= 0) 1271 FdInotifyCreate(thr, pc, fd); 1272 return fd; 1273 } 1274 1275 TSAN_INTERCEPTOR(int, socket, int domain, int type, int protocol) { 1276 SCOPED_TSAN_INTERCEPTOR(socket, domain, type, protocol); 1277 int fd = REAL(socket)(domain, type, protocol); 1278 if (fd >= 0) 1279 FdSocketCreate(thr, pc, fd); 1280 return fd; 1281 } 1282 1283 TSAN_INTERCEPTOR(int, socketpair, int domain, int type, int protocol, int *fd) { 1284 SCOPED_TSAN_INTERCEPTOR(socketpair, domain, type, protocol, fd); 1285 int res = REAL(socketpair)(domain, type, protocol, fd); 1286 if (res == 0 && fd[0] >= 0 && fd[1] >= 0) 1287 FdPipeCreate(thr, pc, fd[0], fd[1]); 1288 return res; 1289 } 1290 1291 TSAN_INTERCEPTOR(int, connect, int fd, void *addr, unsigned addrlen) { 1292 SCOPED_TSAN_INTERCEPTOR(connect, fd, addr, addrlen); 1293 FdSocketConnecting(thr, pc, fd); 1294 int res = REAL(connect)(fd, addr, addrlen); 1295 if (res == 0 && fd >= 0) 1296 FdSocketConnect(thr, pc, fd); 1297 return res; 1298 } 1299 1300 TSAN_INTERCEPTOR(int, bind, int fd, void *addr, unsigned addrlen) { 1301 SCOPED_TSAN_INTERCEPTOR(bind, fd, addr, addrlen); 1302 int res = REAL(bind)(fd, addr, addrlen); 1303 if (fd > 0 && res == 0) 1304 FdAccess(thr, pc, fd); 1305 return res; 1306 } 1307 1308 TSAN_INTERCEPTOR(int, listen, int fd, int backlog) { 1309 SCOPED_TSAN_INTERCEPTOR(listen, fd, backlog); 1310 int res = REAL(listen)(fd, backlog); 1311 if (fd > 0 && res == 0) 1312 FdAccess(thr, pc, fd); 1313 return res; 1314 } 1315 1316 TSAN_INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) { 1317 SCOPED_TSAN_INTERCEPTOR(accept, fd, addr, addrlen); 1318 int fd2 = REAL(accept)(fd, addr, addrlen); 1319 if (fd >= 0 && fd2 >= 0) 1320 FdSocketAccept(thr, pc, fd, fd2); 1321 return fd2; 1322 } 1323 1324 TSAN_INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) { 1325 SCOPED_TSAN_INTERCEPTOR(accept4, fd, addr, addrlen, f); 1326 int fd2 = REAL(accept4)(fd, addr, addrlen, f); 1327 if (fd >= 0 && fd2 >= 0) 1328 FdSocketAccept(thr, pc, fd, fd2); 1329 return fd2; 1330 } 1331 1332 TSAN_INTERCEPTOR(int, epoll_create, int size) { 1333 SCOPED_TSAN_INTERCEPTOR(epoll_create, size); 1334 int fd = REAL(epoll_create)(size); 1335 if (fd >= 0) 1336 FdPollCreate(thr, pc, fd); 1337 return fd; 1338 } 1339 1340 TSAN_INTERCEPTOR(int, epoll_create1, int flags) { 1341 SCOPED_TSAN_INTERCEPTOR(epoll_create1, flags); 1342 int fd = REAL(epoll_create1)(flags); 1343 if (fd >= 0) 1344 FdPollCreate(thr, pc, fd); 1345 return fd; 1346 } 1347 1348 TSAN_INTERCEPTOR(int, close, int fd) { 1349 SCOPED_TSAN_INTERCEPTOR(close, fd); 1350 if (fd >= 0) 1351 FdClose(thr, pc, fd); 1352 return REAL(close)(fd); 1353 } 1354 1355 TSAN_INTERCEPTOR(int, __close, int fd) { 1356 SCOPED_TSAN_INTERCEPTOR(__close, fd); 1357 if (fd >= 0) 1358 FdClose(thr, pc, fd); 1359 return REAL(__close)(fd); 1360 } 1361 1362 // glibc guts 1363 TSAN_INTERCEPTOR(void, __res_iclose, void *state, bool free_addr) { 1364 SCOPED_TSAN_INTERCEPTOR(__res_iclose, state, free_addr); 1365 int fds[64]; 1366 int cnt = ExtractResolvFDs(state, fds, ARRAY_SIZE(fds)); 1367 for (int i = 0; i < cnt; i++) { 1368 if (fds[i] > 0) 1369 FdClose(thr, pc, fds[i]); 1370 } 1371 REAL(__res_iclose)(state, free_addr); 1372 } 1373 1374 TSAN_INTERCEPTOR(int, pipe, int *pipefd) { 1375 SCOPED_TSAN_INTERCEPTOR(pipe, pipefd); 1376 int res = REAL(pipe)(pipefd); 1377 if (res == 0 && pipefd[0] >= 0 && pipefd[1] >= 0) 1378 FdPipeCreate(thr, pc, pipefd[0], pipefd[1]); 1379 return res; 1380 } 1381 1382 TSAN_INTERCEPTOR(int, pipe2, int *pipefd, int flags) { 1383 SCOPED_TSAN_INTERCEPTOR(pipe2, pipefd, flags); 1384 int res = REAL(pipe2)(pipefd, flags); 1385 if (res == 0 && pipefd[0] >= 0 && pipefd[1] >= 0) 1386 FdPipeCreate(thr, pc, pipefd[0], pipefd[1]); 1387 return res; 1388 } 1389 1390 TSAN_INTERCEPTOR(long_t, readv, int fd, void *vec, int cnt) { 1391 SCOPED_TSAN_INTERCEPTOR(readv, fd, vec, cnt); 1392 int res = REAL(readv)(fd, vec, cnt); 1393 if (res >= 0 && fd >= 0) { 1394 FdAcquire(thr, pc, fd); 1395 } 1396 return res; 1397 } 1398 1399 TSAN_INTERCEPTOR(long_t, preadv64, int fd, void *vec, int cnt, u64 off) { 1400 SCOPED_TSAN_INTERCEPTOR(preadv64, fd, vec, cnt, off); 1401 int res = REAL(preadv64)(fd, vec, cnt, off); 1402 if (res >= 0 && fd >= 0) { 1403 FdAcquire(thr, pc, fd); 1404 } 1405 return res; 1406 } 1407 1408 TSAN_INTERCEPTOR(long_t, writev, int fd, void *vec, int cnt) { 1409 SCOPED_TSAN_INTERCEPTOR(writev, fd, vec, cnt); 1410 if (fd >= 0) 1411 FdRelease(thr, pc, fd); 1412 int res = REAL(writev)(fd, vec, cnt); 1413 return res; 1414 } 1415 1416 TSAN_INTERCEPTOR(long_t, pwritev64, int fd, void *vec, int cnt, u64 off) { 1417 SCOPED_TSAN_INTERCEPTOR(pwritev64, fd, vec, cnt, off); 1418 if (fd >= 0) 1419 FdRelease(thr, pc, fd); 1420 int res = REAL(pwritev64)(fd, vec, cnt, off); 1421 return res; 1422 } 1423 1424 TSAN_INTERCEPTOR(long_t, send, int fd, void *buf, long_t len, int flags) { 1425 SCOPED_TSAN_INTERCEPTOR(send, fd, buf, len, flags); 1426 if (fd >= 0) 1427 FdRelease(thr, pc, fd); 1428 int res = REAL(send)(fd, buf, len, flags); 1429 return res; 1430 } 1431 1432 TSAN_INTERCEPTOR(long_t, sendmsg, int fd, void *msg, int flags) { 1433 SCOPED_TSAN_INTERCEPTOR(sendmsg, fd, msg, flags); 1434 if (fd >= 0) 1435 FdRelease(thr, pc, fd); 1436 int res = REAL(sendmsg)(fd, msg, flags); 1437 return res; 1438 } 1439 1440 TSAN_INTERCEPTOR(long_t, recv, int fd, void *buf, long_t len, int flags) { 1441 SCOPED_TSAN_INTERCEPTOR(recv, fd, buf, len, flags); 1442 int res = REAL(recv)(fd, buf, len, flags); 1443 if (res >= 0 && fd >= 0) { 1444 FdAcquire(thr, pc, fd); 1445 } 1446 return res; 1447 } 1448 1449 TSAN_INTERCEPTOR(long_t, recvmsg, int fd, void *msg, int flags) { 1450 SCOPED_TSAN_INTERCEPTOR(recvmsg, fd, msg, flags); 1451 int res = REAL(recvmsg)(fd, msg, flags); 1452 if (res >= 0 && fd >= 0) { 1453 FdAcquire(thr, pc, fd); 1454 } 1455 return res; 1456 } 1457 1458 TSAN_INTERCEPTOR(int, unlink, char *path) { 1459 SCOPED_TSAN_INTERCEPTOR(unlink, path); 1460 Release(thr, pc, File2addr(path)); 1461 int res = REAL(unlink)(path); 1462 return res; 1463 } 1464 1465 TSAN_INTERCEPTOR(void*, fopen, char *path, char *mode) { 1466 SCOPED_TSAN_INTERCEPTOR(fopen, path, mode); 1467 void *res = REAL(fopen)(path, mode); 1468 Acquire(thr, pc, File2addr(path)); 1469 if (res) { 1470 int fd = fileno_unlocked(res); 1471 if (fd >= 0) 1472 FdFileCreate(thr, pc, fd); 1473 } 1474 return res; 1475 } 1476 1477 TSAN_INTERCEPTOR(void*, freopen, char *path, char *mode, void *stream) { 1478 SCOPED_TSAN_INTERCEPTOR(freopen, path, mode, stream); 1479 if (stream) { 1480 int fd = fileno_unlocked(stream); 1481 if (fd >= 0) 1482 FdClose(thr, pc, fd); 1483 } 1484 void *res = REAL(freopen)(path, mode, stream); 1485 Acquire(thr, pc, File2addr(path)); 1486 if (res) { 1487 int fd = fileno_unlocked(res); 1488 if (fd >= 0) 1489 FdFileCreate(thr, pc, fd); 1490 } 1491 return res; 1492 } 1493 1494 TSAN_INTERCEPTOR(int, fclose, void *stream) { 1495 { 1496 SCOPED_TSAN_INTERCEPTOR(fclose, stream); 1497 if (stream) { 1498 int fd = fileno_unlocked(stream); 1499 if (fd >= 0) 1500 FdClose(thr, pc, fd); 1501 } 1502 } 1503 return REAL(fclose)(stream); 1504 } 1505 1506 TSAN_INTERCEPTOR(uptr, fread, void *ptr, uptr size, uptr nmemb, void *f) { 1507 { 1508 SCOPED_TSAN_INTERCEPTOR(fread, ptr, size, nmemb, f); 1509 MemoryAccessRange(thr, pc, (uptr)ptr, size * nmemb, true); 1510 } 1511 return REAL(fread)(ptr, size, nmemb, f); 1512 } 1513 1514 TSAN_INTERCEPTOR(uptr, fwrite, const void *p, uptr size, uptr nmemb, void *f) { 1515 { 1516 SCOPED_TSAN_INTERCEPTOR(fwrite, p, size, nmemb, f); 1517 MemoryAccessRange(thr, pc, (uptr)p, size * nmemb, false); 1518 } 1519 return REAL(fwrite)(p, size, nmemb, f); 1520 } 1521 1522 TSAN_INTERCEPTOR(int, puts, const char *s) { 1523 SCOPED_TSAN_INTERCEPTOR(puts, s); 1524 MemoryAccessRange(thr, pc, (uptr)s, internal_strlen(s), false); 1525 return REAL(puts)(s); 1526 } 1527 1528 TSAN_INTERCEPTOR(int, rmdir, char *path) { 1529 SCOPED_TSAN_INTERCEPTOR(rmdir, path); 1530 Release(thr, pc, Dir2addr(path)); 1531 int res = REAL(rmdir)(path); 1532 return res; 1533 } 1534 1535 TSAN_INTERCEPTOR(void*, opendir, char *path) { 1536 SCOPED_TSAN_INTERCEPTOR(opendir, path); 1537 void *res = REAL(opendir)(path); 1538 if (res != 0) 1539 Acquire(thr, pc, Dir2addr(path)); 1540 return res; 1541 } 1542 1543 TSAN_INTERCEPTOR(int, epoll_ctl, int epfd, int op, int fd, void *ev) { 1544 SCOPED_TSAN_INTERCEPTOR(epoll_ctl, epfd, op, fd, ev); 1545 if (op == EPOLL_CTL_ADD && epfd >= 0) { 1546 FdRelease(thr, pc, epfd); 1547 } 1548 int res = REAL(epoll_ctl)(epfd, op, fd, ev); 1549 if (fd >= 0) 1550 FdAccess(thr, pc, fd); 1551 return res; 1552 } 1553 1554 TSAN_INTERCEPTOR(int, epoll_wait, int epfd, void *ev, int cnt, int timeout) { 1555 SCOPED_TSAN_INTERCEPTOR(epoll_wait, epfd, ev, cnt, timeout); 1556 int res = BLOCK_REAL(epoll_wait)(epfd, ev, cnt, timeout); 1557 if (res > 0 && epfd >= 0) { 1558 FdAcquire(thr, pc, epfd); 1559 } 1560 return res; 1561 } 1562 1563 TSAN_INTERCEPTOR(int, poll, void *fds, long_t nfds, int timeout) { 1564 SCOPED_TSAN_INTERCEPTOR(poll, fds, nfds, timeout); 1565 int res = BLOCK_REAL(poll)(fds, nfds, timeout); 1566 return res; 1567 } 1568 1569 static void ALWAYS_INLINE rtl_generic_sighandler(bool sigact, int sig, 1570 my_siginfo_t *info, void *ctx) { 1571 ThreadState *thr = cur_thread(); 1572 SignalContext *sctx = SigCtx(thr); 1573 // Don't mess with synchronous signals. 1574 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL || 1575 sig == SIGABRT || sig == SIGFPE || sig == SIGPIPE || 1576 // If we are sending signal to ourselves, we must process it now. 1577 (sctx && sig == sctx->int_signal_send) || 1578 // If we are in blocking function, we can safely process it now 1579 // (but check if we are in a recursive interceptor, 1580 // i.e. pthread_join()->munmap()). 1581 (sctx && sctx->in_blocking_func == 1 && thr->in_rtl == 1)) { 1582 CHECK(thr->in_rtl == 0 || thr->in_rtl == 1); 1583 int in_rtl = thr->in_rtl; 1584 thr->in_rtl = 0; 1585 CHECK_EQ(thr->in_signal_handler, false); 1586 thr->in_signal_handler = true; 1587 if (sigact) 1588 sigactions[sig].sa_sigaction(sig, info, ctx); 1589 else 1590 sigactions[sig].sa_handler(sig); 1591 CHECK_EQ(thr->in_signal_handler, true); 1592 thr->in_signal_handler = false; 1593 thr->in_rtl = in_rtl; 1594 return; 1595 } 1596 1597 if (sctx == 0) 1598 return; 1599 SignalDesc *signal = &sctx->pending_signals[sig]; 1600 if (signal->armed == false) { 1601 signal->armed = true; 1602 signal->sigaction = sigact; 1603 if (info) 1604 internal_memcpy(&signal->siginfo, info, sizeof(*info)); 1605 if (ctx) 1606 internal_memcpy(&signal->ctx, ctx, sizeof(signal->ctx)); 1607 sctx->pending_signal_count++; 1608 } 1609 } 1610 1611 static void rtl_sighandler(int sig) { 1612 rtl_generic_sighandler(false, sig, 0, 0); 1613 } 1614 1615 static void rtl_sigaction(int sig, my_siginfo_t *info, void *ctx) { 1616 rtl_generic_sighandler(true, sig, info, ctx); 1617 } 1618 1619 TSAN_INTERCEPTOR(int, sigaction, int sig, sigaction_t *act, sigaction_t *old) { 1620 SCOPED_TSAN_INTERCEPTOR(sigaction, sig, act, old); 1621 if (old) 1622 internal_memcpy(old, &sigactions[sig], sizeof(*old)); 1623 if (act == 0) 1624 return 0; 1625 internal_memcpy(&sigactions[sig], act, sizeof(*act)); 1626 sigaction_t newact; 1627 internal_memcpy(&newact, act, sizeof(newact)); 1628 sigfillset(&newact.sa_mask); 1629 if (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL) { 1630 if (newact.sa_flags & SA_SIGINFO) 1631 newact.sa_sigaction = rtl_sigaction; 1632 else 1633 newact.sa_handler = rtl_sighandler; 1634 } 1635 int res = REAL(sigaction)(sig, &newact, 0); 1636 return res; 1637 } 1638 1639 TSAN_INTERCEPTOR(sighandler_t, signal, int sig, sighandler_t h) { 1640 sigaction_t act; 1641 act.sa_handler = h; 1642 REAL(memset)(&act.sa_mask, -1, sizeof(act.sa_mask)); 1643 act.sa_flags = 0; 1644 sigaction_t old; 1645 int res = sigaction(sig, &act, &old); 1646 if (res) 1647 return SIG_ERR; 1648 return old.sa_handler; 1649 } 1650 1651 TSAN_INTERCEPTOR(int, raise, int sig) { 1652 SCOPED_TSAN_INTERCEPTOR(raise, sig); 1653 SignalContext *sctx = SigCtx(thr); 1654 CHECK_NE(sctx, 0); 1655 int prev = sctx->int_signal_send; 1656 sctx->int_signal_send = sig; 1657 int res = REAL(raise)(sig); 1658 CHECK_EQ(sctx->int_signal_send, sig); 1659 sctx->int_signal_send = prev; 1660 return res; 1661 } 1662 1663 TSAN_INTERCEPTOR(int, kill, int pid, int sig) { 1664 SCOPED_TSAN_INTERCEPTOR(kill, pid, sig); 1665 SignalContext *sctx = SigCtx(thr); 1666 CHECK_NE(sctx, 0); 1667 int prev = sctx->int_signal_send; 1668 if (pid == GetPid()) { 1669 sctx->int_signal_send = sig; 1670 } 1671 int res = REAL(kill)(pid, sig); 1672 if (pid == GetPid()) { 1673 CHECK_EQ(sctx->int_signal_send, sig); 1674 sctx->int_signal_send = prev; 1675 } 1676 return res; 1677 } 1678 1679 TSAN_INTERCEPTOR(int, pthread_kill, void *tid, int sig) { 1680 SCOPED_TSAN_INTERCEPTOR(pthread_kill, tid, sig); 1681 SignalContext *sctx = SigCtx(thr); 1682 CHECK_NE(sctx, 0); 1683 int prev = sctx->int_signal_send; 1684 if (tid == pthread_self()) { 1685 sctx->int_signal_send = sig; 1686 } 1687 int res = REAL(pthread_kill)(tid, sig); 1688 if (tid == pthread_self()) { 1689 CHECK_EQ(sctx->int_signal_send, sig); 1690 sctx->int_signal_send = prev; 1691 } 1692 return res; 1693 } 1694 1695 TSAN_INTERCEPTOR(int, gettimeofday, void *tv, void *tz) { 1696 SCOPED_TSAN_INTERCEPTOR(gettimeofday, tv, tz); 1697 // It's intercepted merely to process pending signals. 1698 return REAL(gettimeofday)(tv, tz); 1699 } 1700 1701 // Linux kernel has a bug that leads to kernel deadlock if a process 1702 // maps TBs of memory and then calls mlock(). 1703 static void MlockIsUnsupported() { 1704 static atomic_uint8_t printed; 1705 if (atomic_exchange(&printed, 1, memory_order_relaxed)) 1706 return; 1707 Printf("INFO: ThreadSanitizer ignores mlock/mlockall/munlock/munlockall\n"); 1708 } 1709 1710 TSAN_INTERCEPTOR(int, mlock, const void *addr, uptr len) { 1711 MlockIsUnsupported(); 1712 return 0; 1713 } 1714 1715 TSAN_INTERCEPTOR(int, munlock, const void *addr, uptr len) { 1716 MlockIsUnsupported(); 1717 return 0; 1718 } 1719 1720 TSAN_INTERCEPTOR(int, mlockall, int flags) { 1721 MlockIsUnsupported(); 1722 return 0; 1723 } 1724 1725 TSAN_INTERCEPTOR(int, munlockall, void) { 1726 MlockIsUnsupported(); 1727 return 0; 1728 } 1729 1730 TSAN_INTERCEPTOR(int, fork, int fake) { 1731 SCOPED_TSAN_INTERCEPTOR(fork, fake); 1732 // It's intercepted merely to process pending signals. 1733 int pid = REAL(fork)(fake); 1734 if (pid == 0) { 1735 // child 1736 FdOnFork(thr, pc); 1737 } else if (pid > 0) { 1738 // parent 1739 } 1740 return pid; 1741 } 1742 1743 struct TsanInterceptorContext { 1744 ThreadState *thr; 1745 const uptr caller_pc; 1746 const uptr pc; 1747 }; 1748 1749 #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \ 1750 MemoryAccessRange(((TsanInterceptorContext*)ctx)->thr, \ 1751 ((TsanInterceptorContext*)ctx)->pc, \ 1752 (uptr)ptr, size, true) 1753 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \ 1754 MemoryAccessRange(((TsanInterceptorContext*)ctx)->thr, \ 1755 ((TsanInterceptorContext*)ctx)->pc, \ 1756 (uptr)ptr, size, false) 1757 #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \ 1758 SCOPED_TSAN_INTERCEPTOR(func, __VA_ARGS__) \ 1759 TsanInterceptorContext _ctx = {thr, caller_pc, pc}; \ 1760 ctx = (void*)&_ctx; \ 1761 (void)ctx; 1762 #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \ 1763 FdAcquire(((TsanInterceptorContext*)ctx)->thr, pc, fd) 1764 #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \ 1765 FdRelease(((TsanInterceptorContext*)ctx)->thr, pc, fd) 1766 #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) \ 1767 ThreadSetName(((TsanInterceptorContext*)ctx)->thr, name) 1768 #include "sanitizer_common/sanitizer_common_interceptors.inc" 1769 1770 namespace __tsan { 1771 1772 void ProcessPendingSignals(ThreadState *thr) { 1773 CHECK_EQ(thr->in_rtl, 0); 1774 SignalContext *sctx = SigCtx(thr); 1775 if (sctx == 0 || sctx->pending_signal_count == 0 || thr->in_signal_handler) 1776 return; 1777 Context *ctx = CTX(); 1778 thr->in_signal_handler = true; 1779 sctx->pending_signal_count = 0; 1780 // These are too big for stack. 1781 static THREADLOCAL sigset_t emptyset, oldset; 1782 sigfillset(&emptyset); 1783 pthread_sigmask(SIG_SETMASK, &emptyset, &oldset); 1784 for (int sig = 0; sig < kSigCount; sig++) { 1785 SignalDesc *signal = &sctx->pending_signals[sig]; 1786 if (signal->armed) { 1787 signal->armed = false; 1788 if (sigactions[sig].sa_handler != SIG_DFL 1789 && sigactions[sig].sa_handler != SIG_IGN) { 1790 // Insure that the handler does not spoil errno. 1791 const int saved_errno = errno; 1792 errno = 0; 1793 if (signal->sigaction) 1794 sigactions[sig].sa_sigaction(sig, &signal->siginfo, &signal->ctx); 1795 else 1796 sigactions[sig].sa_handler(sig); 1797 if (flags()->report_bugs && errno != 0) { 1798 ScopedInRtl in_rtl; 1799 __tsan::StackTrace stack; 1800 uptr pc = signal->sigaction ? 1801 (uptr)sigactions[sig].sa_sigaction : 1802 (uptr)sigactions[sig].sa_handler; 1803 stack.Init(&pc, 1); 1804 ThreadRegistryLock l(ctx->thread_registry); 1805 ScopedReport rep(ReportTypeErrnoInSignal); 1806 if (!IsFiredSuppression(ctx, rep, stack)) { 1807 rep.AddStack(&stack); 1808 OutputReport(ctx, rep, rep.GetReport()->stacks[0]); 1809 } 1810 } 1811 errno = saved_errno; 1812 } 1813 } 1814 } 1815 pthread_sigmask(SIG_SETMASK, &oldset, 0); 1816 CHECK_EQ(thr->in_signal_handler, true); 1817 thr->in_signal_handler = false; 1818 } 1819 1820 static void unreachable() { 1821 Printf("FATAL: ThreadSanitizer: unreachable called\n"); 1822 Die(); 1823 } 1824 1825 void InitializeInterceptors() { 1826 CHECK_GT(cur_thread()->in_rtl, 0); 1827 1828 // We need to setup it early, because functions like dlsym() can call it. 1829 REAL(memset) = internal_memset; 1830 REAL(memcpy) = internal_memcpy; 1831 REAL(memcmp) = internal_memcmp; 1832 1833 SANITIZER_COMMON_INTERCEPTORS_INIT; 1834 1835 TSAN_INTERCEPT(longjmp); 1836 TSAN_INTERCEPT(siglongjmp); 1837 1838 TSAN_INTERCEPT(malloc); 1839 TSAN_INTERCEPT(__libc_memalign); 1840 TSAN_INTERCEPT(calloc); 1841 TSAN_INTERCEPT(realloc); 1842 TSAN_INTERCEPT(free); 1843 TSAN_INTERCEPT(cfree); 1844 TSAN_INTERCEPT(mmap); 1845 TSAN_INTERCEPT(mmap64); 1846 TSAN_INTERCEPT(munmap); 1847 TSAN_INTERCEPT(memalign); 1848 TSAN_INTERCEPT(valloc); 1849 TSAN_INTERCEPT(pvalloc); 1850 TSAN_INTERCEPT(posix_memalign); 1851 1852 TSAN_INTERCEPT(strlen); 1853 TSAN_INTERCEPT(memset); 1854 TSAN_INTERCEPT(memcpy); 1855 TSAN_INTERCEPT(strcmp); 1856 TSAN_INTERCEPT(memchr); 1857 TSAN_INTERCEPT(memrchr); 1858 TSAN_INTERCEPT(memmove); 1859 TSAN_INTERCEPT(memcmp); 1860 TSAN_INTERCEPT(strchr); 1861 TSAN_INTERCEPT(strchrnul); 1862 TSAN_INTERCEPT(strrchr); 1863 TSAN_INTERCEPT(strncmp); 1864 TSAN_INTERCEPT(strcpy); // NOLINT 1865 TSAN_INTERCEPT(strncpy); 1866 TSAN_INTERCEPT(strstr); 1867 1868 TSAN_INTERCEPT(pthread_create); 1869 TSAN_INTERCEPT(pthread_join); 1870 TSAN_INTERCEPT(pthread_detach); 1871 1872 TSAN_INTERCEPT(pthread_mutex_init); 1873 TSAN_INTERCEPT(pthread_mutex_destroy); 1874 TSAN_INTERCEPT(pthread_mutex_lock); 1875 TSAN_INTERCEPT(pthread_mutex_trylock); 1876 TSAN_INTERCEPT(pthread_mutex_timedlock); 1877 TSAN_INTERCEPT(pthread_mutex_unlock); 1878 1879 TSAN_INTERCEPT(pthread_spin_init); 1880 TSAN_INTERCEPT(pthread_spin_destroy); 1881 TSAN_INTERCEPT(pthread_spin_lock); 1882 TSAN_INTERCEPT(pthread_spin_trylock); 1883 TSAN_INTERCEPT(pthread_spin_unlock); 1884 1885 TSAN_INTERCEPT(pthread_rwlock_init); 1886 TSAN_INTERCEPT(pthread_rwlock_destroy); 1887 TSAN_INTERCEPT(pthread_rwlock_rdlock); 1888 TSAN_INTERCEPT(pthread_rwlock_tryrdlock); 1889 TSAN_INTERCEPT(pthread_rwlock_timedrdlock); 1890 TSAN_INTERCEPT(pthread_rwlock_wrlock); 1891 TSAN_INTERCEPT(pthread_rwlock_trywrlock); 1892 TSAN_INTERCEPT(pthread_rwlock_timedwrlock); 1893 TSAN_INTERCEPT(pthread_rwlock_unlock); 1894 1895 // TSAN_INTERCEPT(pthread_cond_init); 1896 TSAN_INTERCEPT(pthread_cond_destroy); 1897 TSAN_INTERCEPT(pthread_cond_signal); 1898 TSAN_INTERCEPT(pthread_cond_broadcast); 1899 TSAN_INTERCEPT(pthread_cond_wait); 1900 TSAN_INTERCEPT(pthread_cond_timedwait); 1901 1902 TSAN_INTERCEPT(pthread_barrier_init); 1903 TSAN_INTERCEPT(pthread_barrier_destroy); 1904 TSAN_INTERCEPT(pthread_barrier_wait); 1905 1906 TSAN_INTERCEPT(pthread_once); 1907 1908 TSAN_INTERCEPT(sem_init); 1909 TSAN_INTERCEPT(sem_destroy); 1910 TSAN_INTERCEPT(sem_wait); 1911 TSAN_INTERCEPT(sem_trywait); 1912 TSAN_INTERCEPT(sem_timedwait); 1913 TSAN_INTERCEPT(sem_post); 1914 TSAN_INTERCEPT(sem_getvalue); 1915 1916 TSAN_INTERCEPT(stat); 1917 TSAN_INTERCEPT(__xstat); 1918 TSAN_INTERCEPT(stat64); 1919 TSAN_INTERCEPT(__xstat64); 1920 TSAN_INTERCEPT(lstat); 1921 TSAN_INTERCEPT(__lxstat); 1922 TSAN_INTERCEPT(lstat64); 1923 TSAN_INTERCEPT(__lxstat64); 1924 TSAN_INTERCEPT(fstat); 1925 TSAN_INTERCEPT(__fxstat); 1926 TSAN_INTERCEPT(fstat64); 1927 TSAN_INTERCEPT(__fxstat64); 1928 TSAN_INTERCEPT(open); 1929 TSAN_INTERCEPT(open64); 1930 TSAN_INTERCEPT(creat); 1931 TSAN_INTERCEPT(creat64); 1932 TSAN_INTERCEPT(dup); 1933 TSAN_INTERCEPT(dup2); 1934 TSAN_INTERCEPT(dup3); 1935 TSAN_INTERCEPT(eventfd); 1936 TSAN_INTERCEPT(signalfd); 1937 TSAN_INTERCEPT(inotify_init); 1938 TSAN_INTERCEPT(inotify_init1); 1939 TSAN_INTERCEPT(socket); 1940 TSAN_INTERCEPT(socketpair); 1941 TSAN_INTERCEPT(connect); 1942 TSAN_INTERCEPT(bind); 1943 TSAN_INTERCEPT(listen); 1944 TSAN_INTERCEPT(accept); 1945 TSAN_INTERCEPT(accept4); 1946 TSAN_INTERCEPT(epoll_create); 1947 TSAN_INTERCEPT(epoll_create1); 1948 TSAN_INTERCEPT(close); 1949 TSAN_INTERCEPT(__close); 1950 TSAN_INTERCEPT(__res_iclose); 1951 TSAN_INTERCEPT(pipe); 1952 TSAN_INTERCEPT(pipe2); 1953 1954 TSAN_INTERCEPT(readv); 1955 TSAN_INTERCEPT(preadv64); 1956 TSAN_INTERCEPT(writev); 1957 TSAN_INTERCEPT(pwritev64); 1958 TSAN_INTERCEPT(send); 1959 TSAN_INTERCEPT(sendmsg); 1960 TSAN_INTERCEPT(recv); 1961 TSAN_INTERCEPT(recvmsg); 1962 1963 TSAN_INTERCEPT(unlink); 1964 TSAN_INTERCEPT(fopen); 1965 TSAN_INTERCEPT(freopen); 1966 TSAN_INTERCEPT(fclose); 1967 TSAN_INTERCEPT(fread); 1968 TSAN_INTERCEPT(fwrite); 1969 TSAN_INTERCEPT(puts); 1970 TSAN_INTERCEPT(rmdir); 1971 TSAN_INTERCEPT(opendir); 1972 1973 TSAN_INTERCEPT(epoll_ctl); 1974 TSAN_INTERCEPT(epoll_wait); 1975 TSAN_INTERCEPT(poll); 1976 1977 TSAN_INTERCEPT(sigaction); 1978 TSAN_INTERCEPT(signal); 1979 TSAN_INTERCEPT(raise); 1980 TSAN_INTERCEPT(kill); 1981 TSAN_INTERCEPT(pthread_kill); 1982 TSAN_INTERCEPT(sleep); 1983 TSAN_INTERCEPT(usleep); 1984 TSAN_INTERCEPT(nanosleep); 1985 TSAN_INTERCEPT(gettimeofday); 1986 1987 TSAN_INTERCEPT(mlock); 1988 TSAN_INTERCEPT(munlock); 1989 TSAN_INTERCEPT(mlockall); 1990 TSAN_INTERCEPT(munlockall); 1991 1992 TSAN_INTERCEPT(fork); 1993 TSAN_INTERCEPT(on_exit); 1994 TSAN_INTERCEPT(__cxa_atexit); 1995 1996 // Need to setup it, because interceptors check that the function is resolved. 1997 // But atexit is emitted directly into the module, so can't be resolved. 1998 REAL(atexit) = (int(*)(void(*)()))unreachable; 1999 atexit_ctx = new(internal_alloc(MBlockAtExit, sizeof(AtExitContext))) 2000 AtExitContext(); 2001 2002 if (REAL(__cxa_atexit)(&finalize, 0, 0)) { 2003 Printf("ThreadSanitizer: failed to setup atexit callback\n"); 2004 Die(); 2005 } 2006 2007 if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) { 2008 Printf("ThreadSanitizer: failed to create thread key\n"); 2009 Die(); 2010 } 2011 2012 FdInit(); 2013 } 2014 2015 void internal_start_thread(void(*func)(void *arg), void *arg) { 2016 void *th; 2017 REAL(pthread_create)(&th, 0, (void*(*)(void *arg))func, arg); 2018 REAL(pthread_detach)(th); 2019 } 2020 2021 } // namespace __tsan 2022