tsan_interceptors.cc revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
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.inc 14//===----------------------------------------------------------------------===// 15 16#include "sanitizer_common/sanitizer_atomic.h" 17#include "sanitizer_common/sanitizer_libc.h" 18#include "sanitizer_common/sanitizer_linux.h" 19#include "sanitizer_common/sanitizer_platform_limits_posix.h" 20#include "sanitizer_common/sanitizer_placement_new.h" 21#include "sanitizer_common/sanitizer_stacktrace.h" 22#include "interception/interception.h" 23#include "tsan_interface.h" 24#include "tsan_platform.h" 25#include "tsan_suppressions.h" 26#include "tsan_rtl.h" 27#include "tsan_mman.h" 28#include "tsan_fd.h" 29 30using namespace __tsan; // NOLINT 31 32const int kSigCount = 65; 33 34struct my_siginfo_t { 35 // The size is determined by looking at sizeof of real siginfo_t on linux. 36 u64 opaque[128 / sizeof(u64)]; 37}; 38 39struct ucontext_t { 40 // The size is determined by looking at sizeof of real ucontext_t on linux. 41 u64 opaque[936 / sizeof(u64) + 1]; 42}; 43 44extern "C" int pthread_attr_init(void *attr); 45extern "C" int pthread_attr_destroy(void *attr); 46DECLARE_REAL(int, pthread_attr_getdetachstate, void *, void *) 47extern "C" int pthread_attr_setstacksize(void *attr, uptr stacksize); 48extern "C" int pthread_key_create(unsigned *key, void (*destructor)(void* v)); 49extern "C" int pthread_setspecific(unsigned key, const void *v); 50extern "C" int pthread_mutexattr_gettype(void *a, int *type); 51extern "C" int pthread_yield(); 52extern "C" int pthread_sigmask(int how, const __sanitizer_sigset_t *set, 53 __sanitizer_sigset_t *oldset); 54// REAL(sigfillset) defined in common interceptors. 55DECLARE_REAL(int, sigfillset, __sanitizer_sigset_t *set) 56DECLARE_REAL(int, fflush, __sanitizer_FILE *fp) 57extern "C" void *pthread_self(); 58extern "C" void _exit(int status); 59extern "C" int *__errno_location(); 60extern "C" int fileno_unlocked(void *stream); 61extern "C" void *__libc_malloc(uptr size); 62extern "C" void *__libc_calloc(uptr size, uptr n); 63extern "C" void *__libc_realloc(void *ptr, uptr size); 64extern "C" void __libc_free(void *ptr); 65extern "C" int mallopt(int param, int value); 66extern __sanitizer_FILE *stdout, *stderr; 67const int PTHREAD_MUTEX_RECURSIVE = 1; 68const int PTHREAD_MUTEX_RECURSIVE_NP = 1; 69const int EINVAL = 22; 70const int EBUSY = 16; 71const int EOWNERDEAD = 130; 72const int EPOLL_CTL_ADD = 1; 73const int SIGILL = 4; 74const int SIGABRT = 6; 75const int SIGFPE = 8; 76const int SIGSEGV = 11; 77const int SIGPIPE = 13; 78const int SIGTERM = 15; 79const int SIGBUS = 7; 80const int SIGSYS = 31; 81void *const MAP_FAILED = (void*)-1; 82const int PTHREAD_BARRIER_SERIAL_THREAD = -1; 83const int MAP_FIXED = 0x10; 84typedef long long_t; // NOLINT 85 86// From /usr/include/unistd.h 87# define F_ULOCK 0 /* Unlock a previously locked region. */ 88# define F_LOCK 1 /* Lock a region for exclusive use. */ 89# define F_TLOCK 2 /* Test and lock a region for exclusive use. */ 90# define F_TEST 3 /* Test a region for other processes locks. */ 91 92typedef void (*sighandler_t)(int sig); 93 94#define errno (*__errno_location()) 95 96struct sigaction_t { 97 union { 98 sighandler_t sa_handler; 99 void (*sa_sigaction)(int sig, my_siginfo_t *siginfo, void *uctx); 100 }; 101 __sanitizer_sigset_t sa_mask; 102 int sa_flags; 103 void (*sa_restorer)(); 104}; 105 106const sighandler_t SIG_DFL = (sighandler_t)0; 107const sighandler_t SIG_IGN = (sighandler_t)1; 108const sighandler_t SIG_ERR = (sighandler_t)-1; 109const int SA_SIGINFO = 4; 110const int SIG_SETMASK = 2; 111 112namespace std { 113struct nothrow_t {}; 114} // namespace std 115 116static sigaction_t sigactions[kSigCount]; 117 118namespace __tsan { 119struct SignalDesc { 120 bool armed; 121 bool sigaction; 122 my_siginfo_t siginfo; 123 ucontext_t ctx; 124}; 125 126struct SignalContext { 127 int in_blocking_func; 128 int int_signal_send; 129 int pending_signal_count; 130 SignalDesc pending_signals[kSigCount]; 131}; 132 133// The object is 64-byte aligned, because we want hot data to be located in 134// a single cache line if possible (it's accessed in every interceptor). 135static ALIGNED(64) char libignore_placeholder[sizeof(LibIgnore)]; 136static LibIgnore *libignore() { 137 return reinterpret_cast<LibIgnore*>(&libignore_placeholder[0]); 138} 139 140void InitializeLibIgnore() { 141 libignore()->Init(*GetSuppressionContext()); 142 libignore()->OnLibraryLoaded(0); 143} 144 145} // namespace __tsan 146 147static SignalContext *SigCtx(ThreadState *thr) { 148 SignalContext *ctx = (SignalContext*)thr->signal_ctx; 149 if (ctx == 0 && thr->is_alive) { 150 ctx = (SignalContext*)MmapOrDie(sizeof(*ctx), "SignalContext"); 151 MemoryResetRange(thr, (uptr)&SigCtx, (uptr)ctx, sizeof(*ctx)); 152 thr->signal_ctx = ctx; 153 } 154 return ctx; 155} 156 157static unsigned g_thread_finalize_key; 158 159class ScopedInterceptor { 160 public: 161 ScopedInterceptor(ThreadState *thr, const char *fname, uptr pc); 162 ~ScopedInterceptor(); 163 private: 164 ThreadState *const thr_; 165 const uptr pc_; 166 bool in_ignored_lib_; 167}; 168 169ScopedInterceptor::ScopedInterceptor(ThreadState *thr, const char *fname, 170 uptr pc) 171 : thr_(thr) 172 , pc_(pc) 173 , in_ignored_lib_(false) { 174 if (!thr_->ignore_interceptors) { 175 Initialize(thr); 176 FuncEntry(thr, pc); 177 } 178 DPrintf("#%d: intercept %s()\n", thr_->tid, fname); 179 if (!thr_->in_ignored_lib && libignore()->IsIgnored(pc)) { 180 in_ignored_lib_ = true; 181 thr_->in_ignored_lib = true; 182 ThreadIgnoreBegin(thr_, pc_); 183 } 184} 185 186ScopedInterceptor::~ScopedInterceptor() { 187 if (in_ignored_lib_) { 188 thr_->in_ignored_lib = false; 189 ThreadIgnoreEnd(thr_, pc_); 190 } 191 if (!thr_->ignore_interceptors) { 192 ProcessPendingSignals(thr_); 193 FuncExit(thr_); 194 } 195} 196 197#define SCOPED_INTERCEPTOR_RAW(func, ...) \ 198 ThreadState *thr = cur_thread(); \ 199 const uptr caller_pc = GET_CALLER_PC(); \ 200 ScopedInterceptor si(thr, #func, caller_pc); \ 201 const uptr pc = __sanitizer::StackTrace::GetCurrentPc(); \ 202 (void)pc; \ 203/**/ 204 205#define SCOPED_TSAN_INTERCEPTOR(func, ...) \ 206 SCOPED_INTERCEPTOR_RAW(func, __VA_ARGS__); \ 207 if (REAL(func) == 0) { \ 208 Report("FATAL: ThreadSanitizer: failed to intercept %s\n", #func); \ 209 Die(); \ 210 } \ 211 if (thr->ignore_interceptors || thr->in_ignored_lib) \ 212 return REAL(func)(__VA_ARGS__); \ 213/**/ 214 215#define TSAN_INTERCEPTOR(ret, func, ...) INTERCEPTOR(ret, func, __VA_ARGS__) 216#define TSAN_INTERCEPT(func) INTERCEPT_FUNCTION(func) 217#define TSAN_INTERCEPT_VER(func, ver) INTERCEPT_FUNCTION_VER(func, ver) 218 219#define BLOCK_REAL(name) (BlockingCall(thr), REAL(name)) 220 221struct BlockingCall { 222 explicit BlockingCall(ThreadState *thr) 223 : ctx(SigCtx(thr)) { 224 ctx->in_blocking_func++; 225 } 226 227 ~BlockingCall() { 228 ctx->in_blocking_func--; 229 } 230 231 SignalContext *ctx; 232 233 // When we are in a "blocking call", we process signals asynchronously 234 // (right when they arrive). In this context we do not expect to be 235 // executing any user/runtime code. The known interceptor sequence when 236 // this is not true is: pthread_join -> munmap(stack). It's fine 237 // to ignore munmap in this case -- we handle stack shadow separately. 238 ScopedIgnoreInterceptors ignore_interceptors; 239}; 240 241TSAN_INTERCEPTOR(unsigned, sleep, unsigned sec) { 242 SCOPED_TSAN_INTERCEPTOR(sleep, sec); 243 unsigned res = BLOCK_REAL(sleep)(sec); 244 AfterSleep(thr, pc); 245 return res; 246} 247 248TSAN_INTERCEPTOR(int, usleep, long_t usec) { 249 SCOPED_TSAN_INTERCEPTOR(usleep, usec); 250 int res = BLOCK_REAL(usleep)(usec); 251 AfterSleep(thr, pc); 252 return res; 253} 254 255TSAN_INTERCEPTOR(int, nanosleep, void *req, void *rem) { 256 SCOPED_TSAN_INTERCEPTOR(nanosleep, req, rem); 257 int res = BLOCK_REAL(nanosleep)(req, rem); 258 AfterSleep(thr, pc); 259 return res; 260} 261 262class AtExitContext { 263 public: 264 AtExitContext() 265 : mtx_(MutexTypeAtExit, StatMtxAtExit) 266 , pos_() { 267 } 268 269 typedef void(*atexit_t)(); 270 271 int atexit(ThreadState *thr, uptr pc, bool is_on_exit, 272 atexit_t f, void *arg) { 273 Lock l(&mtx_); 274 if (pos_ == kMaxAtExit) 275 return 1; 276 Release(thr, pc, (uptr)this); 277 stack_[pos_] = f; 278 args_[pos_] = arg; 279 is_on_exits_[pos_] = is_on_exit; 280 pos_++; 281 return 0; 282 } 283 284 void exit(ThreadState *thr, uptr pc) { 285 for (;;) { 286 atexit_t f = 0; 287 void *arg = 0; 288 bool is_on_exit = false; 289 { 290 Lock l(&mtx_); 291 if (pos_) { 292 pos_--; 293 f = stack_[pos_]; 294 arg = args_[pos_]; 295 is_on_exit = is_on_exits_[pos_]; 296 Acquire(thr, pc, (uptr)this); 297 } 298 } 299 if (f == 0) 300 break; 301 DPrintf("#%d: executing atexit func %p\n", thr->tid, f); 302 if (is_on_exit) 303 ((void(*)(int status, void *arg))f)(0, arg); 304 else 305 ((void(*)(void *arg, void *dso))f)(arg, 0); 306 } 307 } 308 309 private: 310 static const int kMaxAtExit = 128; 311 Mutex mtx_; 312 atexit_t stack_[kMaxAtExit]; 313 void *args_[kMaxAtExit]; 314 bool is_on_exits_[kMaxAtExit]; 315 int pos_; 316}; 317 318static AtExitContext *atexit_ctx; 319 320TSAN_INTERCEPTOR(int, atexit, void (*f)()) { 321 if (cur_thread()->in_symbolizer) 322 return 0; 323 // We want to setup the atexit callback even if we are in ignored lib 324 // or after fork. 325 SCOPED_INTERCEPTOR_RAW(atexit, f); 326 return atexit_ctx->atexit(thr, pc, false, (void(*)())f, 0); 327} 328 329TSAN_INTERCEPTOR(int, on_exit, void(*f)(int, void*), void *arg) { 330 if (cur_thread()->in_symbolizer) 331 return 0; 332 SCOPED_TSAN_INTERCEPTOR(on_exit, f, arg); 333 return atexit_ctx->atexit(thr, pc, true, (void(*)())f, arg); 334} 335 336TSAN_INTERCEPTOR(int, __cxa_atexit, void (*f)(void *a), void *arg, void *dso) { 337 if (cur_thread()->in_symbolizer) 338 return 0; 339 SCOPED_TSAN_INTERCEPTOR(__cxa_atexit, f, arg, dso); 340 if (dso) { 341 // Memory allocation in __cxa_atexit will race with free during exit, 342 // because we do not see synchronization around atexit callback list. 343 ThreadIgnoreBegin(thr, pc); 344 int res = REAL(__cxa_atexit)(f, arg, dso); 345 ThreadIgnoreEnd(thr, pc); 346 return res; 347 } 348 return atexit_ctx->atexit(thr, pc, false, (void(*)())f, arg); 349} 350 351// Cleanup old bufs. 352static void JmpBufGarbageCollect(ThreadState *thr, uptr sp) { 353 for (uptr i = 0; i < thr->jmp_bufs.Size(); i++) { 354 JmpBuf *buf = &thr->jmp_bufs[i]; 355 if (buf->sp <= sp) { 356 uptr sz = thr->jmp_bufs.Size(); 357 thr->jmp_bufs[i] = thr->jmp_bufs[sz - 1]; 358 thr->jmp_bufs.PopBack(); 359 i--; 360 } 361 } 362} 363 364static void SetJmp(ThreadState *thr, uptr sp, uptr mangled_sp) { 365 if (thr->shadow_stack_pos == 0) // called from libc guts during bootstrap 366 return; 367 // Cleanup old bufs. 368 JmpBufGarbageCollect(thr, sp); 369 // Remember the buf. 370 JmpBuf *buf = thr->jmp_bufs.PushBack(); 371 buf->sp = sp; 372 buf->mangled_sp = mangled_sp; 373 buf->shadow_stack_pos = thr->shadow_stack_pos; 374} 375 376static void LongJmp(ThreadState *thr, uptr *env) { 377 uptr mangled_sp = env[6]; 378 // Find the saved buf by mangled_sp. 379 for (uptr i = 0; i < thr->jmp_bufs.Size(); i++) { 380 JmpBuf *buf = &thr->jmp_bufs[i]; 381 if (buf->mangled_sp == mangled_sp) { 382 CHECK_GE(thr->shadow_stack_pos, buf->shadow_stack_pos); 383 // Unwind the stack. 384 while (thr->shadow_stack_pos > buf->shadow_stack_pos) 385 FuncExit(thr); 386 JmpBufGarbageCollect(thr, buf->sp - 1); // do not collect buf->sp 387 return; 388 } 389 } 390 Printf("ThreadSanitizer: can't find longjmp buf\n"); 391 CHECK(0); 392} 393 394// FIXME: put everything below into a common extern "C" block? 395extern "C" void __tsan_setjmp(uptr sp, uptr mangled_sp) { 396 SetJmp(cur_thread(), sp, mangled_sp); 397} 398 399// Not called. Merely to satisfy TSAN_INTERCEPT(). 400extern "C" SANITIZER_INTERFACE_ATTRIBUTE 401int __interceptor_setjmp(void *env); 402extern "C" int __interceptor_setjmp(void *env) { 403 CHECK(0); 404 return 0; 405} 406 407// FIXME: any reason to have a separate declaration? 408extern "C" SANITIZER_INTERFACE_ATTRIBUTE 409int __interceptor__setjmp(void *env); 410extern "C" int __interceptor__setjmp(void *env) { 411 CHECK(0); 412 return 0; 413} 414 415extern "C" SANITIZER_INTERFACE_ATTRIBUTE 416int __interceptor_sigsetjmp(void *env); 417extern "C" int __interceptor_sigsetjmp(void *env) { 418 CHECK(0); 419 return 0; 420} 421 422extern "C" SANITIZER_INTERFACE_ATTRIBUTE 423int __interceptor___sigsetjmp(void *env); 424extern "C" int __interceptor___sigsetjmp(void *env) { 425 CHECK(0); 426 return 0; 427} 428 429extern "C" int setjmp(void *env); 430extern "C" int _setjmp(void *env); 431extern "C" int sigsetjmp(void *env); 432extern "C" int __sigsetjmp(void *env); 433DEFINE_REAL(int, setjmp, void *env) 434DEFINE_REAL(int, _setjmp, void *env) 435DEFINE_REAL(int, sigsetjmp, void *env) 436DEFINE_REAL(int, __sigsetjmp, void *env) 437 438TSAN_INTERCEPTOR(void, longjmp, uptr *env, int val) { 439 { 440 SCOPED_TSAN_INTERCEPTOR(longjmp, env, val); 441 } 442 LongJmp(cur_thread(), env); 443 REAL(longjmp)(env, val); 444} 445 446TSAN_INTERCEPTOR(void, siglongjmp, uptr *env, int val) { 447 { 448 SCOPED_TSAN_INTERCEPTOR(siglongjmp, env, val); 449 } 450 LongJmp(cur_thread(), env); 451 REAL(siglongjmp)(env, val); 452} 453 454TSAN_INTERCEPTOR(void*, malloc, uptr size) { 455 if (cur_thread()->in_symbolizer) 456 return __libc_malloc(size); 457 void *p = 0; 458 { 459 SCOPED_INTERCEPTOR_RAW(malloc, size); 460 p = user_alloc(thr, pc, size); 461 } 462 invoke_malloc_hook(p, size); 463 return p; 464} 465 466TSAN_INTERCEPTOR(void*, __libc_memalign, uptr align, uptr sz) { 467 SCOPED_TSAN_INTERCEPTOR(__libc_memalign, align, sz); 468 return user_alloc(thr, pc, sz, align); 469} 470 471TSAN_INTERCEPTOR(void*, calloc, uptr size, uptr n) { 472 if (cur_thread()->in_symbolizer) 473 return __libc_calloc(size, n); 474 if (__sanitizer::CallocShouldReturnNullDueToOverflow(size, n)) 475 return AllocatorReturnNull(); 476 void *p = 0; 477 { 478 SCOPED_INTERCEPTOR_RAW(calloc, size, n); 479 p = user_alloc(thr, pc, n * size); 480 if (p) 481 internal_memset(p, 0, n * size); 482 } 483 invoke_malloc_hook(p, n * size); 484 return p; 485} 486 487TSAN_INTERCEPTOR(void*, realloc, void *p, uptr size) { 488 if (cur_thread()->in_symbolizer) 489 return __libc_realloc(p, size); 490 if (p) 491 invoke_free_hook(p); 492 { 493 SCOPED_INTERCEPTOR_RAW(realloc, p, size); 494 p = user_realloc(thr, pc, p, size); 495 } 496 invoke_malloc_hook(p, size); 497 return p; 498} 499 500TSAN_INTERCEPTOR(void, free, void *p) { 501 if (p == 0) 502 return; 503 if (cur_thread()->in_symbolizer) 504 return __libc_free(p); 505 invoke_free_hook(p); 506 SCOPED_INTERCEPTOR_RAW(free, p); 507 user_free(thr, pc, p); 508} 509 510TSAN_INTERCEPTOR(void, cfree, void *p) { 511 if (p == 0) 512 return; 513 if (cur_thread()->in_symbolizer) 514 return __libc_free(p); 515 invoke_free_hook(p); 516 SCOPED_INTERCEPTOR_RAW(cfree, p); 517 user_free(thr, pc, p); 518} 519 520TSAN_INTERCEPTOR(uptr, malloc_usable_size, void *p) { 521 SCOPED_INTERCEPTOR_RAW(malloc_usable_size, p); 522 return user_alloc_usable_size(thr, pc, p); 523} 524 525#define OPERATOR_NEW_BODY(mangled_name) \ 526 if (cur_thread()->in_symbolizer) \ 527 return __libc_malloc(size); \ 528 void *p = 0; \ 529 { \ 530 SCOPED_INTERCEPTOR_RAW(mangled_name, size); \ 531 p = user_alloc(thr, pc, size); \ 532 } \ 533 invoke_malloc_hook(p, size); \ 534 return p; 535 536SANITIZER_INTERFACE_ATTRIBUTE 537void *operator new(__sanitizer::uptr size); 538void *operator new(__sanitizer::uptr size) { 539 OPERATOR_NEW_BODY(_Znwm); 540} 541 542SANITIZER_INTERFACE_ATTRIBUTE 543void *operator new[](__sanitizer::uptr size); 544void *operator new[](__sanitizer::uptr size) { 545 OPERATOR_NEW_BODY(_Znam); 546} 547 548SANITIZER_INTERFACE_ATTRIBUTE 549void *operator new(__sanitizer::uptr size, std::nothrow_t const&); 550void *operator new(__sanitizer::uptr size, std::nothrow_t const&) { 551 OPERATOR_NEW_BODY(_ZnwmRKSt9nothrow_t); 552} 553 554SANITIZER_INTERFACE_ATTRIBUTE 555void *operator new[](__sanitizer::uptr size, std::nothrow_t const&); 556void *operator new[](__sanitizer::uptr size, std::nothrow_t const&) { 557 OPERATOR_NEW_BODY(_ZnamRKSt9nothrow_t); 558} 559 560#define OPERATOR_DELETE_BODY(mangled_name) \ 561 if (ptr == 0) return; \ 562 if (cur_thread()->in_symbolizer) \ 563 return __libc_free(ptr); \ 564 invoke_free_hook(ptr); \ 565 SCOPED_INTERCEPTOR_RAW(mangled_name, ptr); \ 566 user_free(thr, pc, ptr); 567 568SANITIZER_INTERFACE_ATTRIBUTE 569void operator delete(void *ptr) throw(); 570void operator delete(void *ptr) throw() { 571 OPERATOR_DELETE_BODY(_ZdlPv); 572} 573 574SANITIZER_INTERFACE_ATTRIBUTE 575void operator delete[](void *ptr) throw(); 576void operator delete[](void *ptr) throw() { 577 OPERATOR_DELETE_BODY(_ZdaPv); 578} 579 580SANITIZER_INTERFACE_ATTRIBUTE 581void operator delete(void *ptr, std::nothrow_t const&); 582void operator delete(void *ptr, std::nothrow_t const&) { 583 OPERATOR_DELETE_BODY(_ZdlPvRKSt9nothrow_t); 584} 585 586SANITIZER_INTERFACE_ATTRIBUTE 587void operator delete[](void *ptr, std::nothrow_t const&); 588void operator delete[](void *ptr, std::nothrow_t const&) { 589 OPERATOR_DELETE_BODY(_ZdaPvRKSt9nothrow_t); 590} 591 592TSAN_INTERCEPTOR(uptr, strlen, const char *s) { 593 SCOPED_TSAN_INTERCEPTOR(strlen, s); 594 uptr len = internal_strlen(s); 595 MemoryAccessRange(thr, pc, (uptr)s, len + 1, false); 596 return len; 597} 598 599TSAN_INTERCEPTOR(void*, memset, void *dst, int v, uptr size) { 600 SCOPED_TSAN_INTERCEPTOR(memset, dst, v, size); 601 MemoryAccessRange(thr, pc, (uptr)dst, size, true); 602 return internal_memset(dst, v, size); 603} 604 605TSAN_INTERCEPTOR(void*, memcpy, void *dst, const void *src, uptr size) { 606 SCOPED_TSAN_INTERCEPTOR(memcpy, dst, src, size); 607 MemoryAccessRange(thr, pc, (uptr)dst, size, true); 608 MemoryAccessRange(thr, pc, (uptr)src, size, false); 609 return internal_memcpy(dst, src, size); 610} 611 612TSAN_INTERCEPTOR(int, memcmp, const void *s1, const void *s2, uptr n) { 613 SCOPED_TSAN_INTERCEPTOR(memcmp, s1, s2, n); 614 int res = 0; 615 uptr len = 0; 616 for (; len < n; len++) { 617 if ((res = ((unsigned char*)s1)[len] - ((unsigned char*)s2)[len])) 618 break; 619 } 620 MemoryAccessRange(thr, pc, (uptr)s1, len < n ? len + 1 : n, false); 621 MemoryAccessRange(thr, pc, (uptr)s2, len < n ? len + 1 : n, false); 622 return res; 623} 624 625TSAN_INTERCEPTOR(void*, memmove, void *dst, void *src, uptr n) { 626 SCOPED_TSAN_INTERCEPTOR(memmove, dst, src, n); 627 MemoryAccessRange(thr, pc, (uptr)dst, n, true); 628 MemoryAccessRange(thr, pc, (uptr)src, n, false); 629 return REAL(memmove)(dst, src, n); 630} 631 632TSAN_INTERCEPTOR(char*, strchr, char *s, int c) { 633 SCOPED_TSAN_INTERCEPTOR(strchr, s, c); 634 char *res = REAL(strchr)(s, c); 635 uptr len = res ? (char*)res - (char*)s + 1 : internal_strlen(s) + 1; 636 MemoryAccessRange(thr, pc, (uptr)s, len, false); 637 return res; 638} 639 640TSAN_INTERCEPTOR(char*, strchrnul, char *s, int c) { 641 SCOPED_TSAN_INTERCEPTOR(strchrnul, s, c); 642 char *res = REAL(strchrnul)(s, c); 643 uptr len = (char*)res - (char*)s + 1; 644 MemoryAccessRange(thr, pc, (uptr)s, len, false); 645 return res; 646} 647 648TSAN_INTERCEPTOR(char*, strrchr, char *s, int c) { 649 SCOPED_TSAN_INTERCEPTOR(strrchr, s, c); 650 MemoryAccessRange(thr, pc, (uptr)s, internal_strlen(s) + 1, false); 651 return REAL(strrchr)(s, c); 652} 653 654TSAN_INTERCEPTOR(char*, strcpy, char *dst, const char *src) { // NOLINT 655 SCOPED_TSAN_INTERCEPTOR(strcpy, dst, src); // NOLINT 656 uptr srclen = internal_strlen(src); 657 MemoryAccessRange(thr, pc, (uptr)dst, srclen + 1, true); 658 MemoryAccessRange(thr, pc, (uptr)src, srclen + 1, false); 659 return REAL(strcpy)(dst, src); // NOLINT 660} 661 662TSAN_INTERCEPTOR(char*, strncpy, char *dst, char *src, uptr n) { 663 SCOPED_TSAN_INTERCEPTOR(strncpy, dst, src, n); 664 uptr srclen = internal_strnlen(src, n); 665 MemoryAccessRange(thr, pc, (uptr)dst, n, true); 666 MemoryAccessRange(thr, pc, (uptr)src, min(srclen + 1, n), false); 667 return REAL(strncpy)(dst, src, n); 668} 669 670TSAN_INTERCEPTOR(const char*, strstr, const char *s1, const char *s2) { 671 SCOPED_TSAN_INTERCEPTOR(strstr, s1, s2); 672 const char *res = REAL(strstr)(s1, s2); 673 uptr len1 = internal_strlen(s1); 674 uptr len2 = internal_strlen(s2); 675 MemoryAccessRange(thr, pc, (uptr)s1, len1 + 1, false); 676 MemoryAccessRange(thr, pc, (uptr)s2, len2 + 1, false); 677 return res; 678} 679 680TSAN_INTERCEPTOR(char*, strdup, const char *str) { 681 SCOPED_TSAN_INTERCEPTOR(strdup, str); 682 // strdup will call malloc, so no instrumentation is required here. 683 return REAL(strdup)(str); 684} 685 686static bool fix_mmap_addr(void **addr, long_t sz, int flags) { 687 if (*addr) { 688 if (!IsAppMem((uptr)*addr) || !IsAppMem((uptr)*addr + sz - 1)) { 689 if (flags & MAP_FIXED) { 690 errno = EINVAL; 691 return false; 692 } else { 693 *addr = 0; 694 } 695 } 696 } 697 return true; 698} 699 700TSAN_INTERCEPTOR(void*, mmap, void *addr, long_t sz, int prot, 701 int flags, int fd, unsigned off) { 702 SCOPED_TSAN_INTERCEPTOR(mmap, addr, sz, prot, flags, fd, off); 703 if (!fix_mmap_addr(&addr, sz, flags)) 704 return MAP_FAILED; 705 void *res = REAL(mmap)(addr, sz, prot, flags, fd, off); 706 if (res != MAP_FAILED) { 707 if (fd > 0) 708 FdAccess(thr, pc, fd); 709 MemoryRangeImitateWrite(thr, pc, (uptr)res, sz); 710 } 711 return res; 712} 713 714TSAN_INTERCEPTOR(void*, mmap64, void *addr, long_t sz, int prot, 715 int flags, int fd, u64 off) { 716 SCOPED_TSAN_INTERCEPTOR(mmap64, addr, sz, prot, flags, fd, off); 717 if (!fix_mmap_addr(&addr, sz, flags)) 718 return MAP_FAILED; 719 void *res = REAL(mmap64)(addr, sz, prot, flags, fd, off); 720 if (res != MAP_FAILED) { 721 if (fd > 0) 722 FdAccess(thr, pc, fd); 723 MemoryRangeImitateWrite(thr, pc, (uptr)res, sz); 724 } 725 return res; 726} 727 728TSAN_INTERCEPTOR(int, munmap, void *addr, long_t sz) { 729 SCOPED_TSAN_INTERCEPTOR(munmap, addr, sz); 730 DontNeedShadowFor((uptr)addr, sz); 731 int res = REAL(munmap)(addr, sz); 732 return res; 733} 734 735TSAN_INTERCEPTOR(void*, memalign, uptr align, uptr sz) { 736 SCOPED_INTERCEPTOR_RAW(memalign, align, sz); 737 return user_alloc(thr, pc, sz, align); 738} 739 740TSAN_INTERCEPTOR(void*, valloc, uptr sz) { 741 SCOPED_INTERCEPTOR_RAW(valloc, sz); 742 return user_alloc(thr, pc, sz, GetPageSizeCached()); 743} 744 745TSAN_INTERCEPTOR(void*, pvalloc, uptr sz) { 746 SCOPED_INTERCEPTOR_RAW(pvalloc, sz); 747 sz = RoundUp(sz, GetPageSizeCached()); 748 return user_alloc(thr, pc, sz, GetPageSizeCached()); 749} 750 751TSAN_INTERCEPTOR(int, posix_memalign, void **memptr, uptr align, uptr sz) { 752 SCOPED_INTERCEPTOR_RAW(posix_memalign, memptr, align, sz); 753 *memptr = user_alloc(thr, pc, sz, align); 754 return 0; 755} 756 757// Used in thread-safe function static initialization. 758extern "C" int INTERFACE_ATTRIBUTE __cxa_guard_acquire(atomic_uint32_t *g) { 759 SCOPED_INTERCEPTOR_RAW(__cxa_guard_acquire, g); 760 for (;;) { 761 u32 cmp = atomic_load(g, memory_order_acquire); 762 if (cmp == 0) { 763 if (atomic_compare_exchange_strong(g, &cmp, 1<<16, memory_order_relaxed)) 764 return 1; 765 } else if (cmp == 1) { 766 Acquire(thr, pc, (uptr)g); 767 return 0; 768 } else { 769 internal_sched_yield(); 770 } 771 } 772} 773 774extern "C" void INTERFACE_ATTRIBUTE __cxa_guard_release(atomic_uint32_t *g) { 775 SCOPED_INTERCEPTOR_RAW(__cxa_guard_release, g); 776 Release(thr, pc, (uptr)g); 777 atomic_store(g, 1, memory_order_release); 778} 779 780extern "C" void INTERFACE_ATTRIBUTE __cxa_guard_abort(atomic_uint32_t *g) { 781 SCOPED_INTERCEPTOR_RAW(__cxa_guard_abort, g); 782 atomic_store(g, 0, memory_order_relaxed); 783} 784 785static void thread_finalize(void *v) { 786 uptr iter = (uptr)v; 787 if (iter > 1) { 788 if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) { 789 Printf("ThreadSanitizer: failed to set thread key\n"); 790 Die(); 791 } 792 return; 793 } 794 { 795 ThreadState *thr = cur_thread(); 796 ThreadFinish(thr); 797 SignalContext *sctx = thr->signal_ctx; 798 if (sctx) { 799 thr->signal_ctx = 0; 800 UnmapOrDie(sctx, sizeof(*sctx)); 801 } 802 } 803} 804 805 806struct ThreadParam { 807 void* (*callback)(void *arg); 808 void *param; 809 atomic_uintptr_t tid; 810}; 811 812extern "C" void *__tsan_thread_start_func(void *arg) { 813 ThreadParam *p = (ThreadParam*)arg; 814 void* (*callback)(void *arg) = p->callback; 815 void *param = p->param; 816 int tid = 0; 817 { 818 ThreadState *thr = cur_thread(); 819 // Thread-local state is not initialized yet. 820 ScopedIgnoreInterceptors ignore; 821 if (pthread_setspecific(g_thread_finalize_key, 822 (void *)kPthreadDestructorIterations)) { 823 Printf("ThreadSanitizer: failed to set thread key\n"); 824 Die(); 825 } 826 while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0) 827 pthread_yield(); 828 atomic_store(&p->tid, 0, memory_order_release); 829 ThreadStart(thr, tid, GetTid()); 830 } 831 void *res = callback(param); 832 // Prevent the callback from being tail called, 833 // it mixes up stack traces. 834 volatile int foo = 42; 835 foo++; 836 return res; 837} 838 839TSAN_INTERCEPTOR(int, pthread_create, 840 void *th, void *attr, void *(*callback)(void*), void * param) { 841 SCOPED_INTERCEPTOR_RAW(pthread_create, th, attr, callback, param); 842 if (ctx->after_multithreaded_fork) { 843 if (flags()->die_after_fork) { 844 Report("ThreadSanitizer: starting new threads after multi-threaded " 845 "fork is not supported. Dying (set die_after_fork=0 to override)\n"); 846 Die(); 847 } else { 848 VPrintf(1, "ThreadSanitizer: starting new threads after multi-threaded " 849 "fork is not supported (pid %d). Continuing because of " 850 "die_after_fork=0, but you are on your own\n", internal_getpid()); 851 } 852 } 853 __sanitizer_pthread_attr_t myattr; 854 if (attr == 0) { 855 pthread_attr_init(&myattr); 856 attr = &myattr; 857 } 858 int detached = 0; 859 REAL(pthread_attr_getdetachstate)(attr, &detached); 860 AdjustStackSize(attr); 861 862 ThreadParam p; 863 p.callback = callback; 864 p.param = param; 865 atomic_store(&p.tid, 0, memory_order_relaxed); 866 int res = -1; 867 { 868 // Otherwise we see false positives in pthread stack manipulation. 869 ScopedIgnoreInterceptors ignore; 870 ThreadIgnoreBegin(thr, pc); 871 res = REAL(pthread_create)(th, attr, __tsan_thread_start_func, &p); 872 ThreadIgnoreEnd(thr, pc); 873 } 874 if (res == 0) { 875 int tid = ThreadCreate(thr, pc, *(uptr*)th, detached); 876 CHECK_NE(tid, 0); 877 atomic_store(&p.tid, tid, memory_order_release); 878 while (atomic_load(&p.tid, memory_order_acquire) != 0) 879 pthread_yield(); 880 } 881 if (attr == &myattr) 882 pthread_attr_destroy(&myattr); 883 return res; 884} 885 886TSAN_INTERCEPTOR(int, pthread_join, void *th, void **ret) { 887 SCOPED_INTERCEPTOR_RAW(pthread_join, th, ret); 888 int tid = ThreadTid(thr, pc, (uptr)th); 889 ThreadIgnoreBegin(thr, pc); 890 int res = BLOCK_REAL(pthread_join)(th, ret); 891 ThreadIgnoreEnd(thr, pc); 892 if (res == 0) { 893 ThreadJoin(thr, pc, tid); 894 } 895 return res; 896} 897 898TSAN_INTERCEPTOR(int, pthread_detach, void *th) { 899 SCOPED_TSAN_INTERCEPTOR(pthread_detach, th); 900 int tid = ThreadTid(thr, pc, (uptr)th); 901 int res = REAL(pthread_detach)(th); 902 if (res == 0) { 903 ThreadDetach(thr, pc, tid); 904 } 905 return res; 906} 907 908// Problem: 909// NPTL implementation of pthread_cond has 2 versions (2.2.5 and 2.3.2). 910// pthread_cond_t has different size in the different versions. 911// If call new REAL functions for old pthread_cond_t, they will corrupt memory 912// after pthread_cond_t (old cond is smaller). 913// If we call old REAL functions for new pthread_cond_t, we will lose some 914// functionality (e.g. old functions do not support waiting against 915// CLOCK_REALTIME). 916// Proper handling would require to have 2 versions of interceptors as well. 917// But this is messy, in particular requires linker scripts when sanitizer 918// runtime is linked into a shared library. 919// Instead we assume we don't have dynamic libraries built against old 920// pthread (2.2.5 is dated by 2002). And provide legacy_pthread_cond flag 921// that allows to work with old libraries (but this mode does not support 922// some features, e.g. pthread_condattr_getpshared). 923static void *init_cond(void *c, bool force = false) { 924 // sizeof(pthread_cond_t) >= sizeof(uptr) in both versions. 925 // So we allocate additional memory on the side large enough to hold 926 // any pthread_cond_t object. Always call new REAL functions, but pass 927 // the aux object to them. 928 // Note: the code assumes that PTHREAD_COND_INITIALIZER initializes 929 // first word of pthread_cond_t to zero. 930 // It's all relevant only for linux. 931 if (!common_flags()->legacy_pthread_cond) 932 return c; 933 atomic_uintptr_t *p = (atomic_uintptr_t*)c; 934 uptr cond = atomic_load(p, memory_order_acquire); 935 if (!force && cond != 0) 936 return (void*)cond; 937 void *newcond = WRAP(malloc)(pthread_cond_t_sz); 938 internal_memset(newcond, 0, pthread_cond_t_sz); 939 if (atomic_compare_exchange_strong(p, &cond, (uptr)newcond, 940 memory_order_acq_rel)) 941 return newcond; 942 WRAP(free)(newcond); 943 return (void*)cond; 944} 945 946struct CondMutexUnlockCtx { 947 ThreadState *thr; 948 uptr pc; 949 void *m; 950}; 951 952static void cond_mutex_unlock(CondMutexUnlockCtx *arg) { 953 MutexLock(arg->thr, arg->pc, (uptr)arg->m); 954} 955 956INTERCEPTOR(int, pthread_cond_init, void *c, void *a) { 957 void *cond = init_cond(c, true); 958 SCOPED_TSAN_INTERCEPTOR(pthread_cond_init, cond, a); 959 MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), true); 960 return REAL(pthread_cond_init)(cond, a); 961} 962 963INTERCEPTOR(int, pthread_cond_wait, void *c, void *m) { 964 void *cond = init_cond(c); 965 SCOPED_TSAN_INTERCEPTOR(pthread_cond_wait, cond, m); 966 MutexUnlock(thr, pc, (uptr)m); 967 MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), false); 968 CondMutexUnlockCtx arg = {thr, pc, m}; 969 // This ensures that we handle mutex lock even in case of pthread_cancel. 970 // See test/tsan/cond_cancel.cc. 971 int res = call_pthread_cancel_with_cleanup( 972 (int(*)(void *c, void *m, void *abstime))REAL(pthread_cond_wait), 973 cond, m, 0, (void(*)(void *arg))cond_mutex_unlock, &arg); 974 if (res == errno_EOWNERDEAD) 975 MutexRepair(thr, pc, (uptr)m); 976 MutexLock(thr, pc, (uptr)m); 977 return res; 978} 979 980INTERCEPTOR(int, pthread_cond_timedwait, void *c, void *m, void *abstime) { 981 void *cond = init_cond(c); 982 SCOPED_TSAN_INTERCEPTOR(pthread_cond_timedwait, cond, m, abstime); 983 MutexUnlock(thr, pc, (uptr)m); 984 MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), false); 985 CondMutexUnlockCtx arg = {thr, pc, m}; 986 // This ensures that we handle mutex lock even in case of pthread_cancel. 987 // See test/tsan/cond_cancel.cc. 988 int res = call_pthread_cancel_with_cleanup( 989 REAL(pthread_cond_timedwait), cond, m, abstime, 990 (void(*)(void *arg))cond_mutex_unlock, &arg); 991 if (res == errno_EOWNERDEAD) 992 MutexRepair(thr, pc, (uptr)m); 993 MutexLock(thr, pc, (uptr)m); 994 return res; 995} 996 997INTERCEPTOR(int, pthread_cond_signal, void *c) { 998 void *cond = init_cond(c); 999 SCOPED_TSAN_INTERCEPTOR(pthread_cond_signal, cond); 1000 MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), false); 1001 return REAL(pthread_cond_signal)(cond); 1002} 1003 1004INTERCEPTOR(int, pthread_cond_broadcast, void *c) { 1005 void *cond = init_cond(c); 1006 SCOPED_TSAN_INTERCEPTOR(pthread_cond_broadcast, cond); 1007 MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), false); 1008 return REAL(pthread_cond_broadcast)(cond); 1009} 1010 1011INTERCEPTOR(int, pthread_cond_destroy, void *c) { 1012 void *cond = init_cond(c); 1013 SCOPED_TSAN_INTERCEPTOR(pthread_cond_destroy, cond); 1014 MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), true); 1015 int res = REAL(pthread_cond_destroy)(cond); 1016 if (common_flags()->legacy_pthread_cond) { 1017 // Free our aux cond and zero the pointer to not leave dangling pointers. 1018 WRAP(free)(cond); 1019 atomic_store((atomic_uintptr_t*)c, 0, memory_order_relaxed); 1020 } 1021 return res; 1022} 1023 1024TSAN_INTERCEPTOR(int, pthread_mutex_init, void *m, void *a) { 1025 SCOPED_TSAN_INTERCEPTOR(pthread_mutex_init, m, a); 1026 int res = REAL(pthread_mutex_init)(m, a); 1027 if (res == 0) { 1028 bool recursive = false; 1029 if (a) { 1030 int type = 0; 1031 if (pthread_mutexattr_gettype(a, &type) == 0) 1032 recursive = (type == PTHREAD_MUTEX_RECURSIVE 1033 || type == PTHREAD_MUTEX_RECURSIVE_NP); 1034 } 1035 MutexCreate(thr, pc, (uptr)m, false, recursive, false); 1036 } 1037 return res; 1038} 1039 1040TSAN_INTERCEPTOR(int, pthread_mutex_destroy, void *m) { 1041 SCOPED_TSAN_INTERCEPTOR(pthread_mutex_destroy, m); 1042 int res = REAL(pthread_mutex_destroy)(m); 1043 if (res == 0 || res == EBUSY) { 1044 MutexDestroy(thr, pc, (uptr)m); 1045 } 1046 return res; 1047} 1048 1049TSAN_INTERCEPTOR(int, pthread_mutex_trylock, void *m) { 1050 SCOPED_TSAN_INTERCEPTOR(pthread_mutex_trylock, m); 1051 int res = REAL(pthread_mutex_trylock)(m); 1052 if (res == EOWNERDEAD) 1053 MutexRepair(thr, pc, (uptr)m); 1054 if (res == 0 || res == EOWNERDEAD) 1055 MutexLock(thr, pc, (uptr)m, /*rec=*/1, /*try_lock=*/true); 1056 return res; 1057} 1058 1059TSAN_INTERCEPTOR(int, pthread_mutex_timedlock, void *m, void *abstime) { 1060 SCOPED_TSAN_INTERCEPTOR(pthread_mutex_timedlock, m, abstime); 1061 int res = REAL(pthread_mutex_timedlock)(m, abstime); 1062 if (res == 0) { 1063 MutexLock(thr, pc, (uptr)m); 1064 } 1065 return res; 1066} 1067 1068TSAN_INTERCEPTOR(int, pthread_spin_init, void *m, int pshared) { 1069 SCOPED_TSAN_INTERCEPTOR(pthread_spin_init, m, pshared); 1070 int res = REAL(pthread_spin_init)(m, pshared); 1071 if (res == 0) { 1072 MutexCreate(thr, pc, (uptr)m, false, false, false); 1073 } 1074 return res; 1075} 1076 1077TSAN_INTERCEPTOR(int, pthread_spin_destroy, void *m) { 1078 SCOPED_TSAN_INTERCEPTOR(pthread_spin_destroy, m); 1079 int res = REAL(pthread_spin_destroy)(m); 1080 if (res == 0) { 1081 MutexDestroy(thr, pc, (uptr)m); 1082 } 1083 return res; 1084} 1085 1086TSAN_INTERCEPTOR(int, pthread_spin_lock, void *m) { 1087 SCOPED_TSAN_INTERCEPTOR(pthread_spin_lock, m); 1088 int res = REAL(pthread_spin_lock)(m); 1089 if (res == 0) { 1090 MutexLock(thr, pc, (uptr)m); 1091 } 1092 return res; 1093} 1094 1095TSAN_INTERCEPTOR(int, pthread_spin_trylock, void *m) { 1096 SCOPED_TSAN_INTERCEPTOR(pthread_spin_trylock, m); 1097 int res = REAL(pthread_spin_trylock)(m); 1098 if (res == 0) { 1099 MutexLock(thr, pc, (uptr)m, /*rec=*/1, /*try_lock=*/true); 1100 } 1101 return res; 1102} 1103 1104TSAN_INTERCEPTOR(int, pthread_spin_unlock, void *m) { 1105 SCOPED_TSAN_INTERCEPTOR(pthread_spin_unlock, m); 1106 MutexUnlock(thr, pc, (uptr)m); 1107 int res = REAL(pthread_spin_unlock)(m); 1108 return res; 1109} 1110 1111TSAN_INTERCEPTOR(int, pthread_rwlock_init, void *m, void *a) { 1112 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_init, m, a); 1113 int res = REAL(pthread_rwlock_init)(m, a); 1114 if (res == 0) { 1115 MutexCreate(thr, pc, (uptr)m, true, false, false); 1116 } 1117 return res; 1118} 1119 1120TSAN_INTERCEPTOR(int, pthread_rwlock_destroy, void *m) { 1121 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_destroy, m); 1122 int res = REAL(pthread_rwlock_destroy)(m); 1123 if (res == 0) { 1124 MutexDestroy(thr, pc, (uptr)m); 1125 } 1126 return res; 1127} 1128 1129TSAN_INTERCEPTOR(int, pthread_rwlock_rdlock, void *m) { 1130 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_rdlock, m); 1131 int res = REAL(pthread_rwlock_rdlock)(m); 1132 if (res == 0) { 1133 MutexReadLock(thr, pc, (uptr)m); 1134 } 1135 return res; 1136} 1137 1138TSAN_INTERCEPTOR(int, pthread_rwlock_tryrdlock, void *m) { 1139 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_tryrdlock, m); 1140 int res = REAL(pthread_rwlock_tryrdlock)(m); 1141 if (res == 0) { 1142 MutexLock(thr, pc, (uptr)m, /*rec=*/1, /*try_lock=*/true); 1143 } 1144 return res; 1145} 1146 1147TSAN_INTERCEPTOR(int, pthread_rwlock_timedrdlock, void *m, void *abstime) { 1148 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_timedrdlock, m, abstime); 1149 int res = REAL(pthread_rwlock_timedrdlock)(m, abstime); 1150 if (res == 0) { 1151 MutexReadLock(thr, pc, (uptr)m); 1152 } 1153 return res; 1154} 1155 1156TSAN_INTERCEPTOR(int, pthread_rwlock_wrlock, void *m) { 1157 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_wrlock, m); 1158 int res = REAL(pthread_rwlock_wrlock)(m); 1159 if (res == 0) { 1160 MutexLock(thr, pc, (uptr)m); 1161 } 1162 return res; 1163} 1164 1165TSAN_INTERCEPTOR(int, pthread_rwlock_trywrlock, void *m) { 1166 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_trywrlock, m); 1167 int res = REAL(pthread_rwlock_trywrlock)(m); 1168 if (res == 0) { 1169 MutexLock(thr, pc, (uptr)m, /*rec=*/1, /*try_lock=*/true); 1170 } 1171 return res; 1172} 1173 1174TSAN_INTERCEPTOR(int, pthread_rwlock_timedwrlock, void *m, void *abstime) { 1175 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_timedwrlock, m, abstime); 1176 int res = REAL(pthread_rwlock_timedwrlock)(m, abstime); 1177 if (res == 0) { 1178 MutexLock(thr, pc, (uptr)m); 1179 } 1180 return res; 1181} 1182 1183TSAN_INTERCEPTOR(int, pthread_rwlock_unlock, void *m) { 1184 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_unlock, m); 1185 MutexReadOrWriteUnlock(thr, pc, (uptr)m); 1186 int res = REAL(pthread_rwlock_unlock)(m); 1187 return res; 1188} 1189 1190TSAN_INTERCEPTOR(int, pthread_barrier_init, void *b, void *a, unsigned count) { 1191 SCOPED_TSAN_INTERCEPTOR(pthread_barrier_init, b, a, count); 1192 MemoryWrite(thr, pc, (uptr)b, kSizeLog1); 1193 int res = REAL(pthread_barrier_init)(b, a, count); 1194 return res; 1195} 1196 1197TSAN_INTERCEPTOR(int, pthread_barrier_destroy, void *b) { 1198 SCOPED_TSAN_INTERCEPTOR(pthread_barrier_destroy, b); 1199 MemoryWrite(thr, pc, (uptr)b, kSizeLog1); 1200 int res = REAL(pthread_barrier_destroy)(b); 1201 return res; 1202} 1203 1204TSAN_INTERCEPTOR(int, pthread_barrier_wait, void *b) { 1205 SCOPED_TSAN_INTERCEPTOR(pthread_barrier_wait, b); 1206 Release(thr, pc, (uptr)b); 1207 MemoryRead(thr, pc, (uptr)b, kSizeLog1); 1208 int res = REAL(pthread_barrier_wait)(b); 1209 MemoryRead(thr, pc, (uptr)b, kSizeLog1); 1210 if (res == 0 || res == PTHREAD_BARRIER_SERIAL_THREAD) { 1211 Acquire(thr, pc, (uptr)b); 1212 } 1213 return res; 1214} 1215 1216TSAN_INTERCEPTOR(int, pthread_once, void *o, void (*f)()) { 1217 SCOPED_INTERCEPTOR_RAW(pthread_once, o, f); 1218 if (o == 0 || f == 0) 1219 return EINVAL; 1220 atomic_uint32_t *a = static_cast<atomic_uint32_t*>(o); 1221 u32 v = atomic_load(a, memory_order_acquire); 1222 if (v == 0 && atomic_compare_exchange_strong(a, &v, 1, 1223 memory_order_relaxed)) { 1224 (*f)(); 1225 if (!thr->in_ignored_lib) 1226 Release(thr, pc, (uptr)o); 1227 atomic_store(a, 2, memory_order_release); 1228 } else { 1229 while (v != 2) { 1230 pthread_yield(); 1231 v = atomic_load(a, memory_order_acquire); 1232 } 1233 if (!thr->in_ignored_lib) 1234 Acquire(thr, pc, (uptr)o); 1235 } 1236 return 0; 1237} 1238 1239TSAN_INTERCEPTOR(int, sem_init, void *s, int pshared, unsigned value) { 1240 SCOPED_TSAN_INTERCEPTOR(sem_init, s, pshared, value); 1241 int res = REAL(sem_init)(s, pshared, value); 1242 return res; 1243} 1244 1245TSAN_INTERCEPTOR(int, sem_destroy, void *s) { 1246 SCOPED_TSAN_INTERCEPTOR(sem_destroy, s); 1247 int res = REAL(sem_destroy)(s); 1248 return res; 1249} 1250 1251TSAN_INTERCEPTOR(int, sem_wait, void *s) { 1252 SCOPED_TSAN_INTERCEPTOR(sem_wait, s); 1253 int res = BLOCK_REAL(sem_wait)(s); 1254 if (res == 0) { 1255 Acquire(thr, pc, (uptr)s); 1256 } 1257 return res; 1258} 1259 1260TSAN_INTERCEPTOR(int, sem_trywait, void *s) { 1261 SCOPED_TSAN_INTERCEPTOR(sem_trywait, s); 1262 int res = BLOCK_REAL(sem_trywait)(s); 1263 if (res == 0) { 1264 Acquire(thr, pc, (uptr)s); 1265 } 1266 return res; 1267} 1268 1269TSAN_INTERCEPTOR(int, sem_timedwait, void *s, void *abstime) { 1270 SCOPED_TSAN_INTERCEPTOR(sem_timedwait, s, abstime); 1271 int res = BLOCK_REAL(sem_timedwait)(s, abstime); 1272 if (res == 0) { 1273 Acquire(thr, pc, (uptr)s); 1274 } 1275 return res; 1276} 1277 1278TSAN_INTERCEPTOR(int, sem_post, void *s) { 1279 SCOPED_TSAN_INTERCEPTOR(sem_post, s); 1280 Release(thr, pc, (uptr)s); 1281 int res = REAL(sem_post)(s); 1282 return res; 1283} 1284 1285TSAN_INTERCEPTOR(int, sem_getvalue, void *s, int *sval) { 1286 SCOPED_TSAN_INTERCEPTOR(sem_getvalue, s, sval); 1287 int res = REAL(sem_getvalue)(s, sval); 1288 if (res == 0) { 1289 Acquire(thr, pc, (uptr)s); 1290 } 1291 return res; 1292} 1293 1294TSAN_INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) { 1295 SCOPED_TSAN_INTERCEPTOR(__xstat, version, path, buf); 1296 return REAL(__xstat)(version, path, buf); 1297} 1298 1299TSAN_INTERCEPTOR(int, stat, const char *path, void *buf) { 1300 SCOPED_TSAN_INTERCEPTOR(__xstat, 0, path, buf); 1301 return REAL(__xstat)(0, path, buf); 1302} 1303 1304TSAN_INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) { 1305 SCOPED_TSAN_INTERCEPTOR(__xstat64, version, path, buf); 1306 return REAL(__xstat64)(version, path, buf); 1307} 1308 1309TSAN_INTERCEPTOR(int, stat64, const char *path, void *buf) { 1310 SCOPED_TSAN_INTERCEPTOR(__xstat64, 0, path, buf); 1311 return REAL(__xstat64)(0, path, buf); 1312} 1313 1314TSAN_INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) { 1315 SCOPED_TSAN_INTERCEPTOR(__lxstat, version, path, buf); 1316 return REAL(__lxstat)(version, path, buf); 1317} 1318 1319TSAN_INTERCEPTOR(int, lstat, const char *path, void *buf) { 1320 SCOPED_TSAN_INTERCEPTOR(__lxstat, 0, path, buf); 1321 return REAL(__lxstat)(0, path, buf); 1322} 1323 1324TSAN_INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) { 1325 SCOPED_TSAN_INTERCEPTOR(__lxstat64, version, path, buf); 1326 return REAL(__lxstat64)(version, path, buf); 1327} 1328 1329TSAN_INTERCEPTOR(int, lstat64, const char *path, void *buf) { 1330 SCOPED_TSAN_INTERCEPTOR(__lxstat64, 0, path, buf); 1331 return REAL(__lxstat64)(0, path, buf); 1332} 1333 1334TSAN_INTERCEPTOR(int, __fxstat, int version, int fd, void *buf) { 1335 SCOPED_TSAN_INTERCEPTOR(__fxstat, version, fd, buf); 1336 if (fd > 0) 1337 FdAccess(thr, pc, fd); 1338 return REAL(__fxstat)(version, fd, buf); 1339} 1340 1341TSAN_INTERCEPTOR(int, fstat, int fd, void *buf) { 1342 SCOPED_TSAN_INTERCEPTOR(__fxstat, 0, fd, buf); 1343 if (fd > 0) 1344 FdAccess(thr, pc, fd); 1345 return REAL(__fxstat)(0, fd, buf); 1346} 1347 1348TSAN_INTERCEPTOR(int, __fxstat64, int version, int fd, void *buf) { 1349 SCOPED_TSAN_INTERCEPTOR(__fxstat64, version, fd, buf); 1350 if (fd > 0) 1351 FdAccess(thr, pc, fd); 1352 return REAL(__fxstat64)(version, fd, buf); 1353} 1354 1355TSAN_INTERCEPTOR(int, fstat64, int fd, void *buf) { 1356 SCOPED_TSAN_INTERCEPTOR(__fxstat64, 0, fd, buf); 1357 if (fd > 0) 1358 FdAccess(thr, pc, fd); 1359 return REAL(__fxstat64)(0, fd, buf); 1360} 1361 1362TSAN_INTERCEPTOR(int, open, const char *name, int flags, int mode) { 1363 SCOPED_TSAN_INTERCEPTOR(open, name, flags, mode); 1364 int fd = REAL(open)(name, flags, mode); 1365 if (fd >= 0) 1366 FdFileCreate(thr, pc, fd); 1367 return fd; 1368} 1369 1370TSAN_INTERCEPTOR(int, open64, const char *name, int flags, int mode) { 1371 SCOPED_TSAN_INTERCEPTOR(open64, name, flags, mode); 1372 int fd = REAL(open64)(name, flags, mode); 1373 if (fd >= 0) 1374 FdFileCreate(thr, pc, fd); 1375 return fd; 1376} 1377 1378TSAN_INTERCEPTOR(int, creat, const char *name, int mode) { 1379 SCOPED_TSAN_INTERCEPTOR(creat, name, mode); 1380 int fd = REAL(creat)(name, mode); 1381 if (fd >= 0) 1382 FdFileCreate(thr, pc, fd); 1383 return fd; 1384} 1385 1386TSAN_INTERCEPTOR(int, creat64, const char *name, int mode) { 1387 SCOPED_TSAN_INTERCEPTOR(creat64, name, mode); 1388 int fd = REAL(creat64)(name, mode); 1389 if (fd >= 0) 1390 FdFileCreate(thr, pc, fd); 1391 return fd; 1392} 1393 1394TSAN_INTERCEPTOR(int, dup, int oldfd) { 1395 SCOPED_TSAN_INTERCEPTOR(dup, oldfd); 1396 int newfd = REAL(dup)(oldfd); 1397 if (oldfd >= 0 && newfd >= 0 && newfd != oldfd) 1398 FdDup(thr, pc, oldfd, newfd); 1399 return newfd; 1400} 1401 1402TSAN_INTERCEPTOR(int, dup2, int oldfd, int newfd) { 1403 SCOPED_TSAN_INTERCEPTOR(dup2, oldfd, newfd); 1404 int newfd2 = REAL(dup2)(oldfd, newfd); 1405 if (oldfd >= 0 && newfd2 >= 0 && newfd2 != oldfd) 1406 FdDup(thr, pc, oldfd, newfd2); 1407 return newfd2; 1408} 1409 1410TSAN_INTERCEPTOR(int, dup3, int oldfd, int newfd, int flags) { 1411 SCOPED_TSAN_INTERCEPTOR(dup3, oldfd, newfd, flags); 1412 int newfd2 = REAL(dup3)(oldfd, newfd, flags); 1413 if (oldfd >= 0 && newfd2 >= 0 && newfd2 != oldfd) 1414 FdDup(thr, pc, oldfd, newfd2); 1415 return newfd2; 1416} 1417 1418TSAN_INTERCEPTOR(int, eventfd, unsigned initval, int flags) { 1419 SCOPED_TSAN_INTERCEPTOR(eventfd, initval, flags); 1420 int fd = REAL(eventfd)(initval, flags); 1421 if (fd >= 0) 1422 FdEventCreate(thr, pc, fd); 1423 return fd; 1424} 1425 1426TSAN_INTERCEPTOR(int, signalfd, int fd, void *mask, int flags) { 1427 SCOPED_TSAN_INTERCEPTOR(signalfd, fd, mask, flags); 1428 if (fd >= 0) 1429 FdClose(thr, pc, fd); 1430 fd = REAL(signalfd)(fd, mask, flags); 1431 if (fd >= 0) 1432 FdSignalCreate(thr, pc, fd); 1433 return fd; 1434} 1435 1436TSAN_INTERCEPTOR(int, inotify_init, int fake) { 1437 SCOPED_TSAN_INTERCEPTOR(inotify_init, fake); 1438 int fd = REAL(inotify_init)(fake); 1439 if (fd >= 0) 1440 FdInotifyCreate(thr, pc, fd); 1441 return fd; 1442} 1443 1444TSAN_INTERCEPTOR(int, inotify_init1, int flags) { 1445 SCOPED_TSAN_INTERCEPTOR(inotify_init1, flags); 1446 int fd = REAL(inotify_init1)(flags); 1447 if (fd >= 0) 1448 FdInotifyCreate(thr, pc, fd); 1449 return fd; 1450} 1451 1452TSAN_INTERCEPTOR(int, socket, int domain, int type, int protocol) { 1453 SCOPED_TSAN_INTERCEPTOR(socket, domain, type, protocol); 1454 int fd = REAL(socket)(domain, type, protocol); 1455 if (fd >= 0) 1456 FdSocketCreate(thr, pc, fd); 1457 return fd; 1458} 1459 1460TSAN_INTERCEPTOR(int, socketpair, int domain, int type, int protocol, int *fd) { 1461 SCOPED_TSAN_INTERCEPTOR(socketpair, domain, type, protocol, fd); 1462 int res = REAL(socketpair)(domain, type, protocol, fd); 1463 if (res == 0 && fd[0] >= 0 && fd[1] >= 0) 1464 FdPipeCreate(thr, pc, fd[0], fd[1]); 1465 return res; 1466} 1467 1468TSAN_INTERCEPTOR(int, connect, int fd, void *addr, unsigned addrlen) { 1469 SCOPED_TSAN_INTERCEPTOR(connect, fd, addr, addrlen); 1470 FdSocketConnecting(thr, pc, fd); 1471 int res = REAL(connect)(fd, addr, addrlen); 1472 if (res == 0 && fd >= 0) 1473 FdSocketConnect(thr, pc, fd); 1474 return res; 1475} 1476 1477TSAN_INTERCEPTOR(int, bind, int fd, void *addr, unsigned addrlen) { 1478 SCOPED_TSAN_INTERCEPTOR(bind, fd, addr, addrlen); 1479 int res = REAL(bind)(fd, addr, addrlen); 1480 if (fd > 0 && res == 0) 1481 FdAccess(thr, pc, fd); 1482 return res; 1483} 1484 1485TSAN_INTERCEPTOR(int, listen, int fd, int backlog) { 1486 SCOPED_TSAN_INTERCEPTOR(listen, fd, backlog); 1487 int res = REAL(listen)(fd, backlog); 1488 if (fd > 0 && res == 0) 1489 FdAccess(thr, pc, fd); 1490 return res; 1491} 1492 1493TSAN_INTERCEPTOR(int, epoll_create, int size) { 1494 SCOPED_TSAN_INTERCEPTOR(epoll_create, size); 1495 int fd = REAL(epoll_create)(size); 1496 if (fd >= 0) 1497 FdPollCreate(thr, pc, fd); 1498 return fd; 1499} 1500 1501TSAN_INTERCEPTOR(int, epoll_create1, int flags) { 1502 SCOPED_TSAN_INTERCEPTOR(epoll_create1, flags); 1503 int fd = REAL(epoll_create1)(flags); 1504 if (fd >= 0) 1505 FdPollCreate(thr, pc, fd); 1506 return fd; 1507} 1508 1509TSAN_INTERCEPTOR(int, close, int fd) { 1510 SCOPED_TSAN_INTERCEPTOR(close, fd); 1511 if (fd >= 0) 1512 FdClose(thr, pc, fd); 1513 return REAL(close)(fd); 1514} 1515 1516TSAN_INTERCEPTOR(int, __close, int fd) { 1517 SCOPED_TSAN_INTERCEPTOR(__close, fd); 1518 if (fd >= 0) 1519 FdClose(thr, pc, fd); 1520 return REAL(__close)(fd); 1521} 1522 1523// glibc guts 1524TSAN_INTERCEPTOR(void, __res_iclose, void *state, bool free_addr) { 1525 SCOPED_TSAN_INTERCEPTOR(__res_iclose, state, free_addr); 1526 int fds[64]; 1527 int cnt = ExtractResolvFDs(state, fds, ARRAY_SIZE(fds)); 1528 for (int i = 0; i < cnt; i++) { 1529 if (fds[i] > 0) 1530 FdClose(thr, pc, fds[i]); 1531 } 1532 REAL(__res_iclose)(state, free_addr); 1533} 1534 1535TSAN_INTERCEPTOR(int, pipe, int *pipefd) { 1536 SCOPED_TSAN_INTERCEPTOR(pipe, pipefd); 1537 int res = REAL(pipe)(pipefd); 1538 if (res == 0 && pipefd[0] >= 0 && pipefd[1] >= 0) 1539 FdPipeCreate(thr, pc, pipefd[0], pipefd[1]); 1540 return res; 1541} 1542 1543TSAN_INTERCEPTOR(int, pipe2, int *pipefd, int flags) { 1544 SCOPED_TSAN_INTERCEPTOR(pipe2, pipefd, flags); 1545 int res = REAL(pipe2)(pipefd, flags); 1546 if (res == 0 && pipefd[0] >= 0 && pipefd[1] >= 0) 1547 FdPipeCreate(thr, pc, pipefd[0], pipefd[1]); 1548 return res; 1549} 1550 1551TSAN_INTERCEPTOR(long_t, send, int fd, void *buf, long_t len, int flags) { 1552 SCOPED_TSAN_INTERCEPTOR(send, fd, buf, len, flags); 1553 if (fd >= 0) { 1554 FdAccess(thr, pc, fd); 1555 FdRelease(thr, pc, fd); 1556 } 1557 int res = REAL(send)(fd, buf, len, flags); 1558 return res; 1559} 1560 1561TSAN_INTERCEPTOR(long_t, sendmsg, int fd, void *msg, int flags) { 1562 SCOPED_TSAN_INTERCEPTOR(sendmsg, fd, msg, flags); 1563 if (fd >= 0) { 1564 FdAccess(thr, pc, fd); 1565 FdRelease(thr, pc, fd); 1566 } 1567 int res = REAL(sendmsg)(fd, msg, flags); 1568 return res; 1569} 1570 1571TSAN_INTERCEPTOR(long_t, recv, int fd, void *buf, long_t len, int flags) { 1572 SCOPED_TSAN_INTERCEPTOR(recv, fd, buf, len, flags); 1573 if (fd >= 0) 1574 FdAccess(thr, pc, fd); 1575 int res = REAL(recv)(fd, buf, len, flags); 1576 if (res >= 0 && fd >= 0) { 1577 FdAcquire(thr, pc, fd); 1578 } 1579 return res; 1580} 1581 1582TSAN_INTERCEPTOR(int, unlink, char *path) { 1583 SCOPED_TSAN_INTERCEPTOR(unlink, path); 1584 Release(thr, pc, File2addr(path)); 1585 int res = REAL(unlink)(path); 1586 return res; 1587} 1588 1589TSAN_INTERCEPTOR(void*, tmpfile, int fake) { 1590 SCOPED_TSAN_INTERCEPTOR(tmpfile, fake); 1591 void *res = REAL(tmpfile)(fake); 1592 if (res) { 1593 int fd = fileno_unlocked(res); 1594 if (fd >= 0) 1595 FdFileCreate(thr, pc, fd); 1596 } 1597 return res; 1598} 1599 1600TSAN_INTERCEPTOR(void*, tmpfile64, int fake) { 1601 SCOPED_TSAN_INTERCEPTOR(tmpfile64, fake); 1602 void *res = REAL(tmpfile64)(fake); 1603 if (res) { 1604 int fd = fileno_unlocked(res); 1605 if (fd >= 0) 1606 FdFileCreate(thr, pc, fd); 1607 } 1608 return res; 1609} 1610 1611TSAN_INTERCEPTOR(uptr, fread, void *ptr, uptr size, uptr nmemb, void *f) { 1612 // libc file streams can call user-supplied functions, see fopencookie. 1613 { 1614 SCOPED_TSAN_INTERCEPTOR(fread, ptr, size, nmemb, f); 1615 MemoryAccessRange(thr, pc, (uptr)ptr, size * nmemb, true); 1616 } 1617 return REAL(fread)(ptr, size, nmemb, f); 1618} 1619 1620TSAN_INTERCEPTOR(uptr, fwrite, const void *p, uptr size, uptr nmemb, void *f) { 1621 // libc file streams can call user-supplied functions, see fopencookie. 1622 { 1623 SCOPED_TSAN_INTERCEPTOR(fwrite, p, size, nmemb, f); 1624 MemoryAccessRange(thr, pc, (uptr)p, size * nmemb, false); 1625 } 1626 return REAL(fwrite)(p, size, nmemb, f); 1627} 1628 1629TSAN_INTERCEPTOR(void, abort, int fake) { 1630 SCOPED_TSAN_INTERCEPTOR(abort, fake); 1631 REAL(fflush)(0); 1632 REAL(abort)(fake); 1633} 1634 1635TSAN_INTERCEPTOR(int, puts, const char *s) { 1636 SCOPED_TSAN_INTERCEPTOR(puts, s); 1637 MemoryAccessRange(thr, pc, (uptr)s, internal_strlen(s), false); 1638 return REAL(puts)(s); 1639} 1640 1641TSAN_INTERCEPTOR(int, rmdir, char *path) { 1642 SCOPED_TSAN_INTERCEPTOR(rmdir, path); 1643 Release(thr, pc, Dir2addr(path)); 1644 int res = REAL(rmdir)(path); 1645 return res; 1646} 1647 1648TSAN_INTERCEPTOR(void*, opendir, char *path) { 1649 SCOPED_TSAN_INTERCEPTOR(opendir, path); 1650 void *res = REAL(opendir)(path); 1651 if (res != 0) 1652 Acquire(thr, pc, Dir2addr(path)); 1653 return res; 1654} 1655 1656TSAN_INTERCEPTOR(int, epoll_ctl, int epfd, int op, int fd, void *ev) { 1657 SCOPED_TSAN_INTERCEPTOR(epoll_ctl, epfd, op, fd, ev); 1658 if (epfd >= 0) 1659 FdAccess(thr, pc, epfd); 1660 if (epfd >= 0 && fd >= 0) 1661 FdAccess(thr, pc, fd); 1662 if (op == EPOLL_CTL_ADD && epfd >= 0) 1663 FdRelease(thr, pc, epfd); 1664 int res = REAL(epoll_ctl)(epfd, op, fd, ev); 1665 return res; 1666} 1667 1668TSAN_INTERCEPTOR(int, epoll_wait, int epfd, void *ev, int cnt, int timeout) { 1669 SCOPED_TSAN_INTERCEPTOR(epoll_wait, epfd, ev, cnt, timeout); 1670 if (epfd >= 0) 1671 FdAccess(thr, pc, epfd); 1672 int res = BLOCK_REAL(epoll_wait)(epfd, ev, cnt, timeout); 1673 if (res > 0 && epfd >= 0) 1674 FdAcquire(thr, pc, epfd); 1675 return res; 1676} 1677 1678namespace __tsan { 1679 1680static void CallUserSignalHandler(ThreadState *thr, bool sync, bool sigact, 1681 int sig, my_siginfo_t *info, void *uctx) { 1682 // Ensure that the handler does not spoil errno. 1683 const int saved_errno = errno; 1684 errno = 99; 1685 // Need to remember pc before the call, because the handler can reset it. 1686 uptr pc = sigact ? 1687 (uptr)sigactions[sig].sa_sigaction : 1688 (uptr)sigactions[sig].sa_handler; 1689 pc += 1; // return address is expected, OutputReport() will undo this 1690 if (sigact) 1691 sigactions[sig].sa_sigaction(sig, info, uctx); 1692 else 1693 sigactions[sig].sa_handler(sig); 1694 // We do not detect errno spoiling for SIGTERM, 1695 // because some SIGTERM handlers do spoil errno but reraise SIGTERM, 1696 // tsan reports false positive in such case. 1697 // It's difficult to properly detect this situation (reraise), 1698 // because in async signal processing case (when handler is called directly 1699 // from rtl_generic_sighandler) we have not yet received the reraised 1700 // signal; and it looks too fragile to intercept all ways to reraise a signal. 1701 if (flags()->report_bugs && !sync && sig != SIGTERM && errno != 99) { 1702 __tsan::StackTrace stack; 1703 stack.ObtainCurrent(thr, pc); 1704 ThreadRegistryLock l(ctx->thread_registry); 1705 ScopedReport rep(ReportTypeErrnoInSignal); 1706 if (!IsFiredSuppression(ctx, rep, stack)) { 1707 rep.AddStack(&stack); 1708 OutputReport(ctx, rep, rep.GetReport()->stacks[0]); 1709 } 1710 } 1711 errno = saved_errno; 1712} 1713 1714void ProcessPendingSignals(ThreadState *thr) { 1715 SignalContext *sctx = SigCtx(thr); 1716 if (sctx == 0 || sctx->pending_signal_count == 0 || thr->in_signal_handler) 1717 return; 1718 thr->in_signal_handler = true; 1719 sctx->pending_signal_count = 0; 1720 // These are too big for stack. 1721 static THREADLOCAL __sanitizer_sigset_t emptyset, oldset; 1722 REAL(sigfillset)(&emptyset); 1723 pthread_sigmask(SIG_SETMASK, &emptyset, &oldset); 1724 for (int sig = 0; sig < kSigCount; sig++) { 1725 SignalDesc *signal = &sctx->pending_signals[sig]; 1726 if (signal->armed) { 1727 signal->armed = false; 1728 if (sigactions[sig].sa_handler != SIG_DFL 1729 && sigactions[sig].sa_handler != SIG_IGN) { 1730 CallUserSignalHandler(thr, false, signal->sigaction, 1731 sig, &signal->siginfo, &signal->ctx); 1732 } 1733 } 1734 } 1735 pthread_sigmask(SIG_SETMASK, &oldset, 0); 1736 CHECK_EQ(thr->in_signal_handler, true); 1737 thr->in_signal_handler = false; 1738} 1739 1740} // namespace __tsan 1741 1742static bool is_sync_signal(SignalContext *sctx, int sig) { 1743 return sig == SIGSEGV || sig == SIGBUS || sig == SIGILL || 1744 sig == SIGABRT || sig == SIGFPE || sig == SIGPIPE || sig == SIGSYS || 1745 // If we are sending signal to ourselves, we must process it now. 1746 (sctx && sig == sctx->int_signal_send); 1747} 1748 1749void ALWAYS_INLINE rtl_generic_sighandler(bool sigact, int sig, 1750 my_siginfo_t *info, void *ctx) { 1751 ThreadState *thr = cur_thread(); 1752 SignalContext *sctx = SigCtx(thr); 1753 if (sig < 0 || sig >= kSigCount) { 1754 VPrintf(1, "ThreadSanitizer: ignoring signal %d\n", sig); 1755 return; 1756 } 1757 // Don't mess with synchronous signals. 1758 const bool sync = is_sync_signal(sctx, sig); 1759 if (sync || 1760 // If we are in blocking function, we can safely process it now 1761 // (but check if we are in a recursive interceptor, 1762 // i.e. pthread_join()->munmap()). 1763 (sctx && sctx->in_blocking_func == 1)) { 1764 CHECK_EQ(thr->in_signal_handler, false); 1765 thr->in_signal_handler = true; 1766 if (sctx && sctx->in_blocking_func == 1) { 1767 // We ignore interceptors in blocking functions, 1768 // temporary enbled them again while we are calling user function. 1769 int const i = thr->ignore_interceptors; 1770 thr->ignore_interceptors = 0; 1771 CallUserSignalHandler(thr, sync, sigact, sig, info, ctx); 1772 thr->ignore_interceptors = i; 1773 } else { 1774 CallUserSignalHandler(thr, sync, sigact, sig, info, ctx); 1775 } 1776 CHECK_EQ(thr->in_signal_handler, true); 1777 thr->in_signal_handler = false; 1778 return; 1779 } 1780 1781 if (sctx == 0) 1782 return; 1783 SignalDesc *signal = &sctx->pending_signals[sig]; 1784 if (signal->armed == false) { 1785 signal->armed = true; 1786 signal->sigaction = sigact; 1787 if (info) 1788 internal_memcpy(&signal->siginfo, info, sizeof(*info)); 1789 if (ctx) 1790 internal_memcpy(&signal->ctx, ctx, sizeof(signal->ctx)); 1791 sctx->pending_signal_count++; 1792 } 1793} 1794 1795static void rtl_sighandler(int sig) { 1796 rtl_generic_sighandler(false, sig, 0, 0); 1797} 1798 1799static void rtl_sigaction(int sig, my_siginfo_t *info, void *ctx) { 1800 rtl_generic_sighandler(true, sig, info, ctx); 1801} 1802 1803TSAN_INTERCEPTOR(int, sigaction, int sig, sigaction_t *act, sigaction_t *old) { 1804 SCOPED_TSAN_INTERCEPTOR(sigaction, sig, act, old); 1805 if (old) 1806 internal_memcpy(old, &sigactions[sig], sizeof(*old)); 1807 if (act == 0) 1808 return 0; 1809 internal_memcpy(&sigactions[sig], act, sizeof(*act)); 1810 sigaction_t newact; 1811 internal_memcpy(&newact, act, sizeof(newact)); 1812 REAL(sigfillset)(&newact.sa_mask); 1813 if (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL) { 1814 if (newact.sa_flags & SA_SIGINFO) 1815 newact.sa_sigaction = rtl_sigaction; 1816 else 1817 newact.sa_handler = rtl_sighandler; 1818 } 1819 int res = REAL(sigaction)(sig, &newact, 0); 1820 return res; 1821} 1822 1823TSAN_INTERCEPTOR(sighandler_t, signal, int sig, sighandler_t h) { 1824 sigaction_t act; 1825 act.sa_handler = h; 1826 REAL(memset)(&act.sa_mask, -1, sizeof(act.sa_mask)); 1827 act.sa_flags = 0; 1828 sigaction_t old; 1829 int res = sigaction(sig, &act, &old); 1830 if (res) 1831 return SIG_ERR; 1832 return old.sa_handler; 1833} 1834 1835TSAN_INTERCEPTOR(int, sigsuspend, const __sanitizer_sigset_t *mask) { 1836 SCOPED_TSAN_INTERCEPTOR(sigsuspend, mask); 1837 return REAL(sigsuspend)(mask); 1838} 1839 1840TSAN_INTERCEPTOR(int, raise, int sig) { 1841 SCOPED_TSAN_INTERCEPTOR(raise, sig); 1842 SignalContext *sctx = SigCtx(thr); 1843 CHECK_NE(sctx, 0); 1844 int prev = sctx->int_signal_send; 1845 sctx->int_signal_send = sig; 1846 int res = REAL(raise)(sig); 1847 CHECK_EQ(sctx->int_signal_send, sig); 1848 sctx->int_signal_send = prev; 1849 return res; 1850} 1851 1852TSAN_INTERCEPTOR(int, kill, int pid, int sig) { 1853 SCOPED_TSAN_INTERCEPTOR(kill, pid, sig); 1854 SignalContext *sctx = SigCtx(thr); 1855 CHECK_NE(sctx, 0); 1856 int prev = sctx->int_signal_send; 1857 if (pid == (int)internal_getpid()) { 1858 sctx->int_signal_send = sig; 1859 } 1860 int res = REAL(kill)(pid, sig); 1861 if (pid == (int)internal_getpid()) { 1862 CHECK_EQ(sctx->int_signal_send, sig); 1863 sctx->int_signal_send = prev; 1864 } 1865 return res; 1866} 1867 1868TSAN_INTERCEPTOR(int, pthread_kill, void *tid, int sig) { 1869 SCOPED_TSAN_INTERCEPTOR(pthread_kill, tid, sig); 1870 SignalContext *sctx = SigCtx(thr); 1871 CHECK_NE(sctx, 0); 1872 int prev = sctx->int_signal_send; 1873 if (tid == pthread_self()) { 1874 sctx->int_signal_send = sig; 1875 } 1876 int res = REAL(pthread_kill)(tid, sig); 1877 if (tid == pthread_self()) { 1878 CHECK_EQ(sctx->int_signal_send, sig); 1879 sctx->int_signal_send = prev; 1880 } 1881 return res; 1882} 1883 1884TSAN_INTERCEPTOR(int, gettimeofday, void *tv, void *tz) { 1885 SCOPED_TSAN_INTERCEPTOR(gettimeofday, tv, tz); 1886 // It's intercepted merely to process pending signals. 1887 return REAL(gettimeofday)(tv, tz); 1888} 1889 1890TSAN_INTERCEPTOR(int, getaddrinfo, void *node, void *service, 1891 void *hints, void *rv) { 1892 SCOPED_TSAN_INTERCEPTOR(getaddrinfo, node, service, hints, rv); 1893 // We miss atomic synchronization in getaddrinfo, 1894 // and can report false race between malloc and free 1895 // inside of getaddrinfo. So ignore memory accesses. 1896 ThreadIgnoreBegin(thr, pc); 1897 int res = REAL(getaddrinfo)(node, service, hints, rv); 1898 ThreadIgnoreEnd(thr, pc); 1899 return res; 1900} 1901 1902// Linux kernel has a bug that leads to kernel deadlock if a process 1903// maps TBs of memory and then calls mlock(). 1904static void MlockIsUnsupported() { 1905 static atomic_uint8_t printed; 1906 if (atomic_exchange(&printed, 1, memory_order_relaxed)) 1907 return; 1908 VPrintf(1, "INFO: ThreadSanitizer ignores mlock/munlock[all]\n"); 1909} 1910 1911TSAN_INTERCEPTOR(int, mlock, const void *addr, uptr len) { 1912 MlockIsUnsupported(); 1913 return 0; 1914} 1915 1916TSAN_INTERCEPTOR(int, munlock, const void *addr, uptr len) { 1917 MlockIsUnsupported(); 1918 return 0; 1919} 1920 1921TSAN_INTERCEPTOR(int, mlockall, int flags) { 1922 MlockIsUnsupported(); 1923 return 0; 1924} 1925 1926TSAN_INTERCEPTOR(int, munlockall, void) { 1927 MlockIsUnsupported(); 1928 return 0; 1929} 1930 1931TSAN_INTERCEPTOR(int, fork, int fake) { 1932 if (cur_thread()->in_symbolizer) 1933 return REAL(fork)(fake); 1934 SCOPED_INTERCEPTOR_RAW(fork, fake); 1935 ForkBefore(thr, pc); 1936 int pid = REAL(fork)(fake); 1937 if (pid == 0) { 1938 // child 1939 ForkChildAfter(thr, pc); 1940 FdOnFork(thr, pc); 1941 } else if (pid > 0) { 1942 // parent 1943 ForkParentAfter(thr, pc); 1944 } else { 1945 // error 1946 ForkParentAfter(thr, pc); 1947 } 1948 return pid; 1949} 1950 1951TSAN_INTERCEPTOR(int, vfork, int fake) { 1952 // Some programs (e.g. openjdk) call close for all file descriptors 1953 // in the child process. Under tsan it leads to false positives, because 1954 // address space is shared, so the parent process also thinks that 1955 // the descriptors are closed (while they are actually not). 1956 // This leads to false positives due to missed synchronization. 1957 // Strictly saying this is undefined behavior, because vfork child is not 1958 // allowed to call any functions other than exec/exit. But this is what 1959 // openjdk does, so we want to handle it. 1960 // We could disable interceptors in the child process. But it's not possible 1961 // to simply intercept and wrap vfork, because vfork child is not allowed 1962 // to return from the function that calls vfork, and that's exactly what 1963 // we would do. So this would require some assembly trickery as well. 1964 // Instead we simply turn vfork into fork. 1965 return WRAP(fork)(fake); 1966} 1967 1968static int OnExit(ThreadState *thr) { 1969 int status = Finalize(thr); 1970 REAL(fflush)(0); 1971 return status; 1972} 1973 1974struct TsanInterceptorContext { 1975 ThreadState *thr; 1976 const uptr caller_pc; 1977 const uptr pc; 1978}; 1979 1980static void HandleRecvmsg(ThreadState *thr, uptr pc, 1981 __sanitizer_msghdr *msg) { 1982 int fds[64]; 1983 int cnt = ExtractRecvmsgFDs(msg, fds, ARRAY_SIZE(fds)); 1984 for (int i = 0; i < cnt; i++) 1985 FdEventCreate(thr, pc, fds[i]); 1986} 1987 1988#include "sanitizer_common/sanitizer_platform_interceptors.h" 1989// Causes interceptor recursion (getaddrinfo() and fopen()) 1990#undef SANITIZER_INTERCEPT_GETADDRINFO 1991 1992#define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name) 1993 1994#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \ 1995 MemoryAccessRange(((TsanInterceptorContext *)ctx)->thr, \ 1996 ((TsanInterceptorContext *)ctx)->pc, (uptr)ptr, size, \ 1997 true) 1998 1999#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \ 2000 MemoryAccessRange(((TsanInterceptorContext *) ctx)->thr, \ 2001 ((TsanInterceptorContext *) ctx)->pc, (uptr) ptr, size, \ 2002 false) 2003 2004#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \ 2005 SCOPED_TSAN_INTERCEPTOR(func, __VA_ARGS__); \ 2006 TsanInterceptorContext _ctx = {thr, caller_pc, pc}; \ 2007 ctx = (void *)&_ctx; \ 2008 (void) ctx; 2009 2010#define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, func, ...) \ 2011 SCOPED_INTERCEPTOR_RAW(func, __VA_ARGS__); \ 2012 TsanInterceptorContext _ctx = {thr, caller_pc, pc}; \ 2013 ctx = (void *)&_ctx; \ 2014 (void) ctx; 2015 2016#define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) \ 2017 Acquire(thr, pc, File2addr(path)); \ 2018 if (file) { \ 2019 int fd = fileno_unlocked(file); \ 2020 if (fd >= 0) FdFileCreate(thr, pc, fd); \ 2021 } 2022 2023#define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) \ 2024 if (file) { \ 2025 int fd = fileno_unlocked(file); \ 2026 if (fd >= 0) FdClose(thr, pc, fd); \ 2027 } 2028 2029#define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res) \ 2030 libignore()->OnLibraryLoaded(filename) 2031 2032#define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() \ 2033 libignore()->OnLibraryUnloaded() 2034 2035#define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \ 2036 FdAcquire(((TsanInterceptorContext *) ctx)->thr, pc, fd) 2037 2038#define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \ 2039 FdRelease(((TsanInterceptorContext *) ctx)->thr, pc, fd) 2040 2041#define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) \ 2042 FdAccess(((TsanInterceptorContext *) ctx)->thr, pc, fd) 2043 2044#define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \ 2045 FdSocketAccept(((TsanInterceptorContext *) ctx)->thr, pc, fd, newfd) 2046 2047#define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) \ 2048 ThreadSetName(((TsanInterceptorContext *) ctx)->thr, name) 2049 2050#define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \ 2051 __tsan::ctx->thread_registry->SetThreadNameByUserId(thread, name) 2052 2053#define COMMON_INTERCEPTOR_BLOCK_REAL(name) BLOCK_REAL(name) 2054 2055#define COMMON_INTERCEPTOR_ON_EXIT(ctx) \ 2056 OnExit(((TsanInterceptorContext *) ctx)->thr) 2057 2058#define COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m) \ 2059 MutexLock(((TsanInterceptorContext *)ctx)->thr, \ 2060 ((TsanInterceptorContext *)ctx)->pc, (uptr)m) 2061 2062#define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) \ 2063 MutexUnlock(((TsanInterceptorContext *)ctx)->thr, \ 2064 ((TsanInterceptorContext *)ctx)->pc, (uptr)m) 2065 2066#define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) \ 2067 MutexRepair(((TsanInterceptorContext *)ctx)->thr, \ 2068 ((TsanInterceptorContext *)ctx)->pc, (uptr)m) 2069 2070#define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) \ 2071 HandleRecvmsg(((TsanInterceptorContext *)ctx)->thr, \ 2072 ((TsanInterceptorContext *)ctx)->pc, msg) 2073 2074#include "sanitizer_common/sanitizer_common_interceptors.inc" 2075 2076#define TSAN_SYSCALL() \ 2077 ThreadState *thr = cur_thread(); \ 2078 if (thr->ignore_interceptors) \ 2079 return; \ 2080 ScopedSyscall scoped_syscall(thr) \ 2081/**/ 2082 2083struct ScopedSyscall { 2084 ThreadState *thr; 2085 2086 explicit ScopedSyscall(ThreadState *thr) 2087 : thr(thr) { 2088 Initialize(thr); 2089 } 2090 2091 ~ScopedSyscall() { 2092 ProcessPendingSignals(thr); 2093 } 2094}; 2095 2096static void syscall_access_range(uptr pc, uptr p, uptr s, bool write) { 2097 TSAN_SYSCALL(); 2098 MemoryAccessRange(thr, pc, p, s, write); 2099} 2100 2101static void syscall_acquire(uptr pc, uptr addr) { 2102 TSAN_SYSCALL(); 2103 Acquire(thr, pc, addr); 2104 DPrintf("syscall_acquire(%p)\n", addr); 2105} 2106 2107static void syscall_release(uptr pc, uptr addr) { 2108 TSAN_SYSCALL(); 2109 DPrintf("syscall_release(%p)\n", addr); 2110 Release(thr, pc, addr); 2111} 2112 2113static void syscall_fd_close(uptr pc, int fd) { 2114 TSAN_SYSCALL(); 2115 FdClose(thr, pc, fd); 2116} 2117 2118static USED void syscall_fd_acquire(uptr pc, int fd) { 2119 TSAN_SYSCALL(); 2120 FdAcquire(thr, pc, fd); 2121 DPrintf("syscall_fd_acquire(%p)\n", fd); 2122} 2123 2124static USED void syscall_fd_release(uptr pc, int fd) { 2125 TSAN_SYSCALL(); 2126 DPrintf("syscall_fd_release(%p)\n", fd); 2127 FdRelease(thr, pc, fd); 2128} 2129 2130static void syscall_pre_fork(uptr pc) { 2131 TSAN_SYSCALL(); 2132 ForkBefore(thr, pc); 2133} 2134 2135static void syscall_post_fork(uptr pc, int pid) { 2136 TSAN_SYSCALL(); 2137 if (pid == 0) { 2138 // child 2139 ForkChildAfter(thr, pc); 2140 FdOnFork(thr, pc); 2141 } else if (pid > 0) { 2142 // parent 2143 ForkParentAfter(thr, pc); 2144 } else { 2145 // error 2146 ForkParentAfter(thr, pc); 2147 } 2148} 2149 2150#define COMMON_SYSCALL_PRE_READ_RANGE(p, s) \ 2151 syscall_access_range(GET_CALLER_PC(), (uptr)(p), (uptr)(s), false) 2152 2153#define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) \ 2154 syscall_access_range(GET_CALLER_PC(), (uptr)(p), (uptr)(s), true) 2155 2156#define COMMON_SYSCALL_POST_READ_RANGE(p, s) \ 2157 do { \ 2158 (void)(p); \ 2159 (void)(s); \ 2160 } while (false) 2161 2162#define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \ 2163 do { \ 2164 (void)(p); \ 2165 (void)(s); \ 2166 } while (false) 2167 2168#define COMMON_SYSCALL_ACQUIRE(addr) \ 2169 syscall_acquire(GET_CALLER_PC(), (uptr)(addr)) 2170 2171#define COMMON_SYSCALL_RELEASE(addr) \ 2172 syscall_release(GET_CALLER_PC(), (uptr)(addr)) 2173 2174#define COMMON_SYSCALL_FD_CLOSE(fd) syscall_fd_close(GET_CALLER_PC(), fd) 2175 2176#define COMMON_SYSCALL_FD_ACQUIRE(fd) syscall_fd_acquire(GET_CALLER_PC(), fd) 2177 2178#define COMMON_SYSCALL_FD_RELEASE(fd) syscall_fd_release(GET_CALLER_PC(), fd) 2179 2180#define COMMON_SYSCALL_PRE_FORK() \ 2181 syscall_pre_fork(GET_CALLER_PC()) 2182 2183#define COMMON_SYSCALL_POST_FORK(res) \ 2184 syscall_post_fork(GET_CALLER_PC(), res) 2185 2186#include "sanitizer_common/sanitizer_common_syscalls.inc" 2187 2188namespace __tsan { 2189 2190static void finalize(void *arg) { 2191 ThreadState *thr = cur_thread(); 2192 uptr pc = 0; 2193 atexit_ctx->exit(thr, pc); 2194 int status = Finalize(thr); 2195 // Make sure the output is not lost. 2196 // Flushing all the streams here may freeze the process if a child thread is 2197 // performing file stream operations at the same time. 2198 REAL(fflush)(stdout); 2199 REAL(fflush)(stderr); 2200 if (status) 2201 REAL(_exit)(status); 2202} 2203 2204static void unreachable() { 2205 Report("FATAL: ThreadSanitizer: unreachable called\n"); 2206 Die(); 2207} 2208 2209void InitializeInterceptors() { 2210 // We need to setup it early, because functions like dlsym() can call it. 2211 REAL(memset) = internal_memset; 2212 REAL(memcpy) = internal_memcpy; 2213 REAL(memcmp) = internal_memcmp; 2214 2215 // Instruct libc malloc to consume less memory. 2216 mallopt(1, 0); // M_MXFAST 2217 mallopt(-3, 32*1024); // M_MMAP_THRESHOLD 2218 2219 InitializeCommonInterceptors(); 2220 2221 // We can not use TSAN_INTERCEPT to get setjmp addr, 2222 // because it does &setjmp and setjmp is not present in some versions of libc. 2223 using __interception::GetRealFunctionAddress; 2224 GetRealFunctionAddress("setjmp", (uptr*)&REAL(setjmp), 0, 0); 2225 GetRealFunctionAddress("_setjmp", (uptr*)&REAL(_setjmp), 0, 0); 2226 GetRealFunctionAddress("sigsetjmp", (uptr*)&REAL(sigsetjmp), 0, 0); 2227 GetRealFunctionAddress("__sigsetjmp", (uptr*)&REAL(__sigsetjmp), 0, 0); 2228 2229 TSAN_INTERCEPT(longjmp); 2230 TSAN_INTERCEPT(siglongjmp); 2231 2232 TSAN_INTERCEPT(malloc); 2233 TSAN_INTERCEPT(__libc_memalign); 2234 TSAN_INTERCEPT(calloc); 2235 TSAN_INTERCEPT(realloc); 2236 TSAN_INTERCEPT(free); 2237 TSAN_INTERCEPT(cfree); 2238 TSAN_INTERCEPT(mmap); 2239 TSAN_INTERCEPT(mmap64); 2240 TSAN_INTERCEPT(munmap); 2241 TSAN_INTERCEPT(memalign); 2242 TSAN_INTERCEPT(valloc); 2243 TSAN_INTERCEPT(pvalloc); 2244 TSAN_INTERCEPT(posix_memalign); 2245 2246 TSAN_INTERCEPT(strlen); 2247 TSAN_INTERCEPT(memset); 2248 TSAN_INTERCEPT(memcpy); 2249 TSAN_INTERCEPT(memmove); 2250 TSAN_INTERCEPT(memcmp); 2251 TSAN_INTERCEPT(strchr); 2252 TSAN_INTERCEPT(strchrnul); 2253 TSAN_INTERCEPT(strrchr); 2254 TSAN_INTERCEPT(strcpy); // NOLINT 2255 TSAN_INTERCEPT(strncpy); 2256 TSAN_INTERCEPT(strstr); 2257 TSAN_INTERCEPT(strdup); 2258 2259 TSAN_INTERCEPT(pthread_create); 2260 TSAN_INTERCEPT(pthread_join); 2261 TSAN_INTERCEPT(pthread_detach); 2262 2263 TSAN_INTERCEPT_VER(pthread_cond_init, "GLIBC_2.3.2"); 2264 TSAN_INTERCEPT_VER(pthread_cond_signal, "GLIBC_2.3.2"); 2265 TSAN_INTERCEPT_VER(pthread_cond_broadcast, "GLIBC_2.3.2"); 2266 TSAN_INTERCEPT_VER(pthread_cond_wait, "GLIBC_2.3.2"); 2267 TSAN_INTERCEPT_VER(pthread_cond_timedwait, "GLIBC_2.3.2"); 2268 TSAN_INTERCEPT_VER(pthread_cond_destroy, "GLIBC_2.3.2"); 2269 2270 TSAN_INTERCEPT(pthread_mutex_init); 2271 TSAN_INTERCEPT(pthread_mutex_destroy); 2272 TSAN_INTERCEPT(pthread_mutex_trylock); 2273 TSAN_INTERCEPT(pthread_mutex_timedlock); 2274 2275 TSAN_INTERCEPT(pthread_spin_init); 2276 TSAN_INTERCEPT(pthread_spin_destroy); 2277 TSAN_INTERCEPT(pthread_spin_lock); 2278 TSAN_INTERCEPT(pthread_spin_trylock); 2279 TSAN_INTERCEPT(pthread_spin_unlock); 2280 2281 TSAN_INTERCEPT(pthread_rwlock_init); 2282 TSAN_INTERCEPT(pthread_rwlock_destroy); 2283 TSAN_INTERCEPT(pthread_rwlock_rdlock); 2284 TSAN_INTERCEPT(pthread_rwlock_tryrdlock); 2285 TSAN_INTERCEPT(pthread_rwlock_timedrdlock); 2286 TSAN_INTERCEPT(pthread_rwlock_wrlock); 2287 TSAN_INTERCEPT(pthread_rwlock_trywrlock); 2288 TSAN_INTERCEPT(pthread_rwlock_timedwrlock); 2289 TSAN_INTERCEPT(pthread_rwlock_unlock); 2290 2291 TSAN_INTERCEPT(pthread_barrier_init); 2292 TSAN_INTERCEPT(pthread_barrier_destroy); 2293 TSAN_INTERCEPT(pthread_barrier_wait); 2294 2295 TSAN_INTERCEPT(pthread_once); 2296 2297 TSAN_INTERCEPT(sem_init); 2298 TSAN_INTERCEPT(sem_destroy); 2299 TSAN_INTERCEPT(sem_wait); 2300 TSAN_INTERCEPT(sem_trywait); 2301 TSAN_INTERCEPT(sem_timedwait); 2302 TSAN_INTERCEPT(sem_post); 2303 TSAN_INTERCEPT(sem_getvalue); 2304 2305 TSAN_INTERCEPT(stat); 2306 TSAN_INTERCEPT(__xstat); 2307 TSAN_INTERCEPT(stat64); 2308 TSAN_INTERCEPT(__xstat64); 2309 TSAN_INTERCEPT(lstat); 2310 TSAN_INTERCEPT(__lxstat); 2311 TSAN_INTERCEPT(lstat64); 2312 TSAN_INTERCEPT(__lxstat64); 2313 TSAN_INTERCEPT(fstat); 2314 TSAN_INTERCEPT(__fxstat); 2315 TSAN_INTERCEPT(fstat64); 2316 TSAN_INTERCEPT(__fxstat64); 2317 TSAN_INTERCEPT(open); 2318 TSAN_INTERCEPT(open64); 2319 TSAN_INTERCEPT(creat); 2320 TSAN_INTERCEPT(creat64); 2321 TSAN_INTERCEPT(dup); 2322 TSAN_INTERCEPT(dup2); 2323 TSAN_INTERCEPT(dup3); 2324 TSAN_INTERCEPT(eventfd); 2325 TSAN_INTERCEPT(signalfd); 2326 TSAN_INTERCEPT(inotify_init); 2327 TSAN_INTERCEPT(inotify_init1); 2328 TSAN_INTERCEPT(socket); 2329 TSAN_INTERCEPT(socketpair); 2330 TSAN_INTERCEPT(connect); 2331 TSAN_INTERCEPT(bind); 2332 TSAN_INTERCEPT(listen); 2333 TSAN_INTERCEPT(epoll_create); 2334 TSAN_INTERCEPT(epoll_create1); 2335 TSAN_INTERCEPT(close); 2336 TSAN_INTERCEPT(__close); 2337 TSAN_INTERCEPT(__res_iclose); 2338 TSAN_INTERCEPT(pipe); 2339 TSAN_INTERCEPT(pipe2); 2340 2341 TSAN_INTERCEPT(send); 2342 TSAN_INTERCEPT(sendmsg); 2343 TSAN_INTERCEPT(recv); 2344 2345 TSAN_INTERCEPT(unlink); 2346 TSAN_INTERCEPT(tmpfile); 2347 TSAN_INTERCEPT(tmpfile64); 2348 TSAN_INTERCEPT(fread); 2349 TSAN_INTERCEPT(fwrite); 2350 TSAN_INTERCEPT(abort); 2351 TSAN_INTERCEPT(puts); 2352 TSAN_INTERCEPT(rmdir); 2353 TSAN_INTERCEPT(opendir); 2354 2355 TSAN_INTERCEPT(epoll_ctl); 2356 TSAN_INTERCEPT(epoll_wait); 2357 2358 TSAN_INTERCEPT(sigaction); 2359 TSAN_INTERCEPT(signal); 2360 TSAN_INTERCEPT(sigsuspend); 2361 TSAN_INTERCEPT(raise); 2362 TSAN_INTERCEPT(kill); 2363 TSAN_INTERCEPT(pthread_kill); 2364 TSAN_INTERCEPT(sleep); 2365 TSAN_INTERCEPT(usleep); 2366 TSAN_INTERCEPT(nanosleep); 2367 TSAN_INTERCEPT(gettimeofday); 2368 TSAN_INTERCEPT(getaddrinfo); 2369 2370 TSAN_INTERCEPT(mlock); 2371 TSAN_INTERCEPT(munlock); 2372 TSAN_INTERCEPT(mlockall); 2373 TSAN_INTERCEPT(munlockall); 2374 2375 TSAN_INTERCEPT(fork); 2376 TSAN_INTERCEPT(vfork); 2377 TSAN_INTERCEPT(on_exit); 2378 TSAN_INTERCEPT(__cxa_atexit); 2379 TSAN_INTERCEPT(_exit); 2380 2381 // Need to setup it, because interceptors check that the function is resolved. 2382 // But atexit is emitted directly into the module, so can't be resolved. 2383 REAL(atexit) = (int(*)(void(*)()))unreachable; 2384 atexit_ctx = new(internal_alloc(MBlockAtExit, sizeof(AtExitContext))) 2385 AtExitContext(); 2386 2387 if (REAL(__cxa_atexit)(&finalize, 0, 0)) { 2388 Printf("ThreadSanitizer: failed to setup atexit callback\n"); 2389 Die(); 2390 } 2391 2392 if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) { 2393 Printf("ThreadSanitizer: failed to create thread key\n"); 2394 Die(); 2395 } 2396 2397 FdInit(); 2398} 2399 2400void *internal_start_thread(void(*func)(void *arg), void *arg) { 2401 // Start the thread with signals blocked, otherwise it can steal user signals. 2402 __sanitizer_sigset_t set, old; 2403 internal_sigfillset(&set); 2404 internal_sigprocmask(SIG_SETMASK, &set, &old); 2405 void *th; 2406 REAL(pthread_create)(&th, 0, (void*(*)(void *arg))func, arg); 2407 internal_sigprocmask(SIG_SETMASK, &old, 0); 2408 return th; 2409} 2410 2411void internal_join_thread(void *th) { 2412 REAL(pthread_join)(th, 0); 2413} 2414 2415} // namespace __tsan 2416