blob: 62c96cb420477e7fca765f23897b1dc4c7027a95 [file] [log] [blame]
Alexey Samsonov4f0ea392012-09-24 13:19:47 +00001//===-- tsan_interceptors.cc ----------------------------------------------===//
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002//
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//
Kostya Serebryany8530e2b2012-12-12 09:54:35 +000012// FIXME: move as many interceptors as possible into
Dmitry Vyukovedd28212013-04-24 11:06:05 +000013// sanitizer_common/sanitizer_common_interceptors.inc
Kostya Serebryany7ac41482012-05-10 13:48:04 +000014//===----------------------------------------------------------------------===//
15
Dmitry Vyukovfce5bd42012-06-29 16:58:33 +000016#include "sanitizer_common/sanitizer_atomic.h"
Alexey Samsonov230c3be2012-06-06 09:26:25 +000017#include "sanitizer_common/sanitizer_libc.h"
Evgeniy Stepanovb9bf7002013-03-19 09:30:52 +000018#include "sanitizer_common/sanitizer_linux.h"
Alexey Samsonov47a0f6e2013-03-14 07:13:00 +000019#include "sanitizer_common/sanitizer_platform_limits_posix.h"
Alexey Samsonov47b16342012-06-07 09:50:16 +000020#include "sanitizer_common/sanitizer_placement_new.h"
Alexey Samsonov7eff3112012-09-27 09:50:19 +000021#include "sanitizer_common/sanitizer_stacktrace.h"
Dmitry Vyukov36decbf2012-12-05 10:16:17 +000022#include "interception/interception.h"
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070023#include "tsan_interceptors.h"
Kostya Serebryany7ac41482012-05-10 13:48:04 +000024#include "tsan_interface.h"
Kostya Serebryany7ac41482012-05-10 13:48:04 +000025#include "tsan_platform.h"
Dmitry Vyukov4af0f212013-10-03 13:37:17 +000026#include "tsan_suppressions.h"
Alexey Samsonov4f0ea392012-09-24 13:19:47 +000027#include "tsan_rtl.h"
Kostya Serebryany7ac41482012-05-10 13:48:04 +000028#include "tsan_mman.h"
Dmitry Vyukovc78839f2012-12-12 11:59:30 +000029#include "tsan_fd.h"
Kostya Serebryany7ac41482012-05-10 13:48:04 +000030
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080031#if SANITIZER_POSIX
32#include "sanitizer_common/sanitizer_posix.h"
33#endif
34
Kostya Serebryany7ac41482012-05-10 13:48:04 +000035using namespace __tsan; // NOLINT
36
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080037#if SANITIZER_FREEBSD || SANITIZER_MAC
Stephen Hines6d186232014-11-26 17:56:19 -080038#define __errno_location __error
Stephen Hines6d186232014-11-26 17:56:19 -080039#define stdout __stdoutp
40#define stderr __stderrp
41#endif
42
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080043#if SANITIZER_FREEBSD
44#define __libc_realloc __realloc
45#define __libc_calloc __calloc
46#elif SANITIZER_MAC
47#define __libc_malloc REAL(malloc)
48#define __libc_realloc REAL(realloc)
49#define __libc_calloc REAL(calloc)
50#define __libc_free REAL(free)
51#elif SANITIZER_ANDROID
52#define __errno_location __errno
53#define __libc_malloc REAL(malloc)
54#define __libc_realloc REAL(realloc)
55#define __libc_calloc REAL(calloc)
56#define __libc_free REAL(free)
57#define mallopt(a, b)
58#endif
59
60#if SANITIZER_LINUX || SANITIZER_FREEBSD
61#define PTHREAD_CREATE_DETACHED 1
62#elif SANITIZER_MAC
63#define PTHREAD_CREATE_DETACHED 2
64#endif
65
66
Stephen Hines86277eb2015-03-23 12:06:32 -070067#ifdef __mips__
68const int kSigCount = 129;
69#else
Stephen Hines2d1fdb22014-05-28 23:58:16 -070070const int kSigCount = 65;
Stephen Hines86277eb2015-03-23 12:06:32 -070071#endif
Dmitry Vyukove9636662012-06-27 16:05:06 +000072
73struct my_siginfo_t {
Dmitry Vyukov4219ea32013-03-21 06:28:04 +000074 // The size is determined by looking at sizeof of real siginfo_t on linux.
Dmitry Vyukov236a0982013-03-20 14:57:28 +000075 u64 opaque[128 / sizeof(u64)];
Dmitry Vyukove9636662012-06-27 16:05:06 +000076};
77
Stephen Hines86277eb2015-03-23 12:06:32 -070078#ifdef __mips__
79struct ucontext_t {
80 u64 opaque[768 / sizeof(u64) + 1];
81};
82#else
Kostya Serebryany7ac41482012-05-10 13:48:04 +000083struct ucontext_t {
Dmitry Vyukov4219ea32013-03-21 06:28:04 +000084 // The size is determined by looking at sizeof of real ucontext_t on linux.
Dmitry Vyukov236a0982013-03-20 14:57:28 +000085 u64 opaque[936 / sizeof(u64) + 1];
Kostya Serebryany7ac41482012-05-10 13:48:04 +000086};
Stephen Hines86277eb2015-03-23 12:06:32 -070087#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +000088
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080089#if defined(__x86_64__) || defined(__mips__) \
90 || (defined(__powerpc64__) && defined(__BIG_ENDIAN__))
91#define PTHREAD_ABI_BASE "GLIBC_2.3.2"
92#elif defined(__aarch64__) || (defined(__powerpc64__) \
93 && defined(__LITTLE_ENDIAN__))
94#define PTHREAD_ABI_BASE "GLIBC_2.17"
95#endif
96
Kostya Serebryany7ac41482012-05-10 13:48:04 +000097extern "C" int pthread_attr_init(void *attr);
98extern "C" int pthread_attr_destroy(void *attr);
Evgeniy Stepanove236dbb2013-10-25 13:01:31 +000099DECLARE_REAL(int, pthread_attr_getdetachstate, void *, void *)
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000100extern "C" int pthread_attr_setstacksize(void *attr, uptr stacksize);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000101extern "C" int pthread_key_create(unsigned *key, void (*destructor)(void* v));
102extern "C" int pthread_setspecific(unsigned key, const void *v);
Stephen Hines6a211c52014-07-21 00:49:56 -0700103DECLARE_REAL(int, pthread_mutexattr_gettype, void *, void *)
Evgeniy Stepanov9a949a82013-09-25 14:47:43 +0000104extern "C" int pthread_sigmask(int how, const __sanitizer_sigset_t *set,
105 __sanitizer_sigset_t *oldset);
106// REAL(sigfillset) defined in common interceptors.
107DECLARE_REAL(int, sigfillset, __sanitizer_sigset_t *set)
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700108DECLARE_REAL(int, fflush, __sanitizer_FILE *fp)
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800109DECLARE_REAL_AND_INTERCEPTOR(void *, malloc, uptr size)
110DECLARE_REAL_AND_INTERCEPTOR(void, free, void *ptr)
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000111extern "C" void *pthread_self();
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000112extern "C" void _exit(int status);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000113extern "C" int *__errno_location();
Dmitry Vyukovf17b3822013-01-09 11:54:52 +0000114extern "C" int fileno_unlocked(void *stream);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800115#if !SANITIZER_ANDROID
Dmitry Vyukov31c05ea2013-01-29 13:05:30 +0000116extern "C" void *__libc_calloc(uptr size, uptr n);
117extern "C" void *__libc_realloc(void *ptr, uptr size);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800118#endif
Stephen Hines86277eb2015-03-23 12:06:32 -0700119extern "C" int dirfd(void *dirp);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800120#if !SANITIZER_FREEBSD && !SANITIZER_ANDROID
Dmitry Vyukov8e1c7692013-03-19 12:24:19 +0000121extern "C" int mallopt(int param, int value);
Stephen Hines6d186232014-11-26 17:56:19 -0800122#endif
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700123extern __sanitizer_FILE *stdout, *stderr;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000124const int PTHREAD_MUTEX_RECURSIVE = 1;
125const int PTHREAD_MUTEX_RECURSIVE_NP = 1;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000126const int EINVAL = 22;
127const int EBUSY = 16;
Dmitry Vyukov11f53092013-11-15 16:58:12 +0000128const int EOWNERDEAD = 130;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800129#if !SANITIZER_MAC
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000130const int EPOLL_CTL_ADD = 1;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800131#endif
Dmitry Vyukovd91d0672012-05-21 08:26:51 +0000132const int SIGILL = 4;
133const int SIGABRT = 6;
134const int SIGFPE = 8;
135const int SIGSEGV = 11;
136const int SIGPIPE = 13;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700137const int SIGTERM = 15;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800138#if defined(__mips__) || SANITIZER_MAC
Stephen Hines86277eb2015-03-23 12:06:32 -0700139const int SIGBUS = 10;
140const int SIGSYS = 12;
141#else
Dmitry Vyukovd91d0672012-05-21 08:26:51 +0000142const int SIGBUS = 7;
Dmitry Vyukov158ca522013-07-18 13:14:11 +0000143const int SIGSYS = 31;
Stephen Hines86277eb2015-03-23 12:06:32 -0700144#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000145void *const MAP_FAILED = (void*)-1;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800146#if !SANITIZER_MAC
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000147const int PTHREAD_BARRIER_SERIAL_THREAD = -1;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800148#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000149const int MAP_FIXED = 0x10;
150typedef long long_t; // NOLINT
151
Alexander Potapenko0ce948e2012-09-11 09:26:35 +0000152// From /usr/include/unistd.h
153# define F_ULOCK 0 /* Unlock a previously locked region. */
154# define F_LOCK 1 /* Lock a region for exclusive use. */
155# define F_TLOCK 2 /* Test and lock a region for exclusive use. */
156# define F_TEST 3 /* Test a region for other processes locks. */
157
Dmitry Vyukov7a72b4a2012-06-27 13:54:46 +0000158#define errno (*__errno_location())
159
Stephen Hines86277eb2015-03-23 12:06:32 -0700160typedef void (*sighandler_t)(int sig);
161typedef void (*sigactionhandler_t)(int sig, my_siginfo_t *siginfo, void *uctx);
162
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800163#if SANITIZER_ANDROID
164struct sigaction_t {
165 u32 sa_flags;
166 union {
167 sighandler_t sa_handler;
168 sigactionhandler_t sa_sgiaction;
169 };
170 __sanitizer_sigset_t sa_mask;
171 void (*sa_restorer)();
172};
173#else
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000174struct sigaction_t {
Stephen Hines86277eb2015-03-23 12:06:32 -0700175#ifdef __mips__
176 u32 sa_flags;
177#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000178 union {
179 sighandler_t sa_handler;
Stephen Hines86277eb2015-03-23 12:06:32 -0700180 sigactionhandler_t sa_sigaction;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000181 };
Stephen Hines6d186232014-11-26 17:56:19 -0800182#if SANITIZER_FREEBSD
183 int sa_flags;
184 __sanitizer_sigset_t sa_mask;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800185#elif SANITIZER_MAC
186 __sanitizer_sigset_t sa_mask;
187 int sa_flags;
Stephen Hines6d186232014-11-26 17:56:19 -0800188#else
Evgeniy Stepanov9a949a82013-09-25 14:47:43 +0000189 __sanitizer_sigset_t sa_mask;
Stephen Hines86277eb2015-03-23 12:06:32 -0700190#ifndef __mips__
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000191 int sa_flags;
Stephen Hines86277eb2015-03-23 12:06:32 -0700192#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000193 void (*sa_restorer)();
Stephen Hines6d186232014-11-26 17:56:19 -0800194#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000195};
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800196#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000197
198const sighandler_t SIG_DFL = (sighandler_t)0;
199const sighandler_t SIG_IGN = (sighandler_t)1;
Dmitry Vyukovd91d0672012-05-21 08:26:51 +0000200const sighandler_t SIG_ERR = (sighandler_t)-1;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800201#if SANITIZER_FREEBSD || SANITIZER_MAC
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700202const int SA_SIGINFO = 0x40;
203const int SIG_SETMASK = 3;
204#elif defined(__mips__)
Stephen Hines86277eb2015-03-23 12:06:32 -0700205const int SA_SIGINFO = 8;
206const int SIG_SETMASK = 3;
207#else
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000208const int SA_SIGINFO = 4;
209const int SIG_SETMASK = 2;
Stephen Hines86277eb2015-03-23 12:06:32 -0700210#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000211
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700212#define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED \
213 (!cur_thread()->is_inited)
214
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000215static sigaction_t sigactions[kSigCount];
216
Dmitry Vyukove9636662012-06-27 16:05:06 +0000217namespace __tsan {
218struct SignalDesc {
219 bool armed;
220 bool sigaction;
221 my_siginfo_t siginfo;
222 ucontext_t ctx;
223};
224
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700225struct ThreadSignalContext {
Dmitry Vyukove9636662012-06-27 16:05:06 +0000226 int int_signal_send;
Stephen Hines6d186232014-11-26 17:56:19 -0800227 atomic_uintptr_t in_blocking_func;
228 atomic_uintptr_t have_pending_signals;
Dmitry Vyukove9636662012-06-27 16:05:06 +0000229 SignalDesc pending_signals[kSigCount];
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800230 // emptyset and oldset are too big for stack.
231 __sanitizer_sigset_t emptyset;
232 __sanitizer_sigset_t oldset;
Dmitry Vyukove9636662012-06-27 16:05:06 +0000233};
Dmitry Vyukovd1886532013-09-21 23:06:00 +0000234
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000235// The object is 64-byte aligned, because we want hot data to be located in
236// a single cache line if possible (it's accessed in every interceptor).
237static ALIGNED(64) char libignore_placeholder[sizeof(LibIgnore)];
238static LibIgnore *libignore() {
239 return reinterpret_cast<LibIgnore*>(&libignore_placeholder[0]);
240}
Dmitry Vyukovd1886532013-09-21 23:06:00 +0000241
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000242void InitializeLibIgnore() {
Stephen Hines86277eb2015-03-23 12:06:32 -0700243 const SuppressionContext &supp = *Suppressions();
244 const uptr n = supp.SuppressionCount();
245 for (uptr i = 0; i < n; i++) {
246 const Suppression *s = supp.SuppressionAt(i);
247 if (0 == internal_strcmp(s->type, kSuppressionLib))
248 libignore()->AddIgnoredLibrary(s->templ);
249 }
Dmitry Vyukov3dbbd852013-10-15 11:34:59 +0000250 libignore()->OnLibraryLoaded(0);
Dmitry Vyukovd1886532013-09-21 23:06:00 +0000251}
252
Alexey Samsonovba5e9962013-01-30 07:45:58 +0000253} // namespace __tsan
Dmitry Vyukove9636662012-06-27 16:05:06 +0000254
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700255static ThreadSignalContext *SigCtx(ThreadState *thr) {
256 ThreadSignalContext *ctx = (ThreadSignalContext*)thr->signal_ctx;
Stephen Hines6d186232014-11-26 17:56:19 -0800257 if (ctx == 0 && !thr->is_dead) {
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700258 ctx = (ThreadSignalContext*)MmapOrDie(sizeof(*ctx), "ThreadSignalContext");
Dmitry Vyukovc78839f2012-12-12 11:59:30 +0000259 MemoryResetRange(thr, (uptr)&SigCtx, (uptr)ctx, sizeof(*ctx));
Dmitry Vyukove9636662012-06-27 16:05:06 +0000260 thr->signal_ctx = ctx;
261 }
262 return ctx;
263}
264
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800265#if !SANITIZER_MAC
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000266static unsigned g_thread_finalize_key;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800267#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000268
Alexey Samsonov4f0ea392012-09-24 13:19:47 +0000269ScopedInterceptor::ScopedInterceptor(ThreadState *thr, const char *fname,
270 uptr pc)
271 : thr_(thr)
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700272 , pc_(pc)
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000273 , in_ignored_lib_(false) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700274 if (!thr_->ignore_interceptors) {
Alexey Samsonov4f0ea392012-09-24 13:19:47 +0000275 Initialize(thr);
276 FuncEntry(thr, pc);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000277 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700278 DPrintf("#%d: intercept %s()\n", thr_->tid, fname);
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000279 if (!thr_->in_ignored_lib && libignore()->IsIgnored(pc)) {
280 in_ignored_lib_ = true;
281 thr_->in_ignored_lib = true;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700282 ThreadIgnoreBegin(thr_, pc_);
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000283 }
Alexey Samsonov4f0ea392012-09-24 13:19:47 +0000284}
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000285
Alexey Samsonov4f0ea392012-09-24 13:19:47 +0000286ScopedInterceptor::~ScopedInterceptor() {
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000287 if (in_ignored_lib_) {
288 thr_->in_ignored_lib = false;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700289 ThreadIgnoreEnd(thr_, pc_);
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000290 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700291 if (!thr_->ignore_interceptors) {
Dmitry Vyukovee8ee242012-11-15 17:40:49 +0000292 ProcessPendingSignals(thr_);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700293 FuncExit(thr_);
Stephen Hines6a211c52014-07-21 00:49:56 -0700294 CheckNoLocks(thr_);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000295 }
Dmitry Vyukov05906a72012-05-21 17:39:40 +0000296}
297
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800298void ScopedInterceptor::UserCallbackStart() {
299 if (in_ignored_lib_) {
300 thr_->in_ignored_lib = false;
301 ThreadIgnoreEnd(thr_, pc_);
302 }
303}
Dmitry Vyukov36decbf2012-12-05 10:16:17 +0000304
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800305void ScopedInterceptor::UserCallbackEnd() {
306 if (in_ignored_lib_) {
307 thr_->in_ignored_lib = true;
308 ThreadIgnoreBegin(thr_, pc_);
309 }
310}
311
Dmitry Vyukov36decbf2012-12-05 10:16:17 +0000312#define TSAN_INTERCEPT(func) INTERCEPT_FUNCTION(func)
Stephen Hines6d186232014-11-26 17:56:19 -0800313#if SANITIZER_FREEBSD
314# define TSAN_INTERCEPT_VER(func, ver) INTERCEPT_FUNCTION(func)
315#else
316# define TSAN_INTERCEPT_VER(func, ver) INTERCEPT_FUNCTION_VER(func, ver)
317#endif
Dmitry Vyukov36decbf2012-12-05 10:16:17 +0000318
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700319#define READ_STRING_OF_LEN(thr, pc, s, len, n) \
320 MemoryAccessRange((thr), (pc), (uptr)(s), \
321 common_flags()->strict_string_checks ? (len) + 1 : (n), false)
322
323#define READ_STRING(thr, pc, s, n) \
324 READ_STRING_OF_LEN((thr), (pc), (s), internal_strlen(s), (n))
325
Dmitry Vyukovee8ee242012-11-15 17:40:49 +0000326#define BLOCK_REAL(name) (BlockingCall(thr), REAL(name))
327
328struct BlockingCall {
Alexey Samsonovb717f4d2012-11-16 11:21:07 +0000329 explicit BlockingCall(ThreadState *thr)
Stephen Hines6d186232014-11-26 17:56:19 -0800330 : thr(thr)
331 , ctx(SigCtx(thr)) {
332 for (;;) {
333 atomic_store(&ctx->in_blocking_func, 1, memory_order_relaxed);
334 if (atomic_load(&ctx->have_pending_signals, memory_order_relaxed) == 0)
335 break;
336 atomic_store(&ctx->in_blocking_func, 0, memory_order_relaxed);
337 ProcessPendingSignals(thr);
338 }
339 // When we are in a "blocking call", we process signals asynchronously
340 // (right when they arrive). In this context we do not expect to be
341 // executing any user/runtime code. The known interceptor sequence when
342 // this is not true is: pthread_join -> munmap(stack). It's fine
343 // to ignore munmap in this case -- we handle stack shadow separately.
344 thr->ignore_interceptors++;
Dmitry Vyukovee8ee242012-11-15 17:40:49 +0000345 }
346
347 ~BlockingCall() {
Stephen Hines6d186232014-11-26 17:56:19 -0800348 thr->ignore_interceptors--;
349 atomic_store(&ctx->in_blocking_func, 0, memory_order_relaxed);
Dmitry Vyukovee8ee242012-11-15 17:40:49 +0000350 }
351
Stephen Hines6d186232014-11-26 17:56:19 -0800352 ThreadState *thr;
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700353 ThreadSignalContext *ctx;
Dmitry Vyukovee8ee242012-11-15 17:40:49 +0000354};
355
Dmitry Vyukov84853112012-08-31 17:27:49 +0000356TSAN_INTERCEPTOR(unsigned, sleep, unsigned sec) {
357 SCOPED_TSAN_INTERCEPTOR(sleep, sec);
Dmitry Vyukovee8ee242012-11-15 17:40:49 +0000358 unsigned res = BLOCK_REAL(sleep)(sec);
Dmitry Vyukov84853112012-08-31 17:27:49 +0000359 AfterSleep(thr, pc);
360 return res;
361}
362
363TSAN_INTERCEPTOR(int, usleep, long_t usec) {
364 SCOPED_TSAN_INTERCEPTOR(usleep, usec);
Dmitry Vyukovee8ee242012-11-15 17:40:49 +0000365 int res = BLOCK_REAL(usleep)(usec);
Dmitry Vyukov84853112012-08-31 17:27:49 +0000366 AfterSleep(thr, pc);
367 return res;
368}
369
370TSAN_INTERCEPTOR(int, nanosleep, void *req, void *rem) {
371 SCOPED_TSAN_INTERCEPTOR(nanosleep, req, rem);
Dmitry Vyukovee8ee242012-11-15 17:40:49 +0000372 int res = BLOCK_REAL(nanosleep)(req, rem);
Dmitry Vyukov84853112012-08-31 17:27:49 +0000373 AfterSleep(thr, pc);
374 return res;
375}
376
Stephen Hines6d186232014-11-26 17:56:19 -0800377// The sole reason tsan wraps atexit callbacks is to establish synchronization
378// between callback setup and callback execution.
379struct AtExitCtx {
380 void (*f)();
381 void *arg;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000382};
383
Stephen Hines6d186232014-11-26 17:56:19 -0800384static void at_exit_wrapper(void *arg) {
385 ThreadState *thr = cur_thread();
386 uptr pc = 0;
387 Acquire(thr, pc, (uptr)arg);
388 AtExitCtx *ctx = (AtExitCtx*)arg;
389 ((void(*)(void *arg))ctx->f)(ctx->arg);
390 __libc_free(ctx);
391}
392
393static int setup_at_exit_wrapper(ThreadState *thr, uptr pc, void(*f)(),
394 void *arg, void *dso);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000395
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800396#if !SANITIZER_ANDROID
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000397TSAN_INTERCEPTOR(int, atexit, void (*f)()) {
Dmitry Vyukov31c05ea2013-01-29 13:05:30 +0000398 if (cur_thread()->in_symbolizer)
399 return 0;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700400 // We want to setup the atexit callback even if we are in ignored lib
401 // or after fork.
402 SCOPED_INTERCEPTOR_RAW(atexit, f);
Stephen Hines6d186232014-11-26 17:56:19 -0800403 return setup_at_exit_wrapper(thr, pc, (void(*)())f, 0, 0);
Dmitry Vyukov31c05ea2013-01-29 13:05:30 +0000404}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800405#endif
Dmitry Vyukov31c05ea2013-01-29 13:05:30 +0000406
407TSAN_INTERCEPTOR(int, __cxa_atexit, void (*f)(void *a), void *arg, void *dso) {
408 if (cur_thread()->in_symbolizer)
409 return 0;
410 SCOPED_TSAN_INTERCEPTOR(__cxa_atexit, f, arg, dso);
Stephen Hines6d186232014-11-26 17:56:19 -0800411 return setup_at_exit_wrapper(thr, pc, (void(*)())f, arg, dso);
412}
413
414static int setup_at_exit_wrapper(ThreadState *thr, uptr pc, void(*f)(),
415 void *arg, void *dso) {
416 AtExitCtx *ctx = (AtExitCtx*)__libc_malloc(sizeof(AtExitCtx));
417 ctx->f = f;
418 ctx->arg = arg;
419 Release(thr, pc, (uptr)ctx);
420 // Memory allocation in __cxa_atexit will race with free during exit,
421 // because we do not see synchronization around atexit callback list.
422 ThreadIgnoreBegin(thr, pc);
423 int res = REAL(__cxa_atexit)(at_exit_wrapper, ctx, dso);
424 ThreadIgnoreEnd(thr, pc);
425 return res;
426}
427
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800428#if !SANITIZER_MAC
Stephen Hines6d186232014-11-26 17:56:19 -0800429static void on_exit_wrapper(int status, void *arg) {
430 ThreadState *thr = cur_thread();
431 uptr pc = 0;
432 Acquire(thr, pc, (uptr)arg);
433 AtExitCtx *ctx = (AtExitCtx*)arg;
434 ((void(*)(int status, void *arg))ctx->f)(status, ctx->arg);
435 __libc_free(ctx);
436}
437
438TSAN_INTERCEPTOR(int, on_exit, void(*f)(int, void*), void *arg) {
439 if (cur_thread()->in_symbolizer)
440 return 0;
441 SCOPED_TSAN_INTERCEPTOR(on_exit, f, arg);
442 AtExitCtx *ctx = (AtExitCtx*)__libc_malloc(sizeof(AtExitCtx));
443 ctx->f = (void(*)())f;
444 ctx->arg = arg;
445 Release(thr, pc, (uptr)ctx);
446 // Memory allocation in __cxa_atexit will race with free during exit,
447 // because we do not see synchronization around atexit callback list.
448 ThreadIgnoreBegin(thr, pc);
449 int res = REAL(on_exit)(on_exit_wrapper, ctx);
450 ThreadIgnoreEnd(thr, pc);
451 return res;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000452}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800453#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000454
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000455// Cleanup old bufs.
456static void JmpBufGarbageCollect(ThreadState *thr, uptr sp) {
457 for (uptr i = 0; i < thr->jmp_bufs.Size(); i++) {
458 JmpBuf *buf = &thr->jmp_bufs[i];
459 if (buf->sp <= sp) {
460 uptr sz = thr->jmp_bufs.Size();
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800461 internal_memcpy(buf, &thr->jmp_bufs[sz - 1], sizeof(*buf));
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000462 thr->jmp_bufs.PopBack();
463 i--;
464 }
465 }
Dmitry Vyukovf037f562012-05-31 18:03:59 +0000466}
467
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000468static void SetJmp(ThreadState *thr, uptr sp, uptr mangled_sp) {
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700469 if (!thr->is_inited) // called from libc guts during bootstrap
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000470 return;
471 // Cleanup old bufs.
472 JmpBufGarbageCollect(thr, sp);
473 // Remember the buf.
474 JmpBuf *buf = thr->jmp_bufs.PushBack();
475 buf->sp = sp;
476 buf->mangled_sp = mangled_sp;
477 buf->shadow_stack_pos = thr->shadow_stack_pos;
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700478 ThreadSignalContext *sctx = SigCtx(thr);
Stephen Hines6d186232014-11-26 17:56:19 -0800479 buf->int_signal_send = sctx ? sctx->int_signal_send : 0;
480 buf->in_blocking_func = sctx ?
481 atomic_load(&sctx->in_blocking_func, memory_order_relaxed) :
482 false;
483 buf->in_signal_handler = atomic_load(&thr->in_signal_handler,
484 memory_order_relaxed);
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000485}
486
487static void LongJmp(ThreadState *thr, uptr *env) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800488#ifdef __powerpc__
489 uptr mangled_sp = env[0];
490#elif SANITIZER_FREEBSD || SANITIZER_MAC
Stephen Hines6d186232014-11-26 17:56:19 -0800491 uptr mangled_sp = env[2];
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800492#elif defined(SANITIZER_LINUX)
493# ifdef __aarch64__
494 uptr mangled_sp = env[13];
495# else
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000496 uptr mangled_sp = env[6];
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800497# endif
498#endif
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000499 // Find the saved buf by mangled_sp.
500 for (uptr i = 0; i < thr->jmp_bufs.Size(); i++) {
501 JmpBuf *buf = &thr->jmp_bufs[i];
502 if (buf->mangled_sp == mangled_sp) {
503 CHECK_GE(thr->shadow_stack_pos, buf->shadow_stack_pos);
504 // Unwind the stack.
505 while (thr->shadow_stack_pos > buf->shadow_stack_pos)
506 FuncExit(thr);
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700507 ThreadSignalContext *sctx = SigCtx(thr);
Stephen Hines6d186232014-11-26 17:56:19 -0800508 if (sctx) {
509 sctx->int_signal_send = buf->int_signal_send;
510 atomic_store(&sctx->in_blocking_func, buf->in_blocking_func,
511 memory_order_relaxed);
512 }
513 atomic_store(&thr->in_signal_handler, buf->in_signal_handler,
514 memory_order_relaxed);
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000515 JmpBufGarbageCollect(thr, buf->sp - 1); // do not collect buf->sp
516 return;
517 }
518 }
519 Printf("ThreadSanitizer: can't find longjmp buf\n");
520 CHECK(0);
521}
522
Timur Iskhodzhanovf1092672013-08-13 12:03:51 +0000523// FIXME: put everything below into a common extern "C" block?
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000524extern "C" void __tsan_setjmp(uptr sp, uptr mangled_sp) {
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000525 SetJmp(cur_thread(), sp, mangled_sp);
526}
527
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800528#if SANITIZER_MAC
529TSAN_INTERCEPTOR(int, setjmp, void *env);
530TSAN_INTERCEPTOR(int, _setjmp, void *env);
531TSAN_INTERCEPTOR(int, sigsetjmp, void *env);
532#else // SANITIZER_MAC
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000533// Not called. Merely to satisfy TSAN_INTERCEPT().
Timur Iskhodzhanovf1092672013-08-13 12:03:51 +0000534extern "C" SANITIZER_INTERFACE_ATTRIBUTE
535int __interceptor_setjmp(void *env);
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000536extern "C" int __interceptor_setjmp(void *env) {
537 CHECK(0);
538 return 0;
539}
540
Timur Iskhodzhanovf1092672013-08-13 12:03:51 +0000541// FIXME: any reason to have a separate declaration?
542extern "C" SANITIZER_INTERFACE_ATTRIBUTE
543int __interceptor__setjmp(void *env);
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000544extern "C" int __interceptor__setjmp(void *env) {
545 CHECK(0);
546 return 0;
547}
548
Timur Iskhodzhanovf1092672013-08-13 12:03:51 +0000549extern "C" SANITIZER_INTERFACE_ATTRIBUTE
550int __interceptor_sigsetjmp(void *env);
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000551extern "C" int __interceptor_sigsetjmp(void *env) {
552 CHECK(0);
553 return 0;
554}
555
Timur Iskhodzhanovf1092672013-08-13 12:03:51 +0000556extern "C" SANITIZER_INTERFACE_ATTRIBUTE
557int __interceptor___sigsetjmp(void *env);
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000558extern "C" int __interceptor___sigsetjmp(void *env) {
559 CHECK(0);
560 return 0;
561}
562
563extern "C" int setjmp(void *env);
564extern "C" int _setjmp(void *env);
565extern "C" int sigsetjmp(void *env);
566extern "C" int __sigsetjmp(void *env);
567DEFINE_REAL(int, setjmp, void *env)
568DEFINE_REAL(int, _setjmp, void *env)
569DEFINE_REAL(int, sigsetjmp, void *env)
570DEFINE_REAL(int, __sigsetjmp, void *env)
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800571#endif // SANITIZER_MAC
Dmitry Vyukov8b30c252013-03-25 10:10:44 +0000572
573TSAN_INTERCEPTOR(void, longjmp, uptr *env, int val) {
574 {
575 SCOPED_TSAN_INTERCEPTOR(longjmp, env, val);
576 }
577 LongJmp(cur_thread(), env);
578 REAL(longjmp)(env, val);
579}
580
581TSAN_INTERCEPTOR(void, siglongjmp, uptr *env, int val) {
582 {
583 SCOPED_TSAN_INTERCEPTOR(siglongjmp, env, val);
584 }
585 LongJmp(cur_thread(), env);
586 REAL(siglongjmp)(env, val);
Dmitry Vyukovf037f562012-05-31 18:03:59 +0000587}
588
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800589#if !SANITIZER_MAC
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000590TSAN_INTERCEPTOR(void*, malloc, uptr size) {
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000591 if (cur_thread()->in_symbolizer)
Dmitry Vyukov31c05ea2013-01-29 13:05:30 +0000592 return __libc_malloc(size);
Dmitry Vyukov05906a72012-05-21 17:39:40 +0000593 void *p = 0;
594 {
595 SCOPED_INTERCEPTOR_RAW(malloc, size);
596 p = user_alloc(thr, pc, size);
597 }
598 invoke_malloc_hook(p, size);
599 return p;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000600}
601
Dmitry Vyukov07ba8ef2012-11-30 17:27:58 +0000602TSAN_INTERCEPTOR(void*, __libc_memalign, uptr align, uptr sz) {
603 SCOPED_TSAN_INTERCEPTOR(__libc_memalign, align, sz);
Dmitry Vyukov543b94a2012-11-30 17:45:53 +0000604 return user_alloc(thr, pc, sz, align);
Dmitry Vyukov07ba8ef2012-11-30 17:27:58 +0000605}
606
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000607TSAN_INTERCEPTOR(void*, calloc, uptr size, uptr n) {
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000608 if (cur_thread()->in_symbolizer)
Dmitry Vyukov31c05ea2013-01-29 13:05:30 +0000609 return __libc_calloc(size, n);
Dmitry Vyukov05906a72012-05-21 17:39:40 +0000610 void *p = 0;
611 {
612 SCOPED_INTERCEPTOR_RAW(calloc, size, n);
Stephen Hines86277eb2015-03-23 12:06:32 -0700613 p = user_calloc(thr, pc, size, n);
Dmitry Vyukov05906a72012-05-21 17:39:40 +0000614 }
615 invoke_malloc_hook(p, n * size);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000616 return p;
617}
618
619TSAN_INTERCEPTOR(void*, realloc, void *p, uptr size) {
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000620 if (cur_thread()->in_symbolizer)
Dmitry Vyukov31c05ea2013-01-29 13:05:30 +0000621 return __libc_realloc(p, size);
Dmitry Vyukov05906a72012-05-21 17:39:40 +0000622 if (p)
623 invoke_free_hook(p);
624 {
625 SCOPED_INTERCEPTOR_RAW(realloc, p, size);
626 p = user_realloc(thr, pc, p, size);
627 }
628 invoke_malloc_hook(p, size);
629 return p;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000630}
631
632TSAN_INTERCEPTOR(void, free, void *p) {
633 if (p == 0)
634 return;
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000635 if (cur_thread()->in_symbolizer)
Dmitry Vyukov31c05ea2013-01-29 13:05:30 +0000636 return __libc_free(p);
Dmitry Vyukov05906a72012-05-21 17:39:40 +0000637 invoke_free_hook(p);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000638 SCOPED_INTERCEPTOR_RAW(free, p);
639 user_free(thr, pc, p);
640}
641
642TSAN_INTERCEPTOR(void, cfree, void *p) {
643 if (p == 0)
644 return;
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000645 if (cur_thread()->in_symbolizer)
Dmitry Vyukov31c05ea2013-01-29 13:05:30 +0000646 return __libc_free(p);
Dmitry Vyukov05906a72012-05-21 17:39:40 +0000647 invoke_free_hook(p);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000648 SCOPED_INTERCEPTOR_RAW(cfree, p);
649 user_free(thr, pc, p);
650}
651
Alexey Samsonov8a6b5e52013-02-25 08:43:10 +0000652TSAN_INTERCEPTOR(uptr, malloc_usable_size, void *p) {
653 SCOPED_INTERCEPTOR_RAW(malloc_usable_size, p);
Stephen Hines6a211c52014-07-21 00:49:56 -0700654 return user_alloc_usable_size(p);
Alexey Samsonov8a6b5e52013-02-25 08:43:10 +0000655}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800656#endif
Alexey Samsonov8a6b5e52013-02-25 08:43:10 +0000657
Dmitry Vyukovd51a1a12012-06-27 21:00:23 +0000658TSAN_INTERCEPTOR(uptr, strlen, const char *s) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000659 SCOPED_TSAN_INTERCEPTOR(strlen, s);
Dmitry Vyukovd51a1a12012-06-27 21:00:23 +0000660 uptr len = internal_strlen(s);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000661 MemoryAccessRange(thr, pc, (uptr)s, len + 1, false);
662 return len;
663}
664
Dmitry Vyukov065c4ac2012-06-29 15:19:14 +0000665TSAN_INTERCEPTOR(void*, memset, void *dst, int v, uptr size) {
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700666 // On FreeBSD we get here from libthr internals on thread initialization.
667 if (!COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) {
668 SCOPED_TSAN_INTERCEPTOR(memset, dst, v, size);
669 MemoryAccessRange(thr, pc, (uptr)dst, size, true);
670 }
Dmitry Vyukov065c4ac2012-06-29 15:19:14 +0000671 return internal_memset(dst, v, size);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000672}
673
Dmitry Vyukov065c4ac2012-06-29 15:19:14 +0000674TSAN_INTERCEPTOR(void*, memcpy, void *dst, const void *src, uptr size) {
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700675 // On FreeBSD we get here from libthr internals on thread initialization.
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700676 if (!COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) {
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700677 SCOPED_TSAN_INTERCEPTOR(memcpy, dst, src, size);
678 MemoryAccessRange(thr, pc, (uptr)dst, size, true);
679 MemoryAccessRange(thr, pc, (uptr)src, size, false);
680 }
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800681 // On OS X, calling internal_memcpy here will cause memory corruptions,
682 // because memcpy and memmove are actually aliases of the same implementation.
683 // We need to use internal_memmove here.
684 return internal_memmove(dst, src, size);
Dmitry Vyukovd51a1a12012-06-27 21:00:23 +0000685}
Dmitry Vyukovd51a1a12012-06-27 21:00:23 +0000686
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000687TSAN_INTERCEPTOR(void*, memmove, void *dst, void *src, uptr n) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800688 if (!COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) {
689 SCOPED_TSAN_INTERCEPTOR(memmove, dst, src, n);
690 MemoryAccessRange(thr, pc, (uptr)dst, n, true);
691 MemoryAccessRange(thr, pc, (uptr)src, n, false);
692 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000693 return REAL(memmove)(dst, src, n);
694}
695
Dmitry Vyukovd51a1a12012-06-27 21:00:23 +0000696TSAN_INTERCEPTOR(char*, strchr, char *s, int c) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000697 SCOPED_TSAN_INTERCEPTOR(strchr, s, c);
Dmitry Vyukovd51a1a12012-06-27 21:00:23 +0000698 char *res = REAL(strchr)(s, c);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700699 uptr len = internal_strlen(s);
700 uptr n = res ? (char*)res - (char*)s + 1 : len + 1;
701 READ_STRING_OF_LEN(thr, pc, s, len, n);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000702 return res;
703}
704
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800705#if !SANITIZER_MAC
Dmitry Vyukovd51a1a12012-06-27 21:00:23 +0000706TSAN_INTERCEPTOR(char*, strchrnul, char *s, int c) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000707 SCOPED_TSAN_INTERCEPTOR(strchrnul, s, c);
Dmitry Vyukovd51a1a12012-06-27 21:00:23 +0000708 char *res = REAL(strchrnul)(s, c);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000709 uptr len = (char*)res - (char*)s + 1;
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700710 READ_STRING(thr, pc, s, len);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000711 return res;
712}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800713#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000714
Dmitry Vyukovd51a1a12012-06-27 21:00:23 +0000715TSAN_INTERCEPTOR(char*, strrchr, char *s, int c) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000716 SCOPED_TSAN_INTERCEPTOR(strrchr, s, c);
Dmitry Vyukovd51a1a12012-06-27 21:00:23 +0000717 MemoryAccessRange(thr, pc, (uptr)s, internal_strlen(s) + 1, false);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000718 return REAL(strrchr)(s, c);
719}
720
Dmitry Vyukovd51a1a12012-06-27 21:00:23 +0000721TSAN_INTERCEPTOR(char*, strcpy, char *dst, const char *src) { // NOLINT
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000722 SCOPED_TSAN_INTERCEPTOR(strcpy, dst, src); // NOLINT
Dmitry Vyukovd51a1a12012-06-27 21:00:23 +0000723 uptr srclen = internal_strlen(src);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000724 MemoryAccessRange(thr, pc, (uptr)dst, srclen + 1, true);
725 MemoryAccessRange(thr, pc, (uptr)src, srclen + 1, false);
726 return REAL(strcpy)(dst, src); // NOLINT
727}
728
Dmitry Vyukovd51a1a12012-06-27 21:00:23 +0000729TSAN_INTERCEPTOR(char*, strncpy, char *dst, char *src, uptr n) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000730 SCOPED_TSAN_INTERCEPTOR(strncpy, dst, src, n);
Kostya Serebryanyb93c3d52012-07-13 13:04:43 +0000731 uptr srclen = internal_strnlen(src, n);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000732 MemoryAccessRange(thr, pc, (uptr)dst, n, true);
733 MemoryAccessRange(thr, pc, (uptr)src, min(srclen + 1, n), false);
734 return REAL(strncpy)(dst, src, n);
735}
736
Dmitry Vyukov39fa68e2013-09-21 23:44:19 +0000737TSAN_INTERCEPTOR(char*, strdup, const char *str) {
738 SCOPED_TSAN_INTERCEPTOR(strdup, str);
Dmitry Vyukov39fa68e2013-09-21 23:44:19 +0000739 // strdup will call malloc, so no instrumentation is required here.
740 return REAL(strdup)(str);
741}
742
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000743static bool fix_mmap_addr(void **addr, long_t sz, int flags) {
744 if (*addr) {
745 if (!IsAppMem((uptr)*addr) || !IsAppMem((uptr)*addr + sz - 1)) {
746 if (flags & MAP_FIXED) {
Dmitry Vyukov7a72b4a2012-06-27 13:54:46 +0000747 errno = EINVAL;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000748 return false;
749 } else {
750 *addr = 0;
751 }
752 }
753 }
754 return true;
755}
756
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800757TSAN_INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags,
758 int fd, OFF_T off) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000759 SCOPED_TSAN_INTERCEPTOR(mmap, addr, sz, prot, flags, fd, off);
760 if (!fix_mmap_addr(&addr, sz, flags))
761 return MAP_FAILED;
762 void *res = REAL(mmap)(addr, sz, prot, flags, fd, off);
763 if (res != MAP_FAILED) {
Dmitry Vyukovc05cbca2013-02-01 15:52:35 +0000764 if (fd > 0)
765 FdAccess(thr, pc, fd);
Dmitry Vyukov74172de2013-03-18 16:56:48 +0000766 MemoryRangeImitateWrite(thr, pc, (uptr)res, sz);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000767 }
768 return res;
769}
770
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800771#if SANITIZER_LINUX
772TSAN_INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags,
773 int fd, OFF64_T off) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000774 SCOPED_TSAN_INTERCEPTOR(mmap64, addr, sz, prot, flags, fd, off);
775 if (!fix_mmap_addr(&addr, sz, flags))
776 return MAP_FAILED;
777 void *res = REAL(mmap64)(addr, sz, prot, flags, fd, off);
778 if (res != MAP_FAILED) {
Dmitry Vyukovc05cbca2013-02-01 15:52:35 +0000779 if (fd > 0)
780 FdAccess(thr, pc, fd);
Dmitry Vyukov74172de2013-03-18 16:56:48 +0000781 MemoryRangeImitateWrite(thr, pc, (uptr)res, sz);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000782 }
783 return res;
784}
Stephen Hines6d186232014-11-26 17:56:19 -0800785#define TSAN_MAYBE_INTERCEPT_MMAP64 TSAN_INTERCEPT(mmap64)
786#else
787#define TSAN_MAYBE_INTERCEPT_MMAP64
788#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000789
790TSAN_INTERCEPTOR(int, munmap, void *addr, long_t sz) {
791 SCOPED_TSAN_INTERCEPTOR(munmap, addr, sz);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700792 if (sz != 0) {
793 // If sz == 0, munmap will return EINVAL and don't unmap any memory.
794 DontNeedShadowFor((uptr)addr, sz);
795 ctx->metamap.ResetRange(thr, pc, (uptr)addr, (uptr)sz);
796 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000797 int res = REAL(munmap)(addr, sz);
798 return res;
799}
800
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800801#if SANITIZER_LINUX
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000802TSAN_INTERCEPTOR(void*, memalign, uptr align, uptr sz) {
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000803 SCOPED_INTERCEPTOR_RAW(memalign, align, sz);
Dmitry Vyukov2e870512012-08-15 15:35:15 +0000804 return user_alloc(thr, pc, sz, align);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000805}
Stephen Hines6d186232014-11-26 17:56:19 -0800806#define TSAN_MAYBE_INTERCEPT_MEMALIGN TSAN_INTERCEPT(memalign)
807#else
808#define TSAN_MAYBE_INTERCEPT_MEMALIGN
809#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000810
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800811#if !SANITIZER_MAC
Stephen Hines6a211c52014-07-21 00:49:56 -0700812TSAN_INTERCEPTOR(void*, aligned_alloc, uptr align, uptr sz) {
813 SCOPED_INTERCEPTOR_RAW(memalign, align, sz);
814 return user_alloc(thr, pc, sz, align);
815}
816
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000817TSAN_INTERCEPTOR(void*, valloc, uptr sz) {
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000818 SCOPED_INTERCEPTOR_RAW(valloc, sz);
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +0000819 return user_alloc(thr, pc, sz, GetPageSizeCached());
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000820}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800821#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000822
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800823#if SANITIZER_LINUX
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000824TSAN_INTERCEPTOR(void*, pvalloc, uptr sz) {
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000825 SCOPED_INTERCEPTOR_RAW(pvalloc, sz);
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +0000826 sz = RoundUp(sz, GetPageSizeCached());
827 return user_alloc(thr, pc, sz, GetPageSizeCached());
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000828}
Stephen Hines6d186232014-11-26 17:56:19 -0800829#define TSAN_MAYBE_INTERCEPT_PVALLOC TSAN_INTERCEPT(pvalloc)
830#else
831#define TSAN_MAYBE_INTERCEPT_PVALLOC
832#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000833
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800834#if !SANITIZER_MAC
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000835TSAN_INTERCEPTOR(int, posix_memalign, void **memptr, uptr align, uptr sz) {
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000836 SCOPED_INTERCEPTOR_RAW(posix_memalign, memptr, align, sz);
Dmitry Vyukov2e870512012-08-15 15:35:15 +0000837 *memptr = user_alloc(thr, pc, sz, align);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000838 return 0;
839}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800840#endif
841
842// __cxa_guard_acquire and friends need to be intercepted in a special way -
843// regular interceptors will break statically-linked libstdc++. Linux
844// interceptors are especially defined as weak functions (so that they don't
845// cause link errors when user defines them as well). So they silently
846// auto-disable themselves when such symbol is already present in the binary. If
847// we link libstdc++ statically, it will bring own __cxa_guard_acquire which
848// will silently replace our interceptor. That's why on Linux we simply export
849// these interceptors with INTERFACE_ATTRIBUTE.
850// On OS X, we don't support statically linking, so we just use a regular
851// interceptor.
852#if SANITIZER_MAC
853#define STDCXX_INTERCEPTOR TSAN_INTERCEPTOR
854#else
855#define STDCXX_INTERCEPTOR(rettype, name, ...) \
856 extern "C" rettype INTERFACE_ATTRIBUTE name(__VA_ARGS__)
857#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000858
859// Used in thread-safe function static initialization.
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800860STDCXX_INTERCEPTOR(int, __cxa_guard_acquire, atomic_uint32_t *g) {
Dmitry Vyukov1ffeded2012-12-05 12:10:22 +0000861 SCOPED_INTERCEPTOR_RAW(__cxa_guard_acquire, g);
862 for (;;) {
863 u32 cmp = atomic_load(g, memory_order_acquire);
864 if (cmp == 0) {
865 if (atomic_compare_exchange_strong(g, &cmp, 1<<16, memory_order_relaxed))
866 return 1;
867 } else if (cmp == 1) {
868 Acquire(thr, pc, (uptr)g);
869 return 0;
870 } else {
871 internal_sched_yield();
872 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000873 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000874}
875
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800876STDCXX_INTERCEPTOR(void, __cxa_guard_release, atomic_uint32_t *g) {
Dmitry Vyukov1ffeded2012-12-05 12:10:22 +0000877 SCOPED_INTERCEPTOR_RAW(__cxa_guard_release, g);
878 Release(thr, pc, (uptr)g);
879 atomic_store(g, 1, memory_order_release);
880}
881
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800882STDCXX_INTERCEPTOR(void, __cxa_guard_abort, atomic_uint32_t *g) {
Dmitry Vyukov1ffeded2012-12-05 12:10:22 +0000883 SCOPED_INTERCEPTOR_RAW(__cxa_guard_abort, g);
884 atomic_store(g, 0, memory_order_relaxed);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000885}
886
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800887namespace __tsan {
888void DestroyThreadState() {
889 ThreadState *thr = cur_thread();
890 ThreadFinish(thr);
891 ThreadSignalContext *sctx = thr->signal_ctx;
892 if (sctx) {
893 thr->signal_ctx = 0;
894 UnmapOrDie(sctx, sizeof(*sctx));
895 }
896 cur_thread_finalize();
897}
898} // namespace __tsan
899
900#if !SANITIZER_MAC
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000901static void thread_finalize(void *v) {
902 uptr iter = (uptr)v;
903 if (iter > 1) {
904 if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000905 Printf("ThreadSanitizer: failed to set thread key\n");
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000906 Die();
907 }
908 return;
909 }
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800910 DestroyThreadState();
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000911}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800912#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000913
914
915struct ThreadParam {
916 void* (*callback)(void *arg);
917 void *param;
918 atomic_uintptr_t tid;
919};
920
921extern "C" void *__tsan_thread_start_func(void *arg) {
922 ThreadParam *p = (ThreadParam*)arg;
923 void* (*callback)(void *arg) = p->callback;
924 void *param = p->param;
925 int tid = 0;
926 {
927 ThreadState *thr = cur_thread();
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700928 // Thread-local state is not initialized yet.
929 ScopedIgnoreInterceptors ignore;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800930#if !SANITIZER_MAC
Stephen Hines6d186232014-11-26 17:56:19 -0800931 ThreadIgnoreBegin(thr, 0);
Sergey Matveev6eb836f2013-10-11 12:09:49 +0000932 if (pthread_setspecific(g_thread_finalize_key,
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800933 (void *)GetPthreadDestructorIterations())) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000934 Printf("ThreadSanitizer: failed to set thread key\n");
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000935 Die();
936 }
Stephen Hines6d186232014-11-26 17:56:19 -0800937 ThreadIgnoreEnd(thr, 0);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800938#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000939 while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0)
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800940 internal_sched_yield();
Dmitry Vyukov7dccf3f2012-10-02 11:52:05 +0000941 ThreadStart(thr, tid, GetTid());
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700942 atomic_store(&p->tid, 0, memory_order_release);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000943 }
944 void *res = callback(param);
945 // Prevent the callback from being tail called,
946 // it mixes up stack traces.
947 volatile int foo = 42;
948 foo++;
949 return res;
950}
951
952TSAN_INTERCEPTOR(int, pthread_create,
953 void *th, void *attr, void *(*callback)(void*), void * param) {
Dmitry Vyukov4af0f212013-10-03 13:37:17 +0000954 SCOPED_INTERCEPTOR_RAW(pthread_create, th, attr, callback, param);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700955 if (ctx->after_multithreaded_fork) {
956 if (flags()->die_after_fork) {
957 Report("ThreadSanitizer: starting new threads after multi-threaded "
958 "fork is not supported. Dying (set die_after_fork=0 to override)\n");
959 Die();
960 } else {
961 VPrintf(1, "ThreadSanitizer: starting new threads after multi-threaded "
962 "fork is not supported (pid %d). Continuing because of "
963 "die_after_fork=0, but you are on your own\n", internal_getpid());
964 }
965 }
Alexey Samsonovf110e352013-03-14 07:10:52 +0000966 __sanitizer_pthread_attr_t myattr;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000967 if (attr == 0) {
968 pthread_attr_init(&myattr);
969 attr = &myattr;
970 }
971 int detached = 0;
Evgeniy Stepanove236dbb2013-10-25 13:01:31 +0000972 REAL(pthread_attr_getdetachstate)(attr, &detached);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700973 AdjustStackSize(attr);
Evgeniy Stepanovb9bf7002013-03-19 09:30:52 +0000974
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000975 ThreadParam p;
976 p.callback = callback;
977 p.param = param;
978 atomic_store(&p.tid, 0, memory_order_relaxed);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700979 int res = -1;
980 {
981 // Otherwise we see false positives in pthread stack manipulation.
982 ScopedIgnoreInterceptors ignore;
983 ThreadIgnoreBegin(thr, pc);
984 res = REAL(pthread_create)(th, attr, __tsan_thread_start_func, &p);
985 ThreadIgnoreEnd(thr, pc);
986 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000987 if (res == 0) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800988 int tid = ThreadCreate(thr, pc, *(uptr*)th,
989 detached == PTHREAD_CREATE_DETACHED);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000990 CHECK_NE(tid, 0);
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700991 // Synchronization on p.tid serves two purposes:
992 // 1. ThreadCreate must finish before the new thread starts.
993 // Otherwise the new thread can call pthread_detach, but the pthread_t
994 // identifier is not yet registered in ThreadRegistry by ThreadCreate.
995 // 2. ThreadStart must finish before this thread continues.
996 // Otherwise, this thread can call pthread_detach and reset thr->sync
997 // before the new thread got a chance to acquire from it in ThreadStart.
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000998 atomic_store(&p.tid, tid, memory_order_release);
999 while (atomic_load(&p.tid, memory_order_acquire) != 0)
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001000 internal_sched_yield();
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001001 }
1002 if (attr == &myattr)
1003 pthread_attr_destroy(&myattr);
1004 return res;
1005}
1006
1007TSAN_INTERCEPTOR(int, pthread_join, void *th, void **ret) {
Dmitry Vyukov4af0f212013-10-03 13:37:17 +00001008 SCOPED_INTERCEPTOR_RAW(pthread_join, th, ret);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001009 int tid = ThreadTid(thr, pc, (uptr)th);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001010 ThreadIgnoreBegin(thr, pc);
Dmitry Vyukovee8ee242012-11-15 17:40:49 +00001011 int res = BLOCK_REAL(pthread_join)(th, ret);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001012 ThreadIgnoreEnd(thr, pc);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001013 if (res == 0) {
Dmitry Vyukov64310b22012-08-16 13:26:54 +00001014 ThreadJoin(thr, pc, tid);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001015 }
1016 return res;
1017}
1018
Stephen Hines86277eb2015-03-23 12:06:32 -07001019DEFINE_REAL_PTHREAD_FUNCTIONS
1020
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001021TSAN_INTERCEPTOR(int, pthread_detach, void *th) {
1022 SCOPED_TSAN_INTERCEPTOR(pthread_detach, th);
1023 int tid = ThreadTid(thr, pc, (uptr)th);
1024 int res = REAL(pthread_detach)(th);
1025 if (res == 0) {
Dmitry Vyukov64310b22012-08-16 13:26:54 +00001026 ThreadDetach(thr, pc, tid);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001027 }
1028 return res;
1029}
1030
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001031// Problem:
1032// NPTL implementation of pthread_cond has 2 versions (2.2.5 and 2.3.2).
1033// pthread_cond_t has different size in the different versions.
1034// If call new REAL functions for old pthread_cond_t, they will corrupt memory
1035// after pthread_cond_t (old cond is smaller).
1036// If we call old REAL functions for new pthread_cond_t, we will lose some
1037// functionality (e.g. old functions do not support waiting against
1038// CLOCK_REALTIME).
1039// Proper handling would require to have 2 versions of interceptors as well.
1040// But this is messy, in particular requires linker scripts when sanitizer
1041// runtime is linked into a shared library.
1042// Instead we assume we don't have dynamic libraries built against old
1043// pthread (2.2.5 is dated by 2002). And provide legacy_pthread_cond flag
1044// that allows to work with old libraries (but this mode does not support
1045// some features, e.g. pthread_condattr_getpshared).
1046static void *init_cond(void *c, bool force = false) {
1047 // sizeof(pthread_cond_t) >= sizeof(uptr) in both versions.
1048 // So we allocate additional memory on the side large enough to hold
1049 // any pthread_cond_t object. Always call new REAL functions, but pass
1050 // the aux object to them.
1051 // Note: the code assumes that PTHREAD_COND_INITIALIZER initializes
1052 // first word of pthread_cond_t to zero.
1053 // It's all relevant only for linux.
1054 if (!common_flags()->legacy_pthread_cond)
1055 return c;
1056 atomic_uintptr_t *p = (atomic_uintptr_t*)c;
1057 uptr cond = atomic_load(p, memory_order_acquire);
1058 if (!force && cond != 0)
1059 return (void*)cond;
1060 void *newcond = WRAP(malloc)(pthread_cond_t_sz);
1061 internal_memset(newcond, 0, pthread_cond_t_sz);
1062 if (atomic_compare_exchange_strong(p, &cond, (uptr)newcond,
1063 memory_order_acq_rel))
1064 return newcond;
1065 WRAP(free)(newcond);
1066 return (void*)cond;
1067}
1068
1069struct CondMutexUnlockCtx {
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001070 ScopedInterceptor *si;
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001071 ThreadState *thr;
1072 uptr pc;
1073 void *m;
1074};
1075
1076static void cond_mutex_unlock(CondMutexUnlockCtx *arg) {
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001077 // pthread_cond_wait interceptor has enabled async signal delivery
1078 // (see BlockingCall below). Disable async signals since we are running
1079 // tsan code. Also ScopedInterceptor and BlockingCall destructors won't run
1080 // since the thread is cancelled, so we have to manually execute them
1081 // (the thread still can run some user code due to pthread_cleanup_push).
1082 ThreadSignalContext *ctx = SigCtx(arg->thr);
1083 CHECK_EQ(atomic_load(&ctx->in_blocking_func, memory_order_relaxed), 1);
1084 atomic_store(&ctx->in_blocking_func, 0, memory_order_relaxed);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001085 MutexLock(arg->thr, arg->pc, (uptr)arg->m);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001086 // Undo BlockingCall ctor effects.
1087 arg->thr->ignore_interceptors--;
1088 arg->si->~ScopedInterceptor();
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001089}
1090
1091INTERCEPTOR(int, pthread_cond_init, void *c, void *a) {
1092 void *cond = init_cond(c, true);
1093 SCOPED_TSAN_INTERCEPTOR(pthread_cond_init, cond, a);
1094 MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), true);
1095 return REAL(pthread_cond_init)(cond, a);
1096}
1097
1098INTERCEPTOR(int, pthread_cond_wait, void *c, void *m) {
1099 void *cond = init_cond(c);
1100 SCOPED_TSAN_INTERCEPTOR(pthread_cond_wait, cond, m);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001101 MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), false);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001102 MutexUnlock(thr, pc, (uptr)m);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001103 CondMutexUnlockCtx arg = {&si, thr, pc, m};
1104 int res = 0;
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001105 // This ensures that we handle mutex lock even in case of pthread_cancel.
1106 // See test/tsan/cond_cancel.cc.
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001107 {
1108 // Enable signal delivery while the thread is blocked.
1109 BlockingCall bc(thr);
1110 res = call_pthread_cancel_with_cleanup(
1111 (int(*)(void *c, void *m, void *abstime))REAL(pthread_cond_wait),
1112 cond, m, 0, (void(*)(void *arg))cond_mutex_unlock, &arg);
1113 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001114 if (res == errno_EOWNERDEAD)
1115 MutexRepair(thr, pc, (uptr)m);
1116 MutexLock(thr, pc, (uptr)m);
1117 return res;
1118}
1119
1120INTERCEPTOR(int, pthread_cond_timedwait, void *c, void *m, void *abstime) {
1121 void *cond = init_cond(c);
1122 SCOPED_TSAN_INTERCEPTOR(pthread_cond_timedwait, cond, m, abstime);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001123 MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), false);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001124 MutexUnlock(thr, pc, (uptr)m);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001125 CondMutexUnlockCtx arg = {&si, thr, pc, m};
1126 int res = 0;
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001127 // This ensures that we handle mutex lock even in case of pthread_cancel.
1128 // See test/tsan/cond_cancel.cc.
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001129 {
1130 BlockingCall bc(thr);
1131 res = call_pthread_cancel_with_cleanup(
1132 REAL(pthread_cond_timedwait), cond, m, abstime,
1133 (void(*)(void *arg))cond_mutex_unlock, &arg);
1134 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001135 if (res == errno_EOWNERDEAD)
1136 MutexRepair(thr, pc, (uptr)m);
1137 MutexLock(thr, pc, (uptr)m);
1138 return res;
1139}
1140
1141INTERCEPTOR(int, pthread_cond_signal, void *c) {
1142 void *cond = init_cond(c);
1143 SCOPED_TSAN_INTERCEPTOR(pthread_cond_signal, cond);
1144 MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), false);
1145 return REAL(pthread_cond_signal)(cond);
1146}
1147
1148INTERCEPTOR(int, pthread_cond_broadcast, void *c) {
1149 void *cond = init_cond(c);
1150 SCOPED_TSAN_INTERCEPTOR(pthread_cond_broadcast, cond);
1151 MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), false);
1152 return REAL(pthread_cond_broadcast)(cond);
1153}
1154
1155INTERCEPTOR(int, pthread_cond_destroy, void *c) {
1156 void *cond = init_cond(c);
1157 SCOPED_TSAN_INTERCEPTOR(pthread_cond_destroy, cond);
1158 MemoryAccessRange(thr, pc, (uptr)c, sizeof(uptr), true);
1159 int res = REAL(pthread_cond_destroy)(cond);
1160 if (common_flags()->legacy_pthread_cond) {
1161 // Free our aux cond and zero the pointer to not leave dangling pointers.
1162 WRAP(free)(cond);
1163 atomic_store((atomic_uintptr_t*)c, 0, memory_order_relaxed);
1164 }
1165 return res;
1166}
1167
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001168TSAN_INTERCEPTOR(int, pthread_mutex_init, void *m, void *a) {
1169 SCOPED_TSAN_INTERCEPTOR(pthread_mutex_init, m, a);
1170 int res = REAL(pthread_mutex_init)(m, a);
1171 if (res == 0) {
1172 bool recursive = false;
1173 if (a) {
1174 int type = 0;
Stephen Hines6a211c52014-07-21 00:49:56 -07001175 if (REAL(pthread_mutexattr_gettype)(a, &type) == 0)
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001176 recursive = (type == PTHREAD_MUTEX_RECURSIVE
1177 || type == PTHREAD_MUTEX_RECURSIVE_NP);
1178 }
Dmitry Vyukovc20e9ba2012-08-16 13:29:41 +00001179 MutexCreate(thr, pc, (uptr)m, false, recursive, false);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001180 }
1181 return res;
1182}
1183
1184TSAN_INTERCEPTOR(int, pthread_mutex_destroy, void *m) {
1185 SCOPED_TSAN_INTERCEPTOR(pthread_mutex_destroy, m);
1186 int res = REAL(pthread_mutex_destroy)(m);
1187 if (res == 0 || res == EBUSY) {
Dmitry Vyukov64310b22012-08-16 13:26:54 +00001188 MutexDestroy(thr, pc, (uptr)m);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001189 }
1190 return res;
1191}
1192
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001193TSAN_INTERCEPTOR(int, pthread_mutex_trylock, void *m) {
1194 SCOPED_TSAN_INTERCEPTOR(pthread_mutex_trylock, m);
1195 int res = REAL(pthread_mutex_trylock)(m);
Dmitry Vyukov11f53092013-11-15 16:58:12 +00001196 if (res == EOWNERDEAD)
1197 MutexRepair(thr, pc, (uptr)m);
1198 if (res == 0 || res == EOWNERDEAD)
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001199 MutexLock(thr, pc, (uptr)m, /*rec=*/1, /*try_lock=*/true);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001200 return res;
1201}
1202
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001203#if !SANITIZER_MAC
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001204TSAN_INTERCEPTOR(int, pthread_mutex_timedlock, void *m, void *abstime) {
1205 SCOPED_TSAN_INTERCEPTOR(pthread_mutex_timedlock, m, abstime);
1206 int res = REAL(pthread_mutex_timedlock)(m, abstime);
1207 if (res == 0) {
Dmitry Vyukov64310b22012-08-16 13:26:54 +00001208 MutexLock(thr, pc, (uptr)m);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001209 }
1210 return res;
1211}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001212#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001213
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001214#if !SANITIZER_MAC
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001215TSAN_INTERCEPTOR(int, pthread_spin_init, void *m, int pshared) {
1216 SCOPED_TSAN_INTERCEPTOR(pthread_spin_init, m, pshared);
1217 int res = REAL(pthread_spin_init)(m, pshared);
1218 if (res == 0) {
Dmitry Vyukovc20e9ba2012-08-16 13:29:41 +00001219 MutexCreate(thr, pc, (uptr)m, false, false, false);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001220 }
1221 return res;
1222}
1223
1224TSAN_INTERCEPTOR(int, pthread_spin_destroy, void *m) {
1225 SCOPED_TSAN_INTERCEPTOR(pthread_spin_destroy, m);
1226 int res = REAL(pthread_spin_destroy)(m);
1227 if (res == 0) {
Dmitry Vyukov64310b22012-08-16 13:26:54 +00001228 MutexDestroy(thr, pc, (uptr)m);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001229 }
1230 return res;
1231}
1232
1233TSAN_INTERCEPTOR(int, pthread_spin_lock, void *m) {
1234 SCOPED_TSAN_INTERCEPTOR(pthread_spin_lock, m);
1235 int res = REAL(pthread_spin_lock)(m);
1236 if (res == 0) {
Dmitry Vyukov64310b22012-08-16 13:26:54 +00001237 MutexLock(thr, pc, (uptr)m);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001238 }
1239 return res;
1240}
1241
1242TSAN_INTERCEPTOR(int, pthread_spin_trylock, void *m) {
1243 SCOPED_TSAN_INTERCEPTOR(pthread_spin_trylock, m);
1244 int res = REAL(pthread_spin_trylock)(m);
1245 if (res == 0) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001246 MutexLock(thr, pc, (uptr)m, /*rec=*/1, /*try_lock=*/true);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001247 }
1248 return res;
1249}
1250
1251TSAN_INTERCEPTOR(int, pthread_spin_unlock, void *m) {
1252 SCOPED_TSAN_INTERCEPTOR(pthread_spin_unlock, m);
Dmitry Vyukov64310b22012-08-16 13:26:54 +00001253 MutexUnlock(thr, pc, (uptr)m);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001254 int res = REAL(pthread_spin_unlock)(m);
1255 return res;
1256}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001257#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001258
1259TSAN_INTERCEPTOR(int, pthread_rwlock_init, void *m, void *a) {
1260 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_init, m, a);
1261 int res = REAL(pthread_rwlock_init)(m, a);
1262 if (res == 0) {
Dmitry Vyukovc20e9ba2012-08-16 13:29:41 +00001263 MutexCreate(thr, pc, (uptr)m, true, false, false);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001264 }
1265 return res;
1266}
1267
1268TSAN_INTERCEPTOR(int, pthread_rwlock_destroy, void *m) {
1269 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_destroy, m);
1270 int res = REAL(pthread_rwlock_destroy)(m);
1271 if (res == 0) {
Dmitry Vyukov64310b22012-08-16 13:26:54 +00001272 MutexDestroy(thr, pc, (uptr)m);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001273 }
1274 return res;
1275}
1276
1277TSAN_INTERCEPTOR(int, pthread_rwlock_rdlock, void *m) {
1278 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_rdlock, m);
1279 int res = REAL(pthread_rwlock_rdlock)(m);
1280 if (res == 0) {
Dmitry Vyukov64310b22012-08-16 13:26:54 +00001281 MutexReadLock(thr, pc, (uptr)m);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001282 }
1283 return res;
1284}
1285
1286TSAN_INTERCEPTOR(int, pthread_rwlock_tryrdlock, void *m) {
1287 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_tryrdlock, m);
1288 int res = REAL(pthread_rwlock_tryrdlock)(m);
1289 if (res == 0) {
Stephen Hines6a211c52014-07-21 00:49:56 -07001290 MutexReadLock(thr, pc, (uptr)m, /*try_lock=*/true);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001291 }
1292 return res;
1293}
1294
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001295#if !SANITIZER_MAC
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001296TSAN_INTERCEPTOR(int, pthread_rwlock_timedrdlock, void *m, void *abstime) {
1297 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_timedrdlock, m, abstime);
1298 int res = REAL(pthread_rwlock_timedrdlock)(m, abstime);
1299 if (res == 0) {
Dmitry Vyukov64310b22012-08-16 13:26:54 +00001300 MutexReadLock(thr, pc, (uptr)m);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001301 }
1302 return res;
1303}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001304#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001305
1306TSAN_INTERCEPTOR(int, pthread_rwlock_wrlock, void *m) {
1307 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_wrlock, m);
1308 int res = REAL(pthread_rwlock_wrlock)(m);
1309 if (res == 0) {
Dmitry Vyukov64310b22012-08-16 13:26:54 +00001310 MutexLock(thr, pc, (uptr)m);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001311 }
1312 return res;
1313}
1314
1315TSAN_INTERCEPTOR(int, pthread_rwlock_trywrlock, void *m) {
1316 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_trywrlock, m);
1317 int res = REAL(pthread_rwlock_trywrlock)(m);
1318 if (res == 0) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001319 MutexLock(thr, pc, (uptr)m, /*rec=*/1, /*try_lock=*/true);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001320 }
1321 return res;
1322}
1323
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001324#if !SANITIZER_MAC
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001325TSAN_INTERCEPTOR(int, pthread_rwlock_timedwrlock, void *m, void *abstime) {
1326 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_timedwrlock, m, abstime);
1327 int res = REAL(pthread_rwlock_timedwrlock)(m, abstime);
1328 if (res == 0) {
Dmitry Vyukov64310b22012-08-16 13:26:54 +00001329 MutexLock(thr, pc, (uptr)m);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001330 }
1331 return res;
1332}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001333#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001334
1335TSAN_INTERCEPTOR(int, pthread_rwlock_unlock, void *m) {
1336 SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_unlock, m);
Dmitry Vyukov64310b22012-08-16 13:26:54 +00001337 MutexReadOrWriteUnlock(thr, pc, (uptr)m);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001338 int res = REAL(pthread_rwlock_unlock)(m);
1339 return res;
1340}
1341
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001342#if !SANITIZER_MAC
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001343TSAN_INTERCEPTOR(int, pthread_barrier_init, void *b, void *a, unsigned count) {
1344 SCOPED_TSAN_INTERCEPTOR(pthread_barrier_init, b, a, count);
Dmitry Vyukov334553e2013-02-01 09:42:06 +00001345 MemoryWrite(thr, pc, (uptr)b, kSizeLog1);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001346 int res = REAL(pthread_barrier_init)(b, a, count);
1347 return res;
1348}
1349
1350TSAN_INTERCEPTOR(int, pthread_barrier_destroy, void *b) {
1351 SCOPED_TSAN_INTERCEPTOR(pthread_barrier_destroy, b);
Dmitry Vyukov334553e2013-02-01 09:42:06 +00001352 MemoryWrite(thr, pc, (uptr)b, kSizeLog1);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001353 int res = REAL(pthread_barrier_destroy)(b);
1354 return res;
1355}
1356
1357TSAN_INTERCEPTOR(int, pthread_barrier_wait, void *b) {
1358 SCOPED_TSAN_INTERCEPTOR(pthread_barrier_wait, b);
Dmitry Vyukov64310b22012-08-16 13:26:54 +00001359 Release(thr, pc, (uptr)b);
Dmitry Vyukov334553e2013-02-01 09:42:06 +00001360 MemoryRead(thr, pc, (uptr)b, kSizeLog1);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001361 int res = REAL(pthread_barrier_wait)(b);
Dmitry Vyukov334553e2013-02-01 09:42:06 +00001362 MemoryRead(thr, pc, (uptr)b, kSizeLog1);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001363 if (res == 0 || res == PTHREAD_BARRIER_SERIAL_THREAD) {
Dmitry Vyukov64310b22012-08-16 13:26:54 +00001364 Acquire(thr, pc, (uptr)b);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001365 }
1366 return res;
1367}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001368#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001369
1370TSAN_INTERCEPTOR(int, pthread_once, void *o, void (*f)()) {
Dmitry Vyukov9fe1d5a2013-10-04 10:27:21 +00001371 SCOPED_INTERCEPTOR_RAW(pthread_once, o, f);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001372 if (o == 0 || f == 0)
1373 return EINVAL;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001374 atomic_uint32_t *a;
1375 if (!SANITIZER_MAC)
1376 a = static_cast<atomic_uint32_t*>(o);
1377 else // On OS X, pthread_once_t has a header with a long-sized signature.
1378 a = static_cast<atomic_uint32_t*>((void *)((char *)o + sizeof(long_t)));
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001379 u32 v = atomic_load(a, memory_order_acquire);
1380 if (v == 0 && atomic_compare_exchange_strong(a, &v, 1,
1381 memory_order_relaxed)) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001382 (*f)();
Dmitry Vyukov9fe1d5a2013-10-04 10:27:21 +00001383 if (!thr->in_ignored_lib)
1384 Release(thr, pc, (uptr)o);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001385 atomic_store(a, 2, memory_order_release);
1386 } else {
1387 while (v != 2) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001388 internal_sched_yield();
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001389 v = atomic_load(a, memory_order_acquire);
1390 }
Dmitry Vyukov9fe1d5a2013-10-04 10:27:21 +00001391 if (!thr->in_ignored_lib)
1392 Acquire(thr, pc, (uptr)o);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001393 }
1394 return 0;
1395}
1396
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001397#if SANITIZER_LINUX && !SANITIZER_ANDROID
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001398TSAN_INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
1399 SCOPED_TSAN_INTERCEPTOR(__xstat, version, path, buf);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001400 READ_STRING(thr, pc, path, 0);
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001401 return REAL(__xstat)(version, path, buf);
1402}
Stephen Hines6d186232014-11-26 17:56:19 -08001403#define TSAN_MAYBE_INTERCEPT___XSTAT TSAN_INTERCEPT(__xstat)
1404#else
1405#define TSAN_MAYBE_INTERCEPT___XSTAT
1406#endif
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001407
1408TSAN_INTERCEPTOR(int, stat, const char *path, void *buf) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001409#if SANITIZER_FREEBSD || SANITIZER_MAC || SANITIZER_ANDROID
Stephen Hines6d186232014-11-26 17:56:19 -08001410 SCOPED_TSAN_INTERCEPTOR(stat, path, buf);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001411 READ_STRING(thr, pc, path, 0);
Stephen Hines6d186232014-11-26 17:56:19 -08001412 return REAL(stat)(path, buf);
1413#else
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001414 SCOPED_TSAN_INTERCEPTOR(__xstat, 0, path, buf);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001415 READ_STRING(thr, pc, path, 0);
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001416 return REAL(__xstat)(0, path, buf);
Stephen Hines6d186232014-11-26 17:56:19 -08001417#endif
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001418}
1419
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001420#if SANITIZER_LINUX && !SANITIZER_ANDROID
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001421TSAN_INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
1422 SCOPED_TSAN_INTERCEPTOR(__xstat64, version, path, buf);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001423 READ_STRING(thr, pc, path, 0);
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001424 return REAL(__xstat64)(version, path, buf);
1425}
Stephen Hines6d186232014-11-26 17:56:19 -08001426#define TSAN_MAYBE_INTERCEPT___XSTAT64 TSAN_INTERCEPT(__xstat64)
1427#else
1428#define TSAN_MAYBE_INTERCEPT___XSTAT64
1429#endif
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001430
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001431#if SANITIZER_LINUX && !SANITIZER_ANDROID
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001432TSAN_INTERCEPTOR(int, stat64, const char *path, void *buf) {
1433 SCOPED_TSAN_INTERCEPTOR(__xstat64, 0, path, buf);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001434 READ_STRING(thr, pc, path, 0);
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001435 return REAL(__xstat64)(0, path, buf);
1436}
Stephen Hines6d186232014-11-26 17:56:19 -08001437#define TSAN_MAYBE_INTERCEPT_STAT64 TSAN_INTERCEPT(stat64)
1438#else
1439#define TSAN_MAYBE_INTERCEPT_STAT64
1440#endif
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001441
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001442#if SANITIZER_LINUX && !SANITIZER_ANDROID
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001443TSAN_INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
1444 SCOPED_TSAN_INTERCEPTOR(__lxstat, version, path, buf);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001445 READ_STRING(thr, pc, path, 0);
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001446 return REAL(__lxstat)(version, path, buf);
1447}
Stephen Hines6d186232014-11-26 17:56:19 -08001448#define TSAN_MAYBE_INTERCEPT___LXSTAT TSAN_INTERCEPT(__lxstat)
1449#else
1450#define TSAN_MAYBE_INTERCEPT___LXSTAT
1451#endif
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001452
1453TSAN_INTERCEPTOR(int, lstat, const char *path, void *buf) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001454#if SANITIZER_FREEBSD || SANITIZER_MAC || SANITIZER_ANDROID
Stephen Hines6d186232014-11-26 17:56:19 -08001455 SCOPED_TSAN_INTERCEPTOR(lstat, path, buf);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001456 READ_STRING(thr, pc, path, 0);
Stephen Hines6d186232014-11-26 17:56:19 -08001457 return REAL(lstat)(path, buf);
1458#else
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001459 SCOPED_TSAN_INTERCEPTOR(__lxstat, 0, path, buf);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001460 READ_STRING(thr, pc, path, 0);
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001461 return REAL(__lxstat)(0, path, buf);
Stephen Hines6d186232014-11-26 17:56:19 -08001462#endif
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001463}
1464
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001465#if SANITIZER_LINUX && !SANITIZER_ANDROID
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001466TSAN_INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
1467 SCOPED_TSAN_INTERCEPTOR(__lxstat64, version, path, buf);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001468 READ_STRING(thr, pc, path, 0);
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001469 return REAL(__lxstat64)(version, path, buf);
1470}
Stephen Hines6d186232014-11-26 17:56:19 -08001471#define TSAN_MAYBE_INTERCEPT___LXSTAT64 TSAN_INTERCEPT(__lxstat64)
1472#else
1473#define TSAN_MAYBE_INTERCEPT___LXSTAT64
1474#endif
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001475
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001476#if SANITIZER_LINUX && !SANITIZER_ANDROID
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001477TSAN_INTERCEPTOR(int, lstat64, const char *path, void *buf) {
1478 SCOPED_TSAN_INTERCEPTOR(__lxstat64, 0, path, buf);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001479 READ_STRING(thr, pc, path, 0);
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001480 return REAL(__lxstat64)(0, path, buf);
1481}
Stephen Hines6d186232014-11-26 17:56:19 -08001482#define TSAN_MAYBE_INTERCEPT_LSTAT64 TSAN_INTERCEPT(lstat64)
1483#else
1484#define TSAN_MAYBE_INTERCEPT_LSTAT64
1485#endif
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001486
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001487#if SANITIZER_LINUX && !SANITIZER_ANDROID
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001488TSAN_INTERCEPTOR(int, __fxstat, int version, int fd, void *buf) {
1489 SCOPED_TSAN_INTERCEPTOR(__fxstat, version, fd, buf);
Dmitry Vyukovc05cbca2013-02-01 15:52:35 +00001490 if (fd > 0)
1491 FdAccess(thr, pc, fd);
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001492 return REAL(__fxstat)(version, fd, buf);
1493}
Stephen Hines6d186232014-11-26 17:56:19 -08001494#define TSAN_MAYBE_INTERCEPT___FXSTAT TSAN_INTERCEPT(__fxstat)
1495#else
1496#define TSAN_MAYBE_INTERCEPT___FXSTAT
1497#endif
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001498
1499TSAN_INTERCEPTOR(int, fstat, int fd, void *buf) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001500#if SANITIZER_FREEBSD || SANITIZER_MAC || SANITIZER_ANDROID
Stephen Hines6d186232014-11-26 17:56:19 -08001501 SCOPED_TSAN_INTERCEPTOR(fstat, fd, buf);
1502 if (fd > 0)
1503 FdAccess(thr, pc, fd);
1504 return REAL(fstat)(fd, buf);
1505#else
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001506 SCOPED_TSAN_INTERCEPTOR(__fxstat, 0, fd, buf);
Dmitry Vyukovc05cbca2013-02-01 15:52:35 +00001507 if (fd > 0)
1508 FdAccess(thr, pc, fd);
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001509 return REAL(__fxstat)(0, fd, buf);
Stephen Hines6d186232014-11-26 17:56:19 -08001510#endif
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001511}
1512
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001513#if SANITIZER_LINUX && !SANITIZER_ANDROID
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001514TSAN_INTERCEPTOR(int, __fxstat64, int version, int fd, void *buf) {
1515 SCOPED_TSAN_INTERCEPTOR(__fxstat64, version, fd, buf);
Dmitry Vyukovc05cbca2013-02-01 15:52:35 +00001516 if (fd > 0)
1517 FdAccess(thr, pc, fd);
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001518 return REAL(__fxstat64)(version, fd, buf);
1519}
Stephen Hines6d186232014-11-26 17:56:19 -08001520#define TSAN_MAYBE_INTERCEPT___FXSTAT64 TSAN_INTERCEPT(__fxstat64)
1521#else
1522#define TSAN_MAYBE_INTERCEPT___FXSTAT64
1523#endif
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001524
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001525#if SANITIZER_LINUX && !SANITIZER_ANDROID
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001526TSAN_INTERCEPTOR(int, fstat64, int fd, void *buf) {
1527 SCOPED_TSAN_INTERCEPTOR(__fxstat64, 0, fd, buf);
Dmitry Vyukovc05cbca2013-02-01 15:52:35 +00001528 if (fd > 0)
1529 FdAccess(thr, pc, fd);
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001530 return REAL(__fxstat64)(0, fd, buf);
1531}
Stephen Hines6d186232014-11-26 17:56:19 -08001532#define TSAN_MAYBE_INTERCEPT_FSTAT64 TSAN_INTERCEPT(fstat64)
1533#else
1534#define TSAN_MAYBE_INTERCEPT_FSTAT64
1535#endif
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00001536
Dmitry Vyukovba3ae352012-12-07 18:30:40 +00001537TSAN_INTERCEPTOR(int, open, const char *name, int flags, int mode) {
1538 SCOPED_TSAN_INTERCEPTOR(open, name, flags, mode);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001539 READ_STRING(thr, pc, name, 0);
Dmitry Vyukovba3ae352012-12-07 18:30:40 +00001540 int fd = REAL(open)(name, flags, mode);
1541 if (fd >= 0)
Dmitry Vyukov68230a12012-12-07 19:23:59 +00001542 FdFileCreate(thr, pc, fd);
Dmitry Vyukovba3ae352012-12-07 18:30:40 +00001543 return fd;
1544}
1545
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001546#if SANITIZER_LINUX
Dmitry Vyukovc78839f2012-12-12 11:59:30 +00001547TSAN_INTERCEPTOR(int, open64, const char *name, int flags, int mode) {
1548 SCOPED_TSAN_INTERCEPTOR(open64, name, flags, mode);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001549 READ_STRING(thr, pc, name, 0);
Dmitry Vyukovc78839f2012-12-12 11:59:30 +00001550 int fd = REAL(open64)(name, flags, mode);
1551 if (fd >= 0)
1552 FdFileCreate(thr, pc, fd);
1553 return fd;
1554}
Stephen Hines6d186232014-11-26 17:56:19 -08001555#define TSAN_MAYBE_INTERCEPT_OPEN64 TSAN_INTERCEPT(open64)
1556#else
1557#define TSAN_MAYBE_INTERCEPT_OPEN64
1558#endif
Dmitry Vyukovc78839f2012-12-12 11:59:30 +00001559
Dmitry Vyukovba3ae352012-12-07 18:30:40 +00001560TSAN_INTERCEPTOR(int, creat, const char *name, int mode) {
1561 SCOPED_TSAN_INTERCEPTOR(creat, name, mode);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001562 READ_STRING(thr, pc, name, 0);
Dmitry Vyukovba3ae352012-12-07 18:30:40 +00001563 int fd = REAL(creat)(name, mode);
1564 if (fd >= 0)
Dmitry Vyukov68230a12012-12-07 19:23:59 +00001565 FdFileCreate(thr, pc, fd);
Dmitry Vyukovba3ae352012-12-07 18:30:40 +00001566 return fd;
1567}
1568
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001569#if SANITIZER_LINUX
Dmitry Vyukovc78839f2012-12-12 11:59:30 +00001570TSAN_INTERCEPTOR(int, creat64, const char *name, int mode) {
1571 SCOPED_TSAN_INTERCEPTOR(creat64, name, mode);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001572 READ_STRING(thr, pc, name, 0);
Dmitry Vyukovc78839f2012-12-12 11:59:30 +00001573 int fd = REAL(creat64)(name, mode);
1574 if (fd >= 0)
1575 FdFileCreate(thr, pc, fd);
1576 return fd;
1577}
Stephen Hines6d186232014-11-26 17:56:19 -08001578#define TSAN_MAYBE_INTERCEPT_CREAT64 TSAN_INTERCEPT(creat64)
1579#else
1580#define TSAN_MAYBE_INTERCEPT_CREAT64
1581#endif
Dmitry Vyukovc78839f2012-12-12 11:59:30 +00001582
Dmitry Vyukovba3ae352012-12-07 18:30:40 +00001583TSAN_INTERCEPTOR(int, dup, int oldfd) {
1584 SCOPED_TSAN_INTERCEPTOR(dup, oldfd);
1585 int newfd = REAL(dup)(oldfd);
Dmitry Vyukovb843c1f2012-12-24 11:43:34 +00001586 if (oldfd >= 0 && newfd >= 0 && newfd != oldfd)
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001587 FdDup(thr, pc, oldfd, newfd, true);
Dmitry Vyukovba3ae352012-12-07 18:30:40 +00001588 return newfd;
1589}
1590
1591TSAN_INTERCEPTOR(int, dup2, int oldfd, int newfd) {
1592 SCOPED_TSAN_INTERCEPTOR(dup2, oldfd, newfd);
1593 int newfd2 = REAL(dup2)(oldfd, newfd);
Dmitry Vyukovb843c1f2012-12-24 11:43:34 +00001594 if (oldfd >= 0 && newfd2 >= 0 && newfd2 != oldfd)
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001595 FdDup(thr, pc, oldfd, newfd2, false);
Dmitry Vyukovba3ae352012-12-07 18:30:40 +00001596 return newfd2;
1597}
1598
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001599#if !SANITIZER_MAC
Dmitry Vyukovba3ae352012-12-07 18:30:40 +00001600TSAN_INTERCEPTOR(int, dup3, int oldfd, int newfd, int flags) {
1601 SCOPED_TSAN_INTERCEPTOR(dup3, oldfd, newfd, flags);
1602 int newfd2 = REAL(dup3)(oldfd, newfd, flags);
Dmitry Vyukovb843c1f2012-12-24 11:43:34 +00001603 if (oldfd >= 0 && newfd2 >= 0 && newfd2 != oldfd)
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001604 FdDup(thr, pc, oldfd, newfd2, false);
Dmitry Vyukovba3ae352012-12-07 18:30:40 +00001605 return newfd2;
1606}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001607#endif
Dmitry Vyukovba3ae352012-12-07 18:30:40 +00001608
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001609#if SANITIZER_LINUX
Dmitry Vyukov68230a12012-12-07 19:23:59 +00001610TSAN_INTERCEPTOR(int, eventfd, unsigned initval, int flags) {
1611 SCOPED_TSAN_INTERCEPTOR(eventfd, initval, flags);
1612 int fd = REAL(eventfd)(initval, flags);
1613 if (fd >= 0)
1614 FdEventCreate(thr, pc, fd);
1615 return fd;
1616}
Stephen Hines6d186232014-11-26 17:56:19 -08001617#define TSAN_MAYBE_INTERCEPT_EVENTFD TSAN_INTERCEPT(eventfd)
1618#else
1619#define TSAN_MAYBE_INTERCEPT_EVENTFD
1620#endif
Dmitry Vyukov68230a12012-12-07 19:23:59 +00001621
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001622#if SANITIZER_LINUX
Dmitry Vyukov45d43242012-12-18 12:35:31 +00001623TSAN_INTERCEPTOR(int, signalfd, int fd, void *mask, int flags) {
1624 SCOPED_TSAN_INTERCEPTOR(signalfd, fd, mask, flags);
Dmitry Vyukovb843c1f2012-12-24 11:43:34 +00001625 if (fd >= 0)
1626 FdClose(thr, pc, fd);
Dmitry Vyukov45d43242012-12-18 12:35:31 +00001627 fd = REAL(signalfd)(fd, mask, flags);
1628 if (fd >= 0)
1629 FdSignalCreate(thr, pc, fd);
1630 return fd;
1631}
Stephen Hines6d186232014-11-26 17:56:19 -08001632#define TSAN_MAYBE_INTERCEPT_SIGNALFD TSAN_INTERCEPT(signalfd)
1633#else
1634#define TSAN_MAYBE_INTERCEPT_SIGNALFD
1635#endif
Dmitry Vyukov45d43242012-12-18 12:35:31 +00001636
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001637#if SANITIZER_LINUX
Dmitry Vyukovb75c67b2012-12-19 06:59:45 +00001638TSAN_INTERCEPTOR(int, inotify_init, int fake) {
1639 SCOPED_TSAN_INTERCEPTOR(inotify_init, fake);
1640 int fd = REAL(inotify_init)(fake);
Dmitry Vyukov45d43242012-12-18 12:35:31 +00001641 if (fd >= 0)
1642 FdInotifyCreate(thr, pc, fd);
1643 return fd;
1644}
Stephen Hines6d186232014-11-26 17:56:19 -08001645#define TSAN_MAYBE_INTERCEPT_INOTIFY_INIT TSAN_INTERCEPT(inotify_init)
1646#else
1647#define TSAN_MAYBE_INTERCEPT_INOTIFY_INIT
1648#endif
Dmitry Vyukov45d43242012-12-18 12:35:31 +00001649
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001650#if SANITIZER_LINUX
Dmitry Vyukov45d43242012-12-18 12:35:31 +00001651TSAN_INTERCEPTOR(int, inotify_init1, int flags) {
1652 SCOPED_TSAN_INTERCEPTOR(inotify_init1, flags);
1653 int fd = REAL(inotify_init1)(flags);
1654 if (fd >= 0)
1655 FdInotifyCreate(thr, pc, fd);
1656 return fd;
1657}
Stephen Hines6d186232014-11-26 17:56:19 -08001658#define TSAN_MAYBE_INTERCEPT_INOTIFY_INIT1 TSAN_INTERCEPT(inotify_init1)
1659#else
1660#define TSAN_MAYBE_INTERCEPT_INOTIFY_INIT1
1661#endif
Dmitry Vyukov45d43242012-12-18 12:35:31 +00001662
Dmitry Vyukov68230a12012-12-07 19:23:59 +00001663TSAN_INTERCEPTOR(int, socket, int domain, int type, int protocol) {
1664 SCOPED_TSAN_INTERCEPTOR(socket, domain, type, protocol);
1665 int fd = REAL(socket)(domain, type, protocol);
1666 if (fd >= 0)
1667 FdSocketCreate(thr, pc, fd);
1668 return fd;
1669}
1670
Dmitry Vyukov983518e2012-12-14 09:57:42 +00001671TSAN_INTERCEPTOR(int, socketpair, int domain, int type, int protocol, int *fd) {
1672 SCOPED_TSAN_INTERCEPTOR(socketpair, domain, type, protocol, fd);
1673 int res = REAL(socketpair)(domain, type, protocol, fd);
1674 if (res == 0 && fd[0] >= 0 && fd[1] >= 0)
1675 FdPipeCreate(thr, pc, fd[0], fd[1]);
1676 return res;
1677}
1678
Dmitry Vyukov68230a12012-12-07 19:23:59 +00001679TSAN_INTERCEPTOR(int, connect, int fd, void *addr, unsigned addrlen) {
1680 SCOPED_TSAN_INTERCEPTOR(connect, fd, addr, addrlen);
Dmitry Vyukoved513f62012-12-14 20:01:58 +00001681 FdSocketConnecting(thr, pc, fd);
Dmitry Vyukov68230a12012-12-07 19:23:59 +00001682 int res = REAL(connect)(fd, addr, addrlen);
Dmitry Vyukovb843c1f2012-12-24 11:43:34 +00001683 if (res == 0 && fd >= 0)
Dmitry Vyukov68230a12012-12-07 19:23:59 +00001684 FdSocketConnect(thr, pc, fd);
1685 return res;
1686}
1687
Dmitry Vyukov52c70e52013-02-04 08:06:32 +00001688TSAN_INTERCEPTOR(int, bind, int fd, void *addr, unsigned addrlen) {
1689 SCOPED_TSAN_INTERCEPTOR(bind, fd, addr, addrlen);
1690 int res = REAL(bind)(fd, addr, addrlen);
1691 if (fd > 0 && res == 0)
1692 FdAccess(thr, pc, fd);
1693 return res;
1694}
1695
1696TSAN_INTERCEPTOR(int, listen, int fd, int backlog) {
1697 SCOPED_TSAN_INTERCEPTOR(listen, fd, backlog);
1698 int res = REAL(listen)(fd, backlog);
1699 if (fd > 0 && res == 0)
1700 FdAccess(thr, pc, fd);
1701 return res;
1702}
1703
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001704#if SANITIZER_LINUX
Dmitry Vyukov68230a12012-12-07 19:23:59 +00001705TSAN_INTERCEPTOR(int, epoll_create, int size) {
1706 SCOPED_TSAN_INTERCEPTOR(epoll_create, size);
1707 int fd = REAL(epoll_create)(size);
1708 if (fd >= 0)
1709 FdPollCreate(thr, pc, fd);
1710 return fd;
1711}
Stephen Hines6d186232014-11-26 17:56:19 -08001712#define TSAN_MAYBE_INTERCEPT_EPOLL_CREATE TSAN_INTERCEPT(epoll_create)
1713#else
1714#define TSAN_MAYBE_INTERCEPT_EPOLL_CREATE
1715#endif
Dmitry Vyukov68230a12012-12-07 19:23:59 +00001716
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001717#if SANITIZER_LINUX
Dmitry Vyukov68230a12012-12-07 19:23:59 +00001718TSAN_INTERCEPTOR(int, epoll_create1, int flags) {
1719 SCOPED_TSAN_INTERCEPTOR(epoll_create1, flags);
1720 int fd = REAL(epoll_create1)(flags);
1721 if (fd >= 0)
1722 FdPollCreate(thr, pc, fd);
1723 return fd;
1724}
Stephen Hines6d186232014-11-26 17:56:19 -08001725#define TSAN_MAYBE_INTERCEPT_EPOLL_CREATE1 TSAN_INTERCEPT(epoll_create1)
1726#else
1727#define TSAN_MAYBE_INTERCEPT_EPOLL_CREATE1
1728#endif
Dmitry Vyukov68230a12012-12-07 19:23:59 +00001729
Dmitry Vyukovddeb2c32012-12-07 15:32:56 +00001730TSAN_INTERCEPTOR(int, close, int fd) {
1731 SCOPED_TSAN_INTERCEPTOR(close, fd);
Dmitry Vyukovc78839f2012-12-12 11:59:30 +00001732 if (fd >= 0)
1733 FdClose(thr, pc, fd);
Dmitry Vyukovddeb2c32012-12-07 15:32:56 +00001734 return REAL(close)(fd);
1735}
1736
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001737#if SANITIZER_LINUX
Dmitry Vyukovc78839f2012-12-12 11:59:30 +00001738TSAN_INTERCEPTOR(int, __close, int fd) {
1739 SCOPED_TSAN_INTERCEPTOR(__close, fd);
1740 if (fd >= 0)
1741 FdClose(thr, pc, fd);
1742 return REAL(__close)(fd);
1743}
Stephen Hines6d186232014-11-26 17:56:19 -08001744#define TSAN_MAYBE_INTERCEPT___CLOSE TSAN_INTERCEPT(__close)
1745#else
1746#define TSAN_MAYBE_INTERCEPT___CLOSE
1747#endif
Dmitry Vyukovc78839f2012-12-12 11:59:30 +00001748
Dmitry Vyukov03f22482013-02-07 15:27:45 +00001749// glibc guts
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001750#if SANITIZER_LINUX && !SANITIZER_ANDROID
Dmitry Vyukov03f22482013-02-07 15:27:45 +00001751TSAN_INTERCEPTOR(void, __res_iclose, void *state, bool free_addr) {
1752 SCOPED_TSAN_INTERCEPTOR(__res_iclose, state, free_addr);
1753 int fds[64];
1754 int cnt = ExtractResolvFDs(state, fds, ARRAY_SIZE(fds));
1755 for (int i = 0; i < cnt; i++) {
1756 if (fds[i] > 0)
1757 FdClose(thr, pc, fds[i]);
1758 }
1759 REAL(__res_iclose)(state, free_addr);
1760}
Stephen Hines6d186232014-11-26 17:56:19 -08001761#define TSAN_MAYBE_INTERCEPT___RES_ICLOSE TSAN_INTERCEPT(__res_iclose)
1762#else
1763#define TSAN_MAYBE_INTERCEPT___RES_ICLOSE
1764#endif
Dmitry Vyukov03f22482013-02-07 15:27:45 +00001765
Dmitry Vyukov01e96982012-12-07 16:22:54 +00001766TSAN_INTERCEPTOR(int, pipe, int *pipefd) {
Dmitry Vyukovddeb2c32012-12-07 15:32:56 +00001767 SCOPED_TSAN_INTERCEPTOR(pipe, pipefd);
1768 int res = REAL(pipe)(pipefd);
Dmitry Vyukovb843c1f2012-12-24 11:43:34 +00001769 if (res == 0 && pipefd[0] >= 0 && pipefd[1] >= 0)
Dmitry Vyukov68230a12012-12-07 19:23:59 +00001770 FdPipeCreate(thr, pc, pipefd[0], pipefd[1]);
Dmitry Vyukov01e96982012-12-07 16:22:54 +00001771 return res;
Dmitry Vyukovddeb2c32012-12-07 15:32:56 +00001772}
1773
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001774#if !SANITIZER_MAC
Dmitry Vyukov01e96982012-12-07 16:22:54 +00001775TSAN_INTERCEPTOR(int, pipe2, int *pipefd, int flags) {
Dmitry Vyukovddeb2c32012-12-07 15:32:56 +00001776 SCOPED_TSAN_INTERCEPTOR(pipe2, pipefd, flags);
1777 int res = REAL(pipe2)(pipefd, flags);
Dmitry Vyukovb843c1f2012-12-24 11:43:34 +00001778 if (res == 0 && pipefd[0] >= 0 && pipefd[1] >= 0)
Dmitry Vyukov68230a12012-12-07 19:23:59 +00001779 FdPipeCreate(thr, pc, pipefd[0], pipefd[1]);
Dmitry Vyukovddeb2c32012-12-07 15:32:56 +00001780 return res;
1781}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001782#endif
Dmitry Vyukovddeb2c32012-12-07 15:32:56 +00001783
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001784TSAN_INTERCEPTOR(long_t, send, int fd, void *buf, long_t len, int flags) {
1785 SCOPED_TSAN_INTERCEPTOR(send, fd, buf, len, flags);
Dmitry Vyukov67f55442013-10-11 14:13:11 +00001786 if (fd >= 0) {
1787 FdAccess(thr, pc, fd);
Dmitry Vyukovb843c1f2012-12-24 11:43:34 +00001788 FdRelease(thr, pc, fd);
Dmitry Vyukov67f55442013-10-11 14:13:11 +00001789 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001790 int res = REAL(send)(fd, buf, len, flags);
1791 return res;
1792}
1793
1794TSAN_INTERCEPTOR(long_t, sendmsg, int fd, void *msg, int flags) {
1795 SCOPED_TSAN_INTERCEPTOR(sendmsg, fd, msg, flags);
Dmitry Vyukov67f55442013-10-11 14:13:11 +00001796 if (fd >= 0) {
1797 FdAccess(thr, pc, fd);
Dmitry Vyukovb843c1f2012-12-24 11:43:34 +00001798 FdRelease(thr, pc, fd);
Dmitry Vyukov67f55442013-10-11 14:13:11 +00001799 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001800 int res = REAL(sendmsg)(fd, msg, flags);
1801 return res;
1802}
1803
1804TSAN_INTERCEPTOR(long_t, recv, int fd, void *buf, long_t len, int flags) {
1805 SCOPED_TSAN_INTERCEPTOR(recv, fd, buf, len, flags);
Dmitry Vyukov67f55442013-10-11 14:13:11 +00001806 if (fd >= 0)
1807 FdAccess(thr, pc, fd);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001808 int res = REAL(recv)(fd, buf, len, flags);
Dmitry Vyukovb843c1f2012-12-24 11:43:34 +00001809 if (res >= 0 && fd >= 0) {
Dmitry Vyukov3f4e6d92012-12-07 14:40:10 +00001810 FdAcquire(thr, pc, fd);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001811 }
1812 return res;
1813}
1814
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001815TSAN_INTERCEPTOR(int, unlink, char *path) {
1816 SCOPED_TSAN_INTERCEPTOR(unlink, path);
Dmitry Vyukovc78839f2012-12-12 11:59:30 +00001817 Release(thr, pc, File2addr(path));
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001818 int res = REAL(unlink)(path);
1819 return res;
1820}
1821
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001822TSAN_INTERCEPTOR(void*, tmpfile, int fake) {
1823 SCOPED_TSAN_INTERCEPTOR(tmpfile, fake);
1824 void *res = REAL(tmpfile)(fake);
Dmitry Vyukovc78839f2012-12-12 11:59:30 +00001825 if (res) {
Dmitry Vyukovf17b3822013-01-09 11:54:52 +00001826 int fd = fileno_unlocked(res);
Dmitry Vyukovc78839f2012-12-12 11:59:30 +00001827 if (fd >= 0)
1828 FdFileCreate(thr, pc, fd);
1829 }
1830 return res;
1831}
1832
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001833#if SANITIZER_LINUX
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001834TSAN_INTERCEPTOR(void*, tmpfile64, int fake) {
1835 SCOPED_TSAN_INTERCEPTOR(tmpfile64, fake);
1836 void *res = REAL(tmpfile64)(fake);
Dmitry Vyukovc78839f2012-12-12 11:59:30 +00001837 if (res) {
Dmitry Vyukovf17b3822013-01-09 11:54:52 +00001838 int fd = fileno_unlocked(res);
Dmitry Vyukovc78839f2012-12-12 11:59:30 +00001839 if (fd >= 0)
1840 FdFileCreate(thr, pc, fd);
1841 }
1842 return res;
1843}
Stephen Hines6d186232014-11-26 17:56:19 -08001844#define TSAN_MAYBE_INTERCEPT_TMPFILE64 TSAN_INTERCEPT(tmpfile64)
1845#else
1846#define TSAN_MAYBE_INTERCEPT_TMPFILE64
1847#endif
Dmitry Vyukovc78839f2012-12-12 11:59:30 +00001848
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001849TSAN_INTERCEPTOR(uptr, fread, void *ptr, uptr size, uptr nmemb, void *f) {
Dmitry Vyukov8aa3d182013-11-01 19:15:32 +00001850 // libc file streams can call user-supplied functions, see fopencookie.
Dmitry Vyukovf17b3822013-01-09 11:54:52 +00001851 {
1852 SCOPED_TSAN_INTERCEPTOR(fread, ptr, size, nmemb, f);
1853 MemoryAccessRange(thr, pc, (uptr)ptr, size * nmemb, true);
1854 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001855 return REAL(fread)(ptr, size, nmemb, f);
1856}
1857
1858TSAN_INTERCEPTOR(uptr, fwrite, const void *p, uptr size, uptr nmemb, void *f) {
Dmitry Vyukov8aa3d182013-11-01 19:15:32 +00001859 // libc file streams can call user-supplied functions, see fopencookie.
Dmitry Vyukovf17b3822013-01-09 11:54:52 +00001860 {
1861 SCOPED_TSAN_INTERCEPTOR(fwrite, p, size, nmemb, f);
1862 MemoryAccessRange(thr, pc, (uptr)p, size * nmemb, false);
1863 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001864 return REAL(fwrite)(p, size, nmemb, f);
1865}
1866
Stephen Hines86277eb2015-03-23 12:06:32 -07001867static void FlushStreams() {
1868 // Flushing all the streams here may freeze the process if a child thread is
1869 // performing file stream operations at the same time.
1870 REAL(fflush)(stdout);
1871 REAL(fflush)(stderr);
1872}
1873
Dmitry Vyukov5043f052013-03-21 12:50:43 +00001874TSAN_INTERCEPTOR(void, abort, int fake) {
1875 SCOPED_TSAN_INTERCEPTOR(abort, fake);
Stephen Hines86277eb2015-03-23 12:06:32 -07001876 FlushStreams();
Dmitry Vyukov5043f052013-03-21 12:50:43 +00001877 REAL(abort)(fake);
1878}
1879
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001880TSAN_INTERCEPTOR(int, puts, const char *s) {
1881 SCOPED_TSAN_INTERCEPTOR(puts, s);
Dmitry Vyukovd51a1a12012-06-27 21:00:23 +00001882 MemoryAccessRange(thr, pc, (uptr)s, internal_strlen(s), false);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001883 return REAL(puts)(s);
1884}
1885
1886TSAN_INTERCEPTOR(int, rmdir, char *path) {
1887 SCOPED_TSAN_INTERCEPTOR(rmdir, path);
Dmitry Vyukovc78839f2012-12-12 11:59:30 +00001888 Release(thr, pc, Dir2addr(path));
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001889 int res = REAL(rmdir)(path);
1890 return res;
1891}
1892
Stephen Hines86277eb2015-03-23 12:06:32 -07001893TSAN_INTERCEPTOR(int, closedir, void *dirp) {
1894 SCOPED_TSAN_INTERCEPTOR(closedir, dirp);
1895 int fd = dirfd(dirp);
1896 FdClose(thr, pc, fd);
1897 return REAL(closedir)(dirp);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001898}
1899
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001900#if SANITIZER_LINUX
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001901TSAN_INTERCEPTOR(int, epoll_ctl, int epfd, int op, int fd, void *ev) {
1902 SCOPED_TSAN_INTERCEPTOR(epoll_ctl, epfd, op, fd, ev);
Dmitry Vyukov29cec482013-10-11 13:33:22 +00001903 if (epfd >= 0)
1904 FdAccess(thr, pc, epfd);
1905 if (epfd >= 0 && fd >= 0)
Dmitry Vyukovf5d42732013-01-10 12:28:58 +00001906 FdAccess(thr, pc, fd);
Dmitry Vyukov29cec482013-10-11 13:33:22 +00001907 if (op == EPOLL_CTL_ADD && epfd >= 0)
1908 FdRelease(thr, pc, epfd);
1909 int res = REAL(epoll_ctl)(epfd, op, fd, ev);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001910 return res;
1911}
Stephen Hines6d186232014-11-26 17:56:19 -08001912#define TSAN_MAYBE_INTERCEPT_EPOLL_CTL TSAN_INTERCEPT(epoll_ctl)
1913#else
1914#define TSAN_MAYBE_INTERCEPT_EPOLL_CTL
1915#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001916
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001917#if SANITIZER_LINUX
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001918TSAN_INTERCEPTOR(int, epoll_wait, int epfd, void *ev, int cnt, int timeout) {
1919 SCOPED_TSAN_INTERCEPTOR(epoll_wait, epfd, ev, cnt, timeout);
Dmitry Vyukov29cec482013-10-11 13:33:22 +00001920 if (epfd >= 0)
1921 FdAccess(thr, pc, epfd);
Dmitry Vyukovee8ee242012-11-15 17:40:49 +00001922 int res = BLOCK_REAL(epoll_wait)(epfd, ev, cnt, timeout);
Dmitry Vyukov29cec482013-10-11 13:33:22 +00001923 if (res > 0 && epfd >= 0)
Dmitry Vyukov3f4e6d92012-12-07 14:40:10 +00001924 FdAcquire(thr, pc, epfd);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001925 return res;
1926}
Stephen Hines6d186232014-11-26 17:56:19 -08001927#define TSAN_MAYBE_INTERCEPT_EPOLL_WAIT TSAN_INTERCEPT(epoll_wait)
1928#else
1929#define TSAN_MAYBE_INTERCEPT_EPOLL_WAIT
1930#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +00001931
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001932namespace __tsan {
1933
Stephen Hines6d186232014-11-26 17:56:19 -08001934static void CallUserSignalHandler(ThreadState *thr, bool sync, bool acquire,
1935 bool sigact, int sig, my_siginfo_t *info, void *uctx) {
1936 if (acquire)
1937 Acquire(thr, 0, (uptr)&sigactions[sig]);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001938 // Ensure that the handler does not spoil errno.
1939 const int saved_errno = errno;
1940 errno = 99;
Stephen Hines86277eb2015-03-23 12:06:32 -07001941 // This code races with sigaction. Be careful to not read sa_sigaction twice.
1942 // Also need to remember pc for reporting before the call,
1943 // because the handler can reset it.
1944 volatile uptr pc = sigact ?
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001945 (uptr)sigactions[sig].sa_sigaction :
1946 (uptr)sigactions[sig].sa_handler;
Stephen Hines86277eb2015-03-23 12:06:32 -07001947 if (pc != (uptr)SIG_DFL && pc != (uptr)SIG_IGN) {
1948 if (sigact)
1949 ((sigactionhandler_t)pc)(sig, info, uctx);
1950 else
1951 ((sighandler_t)pc)(sig);
1952 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001953 // We do not detect errno spoiling for SIGTERM,
1954 // because some SIGTERM handlers do spoil errno but reraise SIGTERM,
1955 // tsan reports false positive in such case.
1956 // It's difficult to properly detect this situation (reraise),
1957 // because in async signal processing case (when handler is called directly
1958 // from rtl_generic_sighandler) we have not yet received the reraised
1959 // signal; and it looks too fragile to intercept all ways to reraise a signal.
1960 if (flags()->report_bugs && !sync && sig != SIGTERM && errno != 99) {
Stephen Hines6d186232014-11-26 17:56:19 -08001961 VarSizeStackTrace stack;
Stephen Hines86277eb2015-03-23 12:06:32 -07001962 // StackTrace::GetNestInstructionPc(pc) is used because return address is
1963 // expected, OutputReport() will undo this.
1964 ObtainCurrentStack(thr, StackTrace::GetNextInstructionPc(pc), &stack);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001965 ThreadRegistryLock l(ctx->thread_registry);
1966 ScopedReport rep(ReportTypeErrnoInSignal);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001967 if (!IsFiredSuppression(ctx, ReportTypeErrnoInSignal, stack)) {
Stephen Hines6d186232014-11-26 17:56:19 -08001968 rep.AddStack(stack, true);
Stephen Hines6a211c52014-07-21 00:49:56 -07001969 OutputReport(thr, rep);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001970 }
1971 }
1972 errno = saved_errno;
1973}
1974
1975void ProcessPendingSignals(ThreadState *thr) {
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -07001976 ThreadSignalContext *sctx = SigCtx(thr);
Stephen Hines6d186232014-11-26 17:56:19 -08001977 if (sctx == 0 ||
1978 atomic_load(&sctx->have_pending_signals, memory_order_relaxed) == 0)
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001979 return;
Stephen Hines6d186232014-11-26 17:56:19 -08001980 atomic_store(&sctx->have_pending_signals, 0, memory_order_relaxed);
1981 atomic_fetch_add(&thr->in_signal_handler, 1, memory_order_relaxed);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001982 CHECK_EQ(0, REAL(sigfillset)(&sctx->emptyset));
1983 CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sctx->emptyset, &sctx->oldset));
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001984 for (int sig = 0; sig < kSigCount; sig++) {
1985 SignalDesc *signal = &sctx->pending_signals[sig];
1986 if (signal->armed) {
1987 signal->armed = false;
Stephen Hines86277eb2015-03-23 12:06:32 -07001988 CallUserSignalHandler(thr, false, true, signal->sigaction, sig,
1989 &signal->siginfo, &signal->ctx);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001990 }
1991 }
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001992 CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sctx->oldset, 0));
Stephen Hines6d186232014-11-26 17:56:19 -08001993 atomic_fetch_add(&thr->in_signal_handler, -1, memory_order_relaxed);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001994}
1995
1996} // namespace __tsan
1997
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -07001998static bool is_sync_signal(ThreadSignalContext *sctx, int sig) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001999 return sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
2000 sig == SIGABRT || sig == SIGFPE || sig == SIGPIPE || sig == SIGSYS ||
2001 // If we are sending signal to ourselves, we must process it now.
2002 (sctx && sig == sctx->int_signal_send);
2003}
2004
Timur Iskhodzhanovb157c672013-03-28 21:16:09 +00002005void ALWAYS_INLINE rtl_generic_sighandler(bool sigact, int sig,
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002006 my_siginfo_t *info, void *ctx) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002007 ThreadState *thr = cur_thread();
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -07002008 ThreadSignalContext *sctx = SigCtx(thr);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002009 if (sig < 0 || sig >= kSigCount) {
2010 VPrintf(1, "ThreadSanitizer: ignoring signal %d\n", sig);
2011 return;
2012 }
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002013 // Don't mess with synchronous signals.
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002014 const bool sync = is_sync_signal(sctx, sig);
2015 if (sync ||
Dmitry Vyukov0bf04ea2012-11-21 11:12:33 +00002016 // If we are in blocking function, we can safely process it now
2017 // (but check if we are in a recursive interceptor,
2018 // i.e. pthread_join()->munmap()).
Stephen Hines6d186232014-11-26 17:56:19 -08002019 (sctx && atomic_load(&sctx->in_blocking_func, memory_order_relaxed))) {
2020 atomic_fetch_add(&thr->in_signal_handler, 1, memory_order_relaxed);
2021 if (sctx && atomic_load(&sctx->in_blocking_func, memory_order_relaxed)) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002022 // We ignore interceptors in blocking functions,
2023 // temporary enbled them again while we are calling user function.
2024 int const i = thr->ignore_interceptors;
2025 thr->ignore_interceptors = 0;
Stephen Hines6d186232014-11-26 17:56:19 -08002026 atomic_store(&sctx->in_blocking_func, 0, memory_order_relaxed);
2027 CallUserSignalHandler(thr, sync, true, sigact, sig, info, ctx);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002028 thr->ignore_interceptors = i;
Stephen Hines6d186232014-11-26 17:56:19 -08002029 atomic_store(&sctx->in_blocking_func, 1, memory_order_relaxed);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002030 } else {
Stephen Hines6d186232014-11-26 17:56:19 -08002031 // Be very conservative with when we do acquire in this case.
2032 // It's unsafe to do acquire in async handlers, because ThreadState
2033 // can be in inconsistent state.
2034 // SIGSYS looks relatively safe -- it's synchronous and can actually
2035 // need some global state.
2036 bool acq = (sig == SIGSYS);
2037 CallUserSignalHandler(thr, sync, acq, sigact, sig, info, ctx);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002038 }
Stephen Hines6d186232014-11-26 17:56:19 -08002039 atomic_fetch_add(&thr->in_signal_handler, -1, memory_order_relaxed);
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002040 return;
2041 }
2042
Dmitry Vyukov1fc03d52012-06-28 18:07:46 +00002043 if (sctx == 0)
2044 return;
Dmitry Vyukove9636662012-06-27 16:05:06 +00002045 SignalDesc *signal = &sctx->pending_signals[sig];
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002046 if (signal->armed == false) {
2047 signal->armed = true;
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002048 signal->sigaction = sigact;
2049 if (info)
Dmitry Vyukov6e481552012-06-29 15:26:55 +00002050 internal_memcpy(&signal->siginfo, info, sizeof(*info));
Dmitry Vyukova5562db2012-06-27 17:35:35 +00002051 if (ctx)
Dmitry Vyukov6e481552012-06-29 15:26:55 +00002052 internal_memcpy(&signal->ctx, ctx, sizeof(signal->ctx));
Stephen Hines6d186232014-11-26 17:56:19 -08002053 atomic_store(&sctx->have_pending_signals, 1, memory_order_relaxed);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002054 }
2055}
2056
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002057static void rtl_sighandler(int sig) {
2058 rtl_generic_sighandler(false, sig, 0, 0);
2059}
2060
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002061static void rtl_sigaction(int sig, my_siginfo_t *info, void *ctx) {
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002062 rtl_generic_sighandler(true, sig, info, ctx);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002063}
2064
2065TSAN_INTERCEPTOR(int, sigaction, int sig, sigaction_t *act, sigaction_t *old) {
2066 SCOPED_TSAN_INTERCEPTOR(sigaction, sig, act, old);
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002067 if (old)
Dmitry Vyukov6e481552012-06-29 15:26:55 +00002068 internal_memcpy(old, &sigactions[sig], sizeof(*old));
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002069 if (act == 0)
2070 return 0;
Stephen Hines86277eb2015-03-23 12:06:32 -07002071 // Copy act into sigactions[sig].
2072 // Can't use struct copy, because compiler can emit call to memcpy.
2073 // Can't use internal_memcpy, because it copies byte-by-byte,
2074 // and signal handler reads the sa_handler concurrently. It it can read
2075 // some bytes from old value and some bytes from new value.
2076 // Use volatile to prevent insertion of memcpy.
2077 sigactions[sig].sa_handler = *(volatile sighandler_t*)&act->sa_handler;
2078 sigactions[sig].sa_flags = *(volatile int*)&act->sa_flags;
2079 internal_memcpy(&sigactions[sig].sa_mask, &act->sa_mask,
2080 sizeof(sigactions[sig].sa_mask));
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002081#if !SANITIZER_FREEBSD && !SANITIZER_MAC
Stephen Hines86277eb2015-03-23 12:06:32 -07002082 sigactions[sig].sa_restorer = act->sa_restorer;
2083#endif
Dmitry Vyukov6e481552012-06-29 15:26:55 +00002084 sigaction_t newact;
2085 internal_memcpy(&newact, act, sizeof(newact));
Evgeniy Stepanov9a949a82013-09-25 14:47:43 +00002086 REAL(sigfillset)(&newact.sa_mask);
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002087 if (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002088 if (newact.sa_flags & SA_SIGINFO)
2089 newact.sa_sigaction = rtl_sigaction;
2090 else
2091 newact.sa_handler = rtl_sighandler;
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002092 }
Stephen Hines6d186232014-11-26 17:56:19 -08002093 ReleaseStore(thr, pc, (uptr)&sigactions[sig]);
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002094 int res = REAL(sigaction)(sig, &newact, 0);
2095 return res;
2096}
2097
2098TSAN_INTERCEPTOR(sighandler_t, signal, int sig, sighandler_t h) {
Alexey Samsonov2135d8a2012-09-13 11:54:41 +00002099 sigaction_t act;
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002100 act.sa_handler = h;
Alexey Samsonov88207ab2012-06-15 12:24:07 +00002101 REAL(memset)(&act.sa_mask, -1, sizeof(act.sa_mask));
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002102 act.sa_flags = 0;
Alexey Samsonov2135d8a2012-09-13 11:54:41 +00002103 sigaction_t old;
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002104 int res = sigaction(sig, &act, &old);
2105 if (res)
2106 return SIG_ERR;
2107 return old.sa_handler;
2108}
2109
Evgeniy Stepanov9a949a82013-09-25 14:47:43 +00002110TSAN_INTERCEPTOR(int, sigsuspend, const __sanitizer_sigset_t *mask) {
Dmitry Vyukovfbeab512013-07-16 11:28:04 +00002111 SCOPED_TSAN_INTERCEPTOR(sigsuspend, mask);
2112 return REAL(sigsuspend)(mask);
2113}
2114
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002115TSAN_INTERCEPTOR(int, raise, int sig) {
2116 SCOPED_TSAN_INTERCEPTOR(raise, sig);
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -07002117 ThreadSignalContext *sctx = SigCtx(thr);
Dmitry Vyukov1fc03d52012-06-28 18:07:46 +00002118 CHECK_NE(sctx, 0);
Dmitry Vyukove9636662012-06-27 16:05:06 +00002119 int prev = sctx->int_signal_send;
2120 sctx->int_signal_send = sig;
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002121 int res = REAL(raise)(sig);
Dmitry Vyukove9636662012-06-27 16:05:06 +00002122 CHECK_EQ(sctx->int_signal_send, sig);
2123 sctx->int_signal_send = prev;
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002124 return res;
2125}
2126
2127TSAN_INTERCEPTOR(int, kill, int pid, int sig) {
2128 SCOPED_TSAN_INTERCEPTOR(kill, pid, sig);
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -07002129 ThreadSignalContext *sctx = SigCtx(thr);
Dmitry Vyukov1fc03d52012-06-28 18:07:46 +00002130 CHECK_NE(sctx, 0);
Dmitry Vyukove9636662012-06-27 16:05:06 +00002131 int prev = sctx->int_signal_send;
Peter Collingbourne0b694fc2013-05-17 16:56:53 +00002132 if (pid == (int)internal_getpid()) {
Dmitry Vyukove9636662012-06-27 16:05:06 +00002133 sctx->int_signal_send = sig;
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002134 }
2135 int res = REAL(kill)(pid, sig);
Peter Collingbourne0b694fc2013-05-17 16:56:53 +00002136 if (pid == (int)internal_getpid()) {
Dmitry Vyukove9636662012-06-27 16:05:06 +00002137 CHECK_EQ(sctx->int_signal_send, sig);
2138 sctx->int_signal_send = prev;
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002139 }
2140 return res;
2141}
2142
2143TSAN_INTERCEPTOR(int, pthread_kill, void *tid, int sig) {
2144 SCOPED_TSAN_INTERCEPTOR(pthread_kill, tid, sig);
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -07002145 ThreadSignalContext *sctx = SigCtx(thr);
Dmitry Vyukov1fc03d52012-06-28 18:07:46 +00002146 CHECK_NE(sctx, 0);
Dmitry Vyukove9636662012-06-27 16:05:06 +00002147 int prev = sctx->int_signal_send;
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002148 if (tid == pthread_self()) {
Dmitry Vyukove9636662012-06-27 16:05:06 +00002149 sctx->int_signal_send = sig;
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002150 }
2151 int res = REAL(pthread_kill)(tid, sig);
2152 if (tid == pthread_self()) {
Dmitry Vyukove9636662012-06-27 16:05:06 +00002153 CHECK_EQ(sctx->int_signal_send, sig);
2154 sctx->int_signal_send = prev;
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002155 }
2156 return res;
2157}
2158
Dmitry Vyukovee8ee242012-11-15 17:40:49 +00002159TSAN_INTERCEPTOR(int, gettimeofday, void *tv, void *tz) {
2160 SCOPED_TSAN_INTERCEPTOR(gettimeofday, tv, tz);
2161 // It's intercepted merely to process pending signals.
2162 return REAL(gettimeofday)(tv, tz);
2163}
2164
Dmitry Vyukovcf878fd2013-08-13 16:51:27 +00002165TSAN_INTERCEPTOR(int, getaddrinfo, void *node, void *service,
2166 void *hints, void *rv) {
2167 SCOPED_TSAN_INTERCEPTOR(getaddrinfo, node, service, hints, rv);
2168 // We miss atomic synchronization in getaddrinfo,
2169 // and can report false race between malloc and free
2170 // inside of getaddrinfo. So ignore memory accesses.
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002171 ThreadIgnoreBegin(thr, pc);
Dmitry Vyukovcf878fd2013-08-13 16:51:27 +00002172 int res = REAL(getaddrinfo)(node, service, hints, rv);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002173 ThreadIgnoreEnd(thr, pc);
Dmitry Vyukovcf878fd2013-08-13 16:51:27 +00002174 return res;
2175}
2176
Dmitry Vyukovb75c67b2012-12-19 06:59:45 +00002177TSAN_INTERCEPTOR(int, fork, int fake) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002178 if (cur_thread()->in_symbolizer)
2179 return REAL(fork)(fake);
Dmitry Vyukov4af0f212013-10-03 13:37:17 +00002180 SCOPED_INTERCEPTOR_RAW(fork, fake);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002181 ForkBefore(thr, pc);
Dmitry Vyukovb75c67b2012-12-19 06:59:45 +00002182 int pid = REAL(fork)(fake);
Dmitry Vyukov4554b7a2012-12-18 14:44:44 +00002183 if (pid == 0) {
2184 // child
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002185 ForkChildAfter(thr, pc);
Dmitry Vyukov4554b7a2012-12-18 14:44:44 +00002186 FdOnFork(thr, pc);
2187 } else if (pid > 0) {
2188 // parent
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002189 ForkParentAfter(thr, pc);
2190 } else {
2191 // error
2192 ForkParentAfter(thr, pc);
Dmitry Vyukov4554b7a2012-12-18 14:44:44 +00002193 }
2194 return pid;
2195}
2196
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002197TSAN_INTERCEPTOR(int, vfork, int fake) {
2198 // Some programs (e.g. openjdk) call close for all file descriptors
2199 // in the child process. Under tsan it leads to false positives, because
2200 // address space is shared, so the parent process also thinks that
2201 // the descriptors are closed (while they are actually not).
2202 // This leads to false positives due to missed synchronization.
2203 // Strictly saying this is undefined behavior, because vfork child is not
2204 // allowed to call any functions other than exec/exit. But this is what
2205 // openjdk does, so we want to handle it.
2206 // We could disable interceptors in the child process. But it's not possible
2207 // to simply intercept and wrap vfork, because vfork child is not allowed
2208 // to return from the function that calls vfork, and that's exactly what
2209 // we would do. So this would require some assembly trickery as well.
2210 // Instead we simply turn vfork into fork.
2211 return WRAP(fork)(fake);
2212}
2213
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002214#if !SANITIZER_MAC && !SANITIZER_ANDROID
2215typedef int (*dl_iterate_phdr_cb_t)(__sanitizer_dl_phdr_info *info, SIZE_T size,
2216 void *data);
2217struct dl_iterate_phdr_data {
2218 ThreadState *thr;
2219 uptr pc;
2220 dl_iterate_phdr_cb_t cb;
2221 void *data;
2222};
2223
2224static bool IsAppNotRodata(uptr addr) {
2225 return IsAppMem(addr) && *(u64*)MemToShadow(addr) != kShadowRodata;
2226}
2227
2228static int dl_iterate_phdr_cb(__sanitizer_dl_phdr_info *info, SIZE_T size,
2229 void *data) {
2230 dl_iterate_phdr_data *cbdata = (dl_iterate_phdr_data *)data;
2231 // dlopen/dlclose allocate/free dynamic-linker-internal memory, which is later
2232 // accessible in dl_iterate_phdr callback. But we don't see synchronization
2233 // inside of dynamic linker, so we "unpoison" it here in order to not
2234 // produce false reports. Ignoring malloc/free in dlopen/dlclose is not enough
2235 // because some libc functions call __libc_dlopen.
2236 if (info && IsAppNotRodata((uptr)info->dlpi_name))
2237 MemoryResetRange(cbdata->thr, cbdata->pc, (uptr)info->dlpi_name,
2238 internal_strlen(info->dlpi_name));
2239 int res = cbdata->cb(info, size, cbdata->data);
2240 // Perform the check one more time in case info->dlpi_name was overwritten
2241 // by user callback.
2242 if (info && IsAppNotRodata((uptr)info->dlpi_name))
2243 MemoryResetRange(cbdata->thr, cbdata->pc, (uptr)info->dlpi_name,
2244 internal_strlen(info->dlpi_name));
2245 return res;
2246}
2247
2248TSAN_INTERCEPTOR(int, dl_iterate_phdr, dl_iterate_phdr_cb_t cb, void *data) {
2249 SCOPED_TSAN_INTERCEPTOR(dl_iterate_phdr, cb, data);
2250 dl_iterate_phdr_data cbdata;
2251 cbdata.thr = thr;
2252 cbdata.pc = pc;
2253 cbdata.cb = cb;
2254 cbdata.data = data;
2255 int res = REAL(dl_iterate_phdr)(dl_iterate_phdr_cb, &cbdata);
2256 return res;
2257}
2258#endif
2259
Dmitry Vyukov14dd9802013-10-03 15:22:29 +00002260static int OnExit(ThreadState *thr) {
2261 int status = Finalize(thr);
Stephen Hines86277eb2015-03-23 12:06:32 -07002262 FlushStreams();
Dmitry Vyukov14dd9802013-10-03 15:22:29 +00002263 return status;
2264}
2265
Evgeniy Stepanov996c4f22013-01-18 11:17:23 +00002266struct TsanInterceptorContext {
2267 ThreadState *thr;
2268 const uptr caller_pc;
2269 const uptr pc;
2270};
2271
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002272#if !SANITIZER_MAC
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002273static void HandleRecvmsg(ThreadState *thr, uptr pc,
2274 __sanitizer_msghdr *msg) {
2275 int fds[64];
2276 int cnt = ExtractRecvmsgFDs(msg, fds, ARRAY_SIZE(fds));
2277 for (int i = 0; i < cnt; i++)
2278 FdEventCreate(thr, pc, fds[i]);
2279}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002280#endif
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002281
Dmitry Vyukovedd28212013-04-24 11:06:05 +00002282#include "sanitizer_common/sanitizer_platform_interceptors.h"
Dmitry Vyukov917f62e2013-06-10 11:35:08 +00002283// Causes interceptor recursion (getaddrinfo() and fopen())
2284#undef SANITIZER_INTERCEPT_GETADDRINFO
Stephen Hines6d186232014-11-26 17:56:19 -08002285// There interceptors do not seem to be strictly necessary for tsan.
2286// But we see cases where the interceptors consume 70% of execution time.
2287// Memory blocks passed to fgetgrent_r are "written to" by tsan several times.
2288// First, there is some recursion (getgrnam_r calls fgetgrent_r), and each
2289// function "writes to" the buffer. Then, the same memory is "written to"
2290// twice, first as buf and then as pwbufp (both of them refer to the same
2291// addresses).
2292#undef SANITIZER_INTERCEPT_GETPWENT
2293#undef SANITIZER_INTERCEPT_GETPWENT_R
2294#undef SANITIZER_INTERCEPT_FGETPWENT
2295#undef SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
2296#undef SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
Stephen Hines86277eb2015-03-23 12:06:32 -07002297// __tls_get_addr can be called with mis-aligned stack due to:
2298// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
2299// There are two potential issues:
2300// 1. Sanitizer code contains a MOVDQA spill (it does not seem to be the case
2301// right now). or 2. ProcessPendingSignal calls user handler which contains
2302// MOVDQA spill (this happens right now).
2303// Since the interceptor only initializes memory for msan, the simplest solution
2304// is to disable the interceptor in tsan (other sanitizers do not call
2305// signal handlers from COMMON_INTERCEPTOR_ENTER).
2306#undef SANITIZER_INTERCEPT_TLS_GET_ADDR
Dmitry Vyukovedd28212013-04-24 11:06:05 +00002307
Evgeniy Stepanova537ea92013-11-11 11:28:30 +00002308#define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name)
Dmitry Vyukov5cf2c462013-10-29 10:30:39 +00002309
Evgeniy Stepanov3fa122e2013-07-09 12:07:59 +00002310#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
2311 MemoryAccessRange(((TsanInterceptorContext *)ctx)->thr, \
2312 ((TsanInterceptorContext *)ctx)->pc, (uptr)ptr, size, \
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +00002313 true)
Dmitry Vyukov5cf2c462013-10-29 10:30:39 +00002314
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +00002315#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
2316 MemoryAccessRange(((TsanInterceptorContext *) ctx)->thr, \
2317 ((TsanInterceptorContext *) ctx)->pc, (uptr) ptr, size, \
2318 false)
Dmitry Vyukov5cf2c462013-10-29 10:30:39 +00002319
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +00002320#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
2321 SCOPED_TSAN_INTERCEPTOR(func, __VA_ARGS__); \
2322 TsanInterceptorContext _ctx = {thr, caller_pc, pc}; \
2323 ctx = (void *)&_ctx; \
2324 (void) ctx;
Dmitry Vyukov5cf2c462013-10-29 10:30:39 +00002325
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002326#define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, func, ...) \
2327 SCOPED_INTERCEPTOR_RAW(func, __VA_ARGS__); \
2328 TsanInterceptorContext _ctx = {thr, caller_pc, pc}; \
2329 ctx = (void *)&_ctx; \
2330 (void) ctx;
2331
2332#define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) \
2333 Acquire(thr, pc, File2addr(path)); \
2334 if (file) { \
2335 int fd = fileno_unlocked(file); \
2336 if (fd >= 0) FdFileCreate(thr, pc, fd); \
2337 }
2338
2339#define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) \
2340 if (file) { \
2341 int fd = fileno_unlocked(file); \
2342 if (fd >= 0) FdClose(thr, pc, fd); \
2343 }
2344
Stephen Hines86277eb2015-03-23 12:06:32 -07002345#define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) \
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002346 libignore()->OnLibraryLoaded(filename)
2347
2348#define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() \
2349 libignore()->OnLibraryUnloaded()
2350
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002351#define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) \
2352 Acquire(((TsanInterceptorContext *) ctx)->thr, pc, u)
2353
2354#define COMMON_INTERCEPTOR_RELEASE(ctx, u) \
2355 Release(((TsanInterceptorContext *) ctx)->thr, pc, u)
2356
Stephen Hines86277eb2015-03-23 12:06:32 -07002357#define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
2358 Acquire(((TsanInterceptorContext *) ctx)->thr, pc, Dir2addr(path))
2359
Evgeniy Stepanov996c4f22013-01-18 11:17:23 +00002360#define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +00002361 FdAcquire(((TsanInterceptorContext *) ctx)->thr, pc, fd)
Dmitry Vyukov5cf2c462013-10-29 10:30:39 +00002362
Evgeniy Stepanov996c4f22013-01-18 11:17:23 +00002363#define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +00002364 FdRelease(((TsanInterceptorContext *) ctx)->thr, pc, fd)
Dmitry Vyukov5cf2c462013-10-29 10:30:39 +00002365
Dmitry Vyukov67f55442013-10-11 14:13:11 +00002366#define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) \
2367 FdAccess(((TsanInterceptorContext *) ctx)->thr, pc, fd)
Dmitry Vyukov5cf2c462013-10-29 10:30:39 +00002368
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +00002369#define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
2370 FdSocketAccept(((TsanInterceptorContext *) ctx)->thr, pc, fd, newfd)
Dmitry Vyukov5cf2c462013-10-29 10:30:39 +00002371
Evgeniy Stepanov996c4f22013-01-18 11:17:23 +00002372#define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) \
Evgeniy Stepanov9d1525e2013-05-29 09:09:58 +00002373 ThreadSetName(((TsanInterceptorContext *) ctx)->thr, name)
Dmitry Vyukov5cf2c462013-10-29 10:30:39 +00002374
2375#define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002376 __tsan::ctx->thread_registry->SetThreadNameByUserId(thread, name)
Dmitry Vyukov5cf2c462013-10-29 10:30:39 +00002377
Evgeniy Stepanove18e3f02013-08-12 13:19:53 +00002378#define COMMON_INTERCEPTOR_BLOCK_REAL(name) BLOCK_REAL(name)
Dmitry Vyukov5cf2c462013-10-29 10:30:39 +00002379
Dmitry Vyukov14dd9802013-10-03 15:22:29 +00002380#define COMMON_INTERCEPTOR_ON_EXIT(ctx) \
2381 OnExit(((TsanInterceptorContext *) ctx)->thr)
Dmitry Vyukov5cf2c462013-10-29 10:30:39 +00002382
Alexey Samsonov5e2d3772013-10-16 08:20:31 +00002383#define COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m) \
2384 MutexLock(((TsanInterceptorContext *)ctx)->thr, \
2385 ((TsanInterceptorContext *)ctx)->pc, (uptr)m)
Dmitry Vyukov5cf2c462013-10-29 10:30:39 +00002386
Alexey Samsonov5e2d3772013-10-16 08:20:31 +00002387#define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) \
2388 MutexUnlock(((TsanInterceptorContext *)ctx)->thr, \
2389 ((TsanInterceptorContext *)ctx)->pc, (uptr)m)
Dmitry Vyukov5cf2c462013-10-29 10:30:39 +00002390
Dmitry Vyukov11f53092013-11-15 16:58:12 +00002391#define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) \
2392 MutexRepair(((TsanInterceptorContext *)ctx)->thr, \
2393 ((TsanInterceptorContext *)ctx)->pc, (uptr)m)
2394
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002395#if !SANITIZER_MAC
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002396#define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) \
2397 HandleRecvmsg(((TsanInterceptorContext *)ctx)->thr, \
2398 ((TsanInterceptorContext *)ctx)->pc, msg)
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002399#endif
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002400
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -07002401#define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
2402 if (TsanThread *t = GetCurrentThread()) { \
2403 *begin = t->tls_begin(); \
2404 *end = t->tls_end(); \
2405 } else { \
2406 *begin = *end = 0; \
2407 }
2408
Evgeniy Stepanov4f32c0b2013-01-18 13:01:18 +00002409#include "sanitizer_common/sanitizer_common_interceptors.inc"
Kostya Serebryanyc8033192013-01-17 13:09:00 +00002410
Dmitry Vyukov6c21e112013-09-21 20:59:04 +00002411#define TSAN_SYSCALL() \
2412 ThreadState *thr = cur_thread(); \
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002413 if (thr->ignore_interceptors) \
2414 return; \
Dmitry Vyukov6c21e112013-09-21 20:59:04 +00002415 ScopedSyscall scoped_syscall(thr) \
2416/**/
2417
2418struct ScopedSyscall {
2419 ThreadState *thr;
2420
2421 explicit ScopedSyscall(ThreadState *thr)
2422 : thr(thr) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002423 Initialize(thr);
Dmitry Vyukov6c21e112013-09-21 20:59:04 +00002424 }
2425
2426 ~ScopedSyscall() {
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002427 ProcessPendingSignals(thr);
Dmitry Vyukov6c21e112013-09-21 20:59:04 +00002428 }
2429};
2430
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002431#if !SANITIZER_MAC
Dmitry Vyukoveb0be612013-09-20 06:54:14 +00002432static void syscall_access_range(uptr pc, uptr p, uptr s, bool write) {
Dmitry Vyukov6c21e112013-09-21 20:59:04 +00002433 TSAN_SYSCALL();
Dmitry Vyukoveb0be612013-09-20 06:54:14 +00002434 MemoryAccessRange(thr, pc, p, s, write);
Dmitry Vyukoveb0be612013-09-20 06:54:14 +00002435}
2436
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002437static void syscall_acquire(uptr pc, uptr addr) {
2438 TSAN_SYSCALL();
2439 Acquire(thr, pc, addr);
2440 DPrintf("syscall_acquire(%p)\n", addr);
2441}
2442
2443static void syscall_release(uptr pc, uptr addr) {
2444 TSAN_SYSCALL();
2445 DPrintf("syscall_release(%p)\n", addr);
2446 Release(thr, pc, addr);
2447}
2448
Dmitry Vyukovb47d41f2013-09-21 03:47:35 +00002449static void syscall_fd_close(uptr pc, int fd) {
Dmitry Vyukov6c21e112013-09-21 20:59:04 +00002450 TSAN_SYSCALL();
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002451 FdClose(thr, pc, fd);
2452}
2453
2454static USED void syscall_fd_acquire(uptr pc, int fd) {
2455 TSAN_SYSCALL();
2456 FdAcquire(thr, pc, fd);
2457 DPrintf("syscall_fd_acquire(%p)\n", fd);
2458}
2459
2460static USED void syscall_fd_release(uptr pc, int fd) {
2461 TSAN_SYSCALL();
2462 DPrintf("syscall_fd_release(%p)\n", fd);
2463 FdRelease(thr, pc, fd);
Dmitry Vyukov6c21e112013-09-21 20:59:04 +00002464}
2465
2466static void syscall_pre_fork(uptr pc) {
2467 TSAN_SYSCALL();
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002468 ForkBefore(thr, pc);
Dmitry Vyukov6c21e112013-09-21 20:59:04 +00002469}
2470
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002471static void syscall_post_fork(uptr pc, int pid) {
Dmitry Vyukov6c21e112013-09-21 20:59:04 +00002472 TSAN_SYSCALL();
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002473 if (pid == 0) {
Dmitry Vyukov6c21e112013-09-21 20:59:04 +00002474 // child
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002475 ForkChildAfter(thr, pc);
Dmitry Vyukov6c21e112013-09-21 20:59:04 +00002476 FdOnFork(thr, pc);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002477 } else if (pid > 0) {
Dmitry Vyukov6c21e112013-09-21 20:59:04 +00002478 // parent
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002479 ForkParentAfter(thr, pc);
2480 } else {
2481 // error
2482 ForkParentAfter(thr, pc);
Dmitry Vyukov6c21e112013-09-21 20:59:04 +00002483 }
Dmitry Vyukovb47d41f2013-09-21 03:47:35 +00002484}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002485#endif
Dmitry Vyukovb47d41f2013-09-21 03:47:35 +00002486
Alexey Samsonovc09250f2013-09-19 07:59:18 +00002487#define COMMON_SYSCALL_PRE_READ_RANGE(p, s) \
Dmitry Vyukoveb0be612013-09-20 06:54:14 +00002488 syscall_access_range(GET_CALLER_PC(), (uptr)(p), (uptr)(s), false)
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002489
Alexey Samsonovc09250f2013-09-19 07:59:18 +00002490#define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) \
Dmitry Vyukoveb0be612013-09-20 06:54:14 +00002491 syscall_access_range(GET_CALLER_PC(), (uptr)(p), (uptr)(s), true)
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002492
Alexey Samsonovc09250f2013-09-19 07:59:18 +00002493#define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
Evgeniy Stepanov0b765782013-10-29 18:44:52 +00002494 do { \
2495 (void)(p); \
2496 (void)(s); \
2497 } while (false)
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002498
Alexey Samsonovc09250f2013-09-19 07:59:18 +00002499#define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
Evgeniy Stepanov0b765782013-10-29 18:44:52 +00002500 do { \
2501 (void)(p); \
2502 (void)(s); \
2503 } while (false)
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002504
2505#define COMMON_SYSCALL_ACQUIRE(addr) \
2506 syscall_acquire(GET_CALLER_PC(), (uptr)(addr))
2507
2508#define COMMON_SYSCALL_RELEASE(addr) \
2509 syscall_release(GET_CALLER_PC(), (uptr)(addr))
2510
Evgeniy Stepanov0b765782013-10-29 18:44:52 +00002511#define COMMON_SYSCALL_FD_CLOSE(fd) syscall_fd_close(GET_CALLER_PC(), fd)
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002512
2513#define COMMON_SYSCALL_FD_ACQUIRE(fd) syscall_fd_acquire(GET_CALLER_PC(), fd)
2514
2515#define COMMON_SYSCALL_FD_RELEASE(fd) syscall_fd_release(GET_CALLER_PC(), fd)
2516
Dmitry Vyukov6c21e112013-09-21 20:59:04 +00002517#define COMMON_SYSCALL_PRE_FORK() \
2518 syscall_pre_fork(GET_CALLER_PC())
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002519
Dmitry Vyukov6c21e112013-09-21 20:59:04 +00002520#define COMMON_SYSCALL_POST_FORK(res) \
2521 syscall_post_fork(GET_CALLER_PC(), res)
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002522
Evgeniy Stepanov881b6772013-04-12 14:57:03 +00002523#include "sanitizer_common/sanitizer_common_syscalls.inc"
2524
Dmitry Vyukovee8ee242012-11-15 17:40:49 +00002525namespace __tsan {
2526
Dmitry Vyukovc78140f2013-10-03 14:00:46 +00002527static void finalize(void *arg) {
Dmitry Vyukov14dd9802013-10-03 15:22:29 +00002528 ThreadState *thr = cur_thread();
Dmitry Vyukovc78140f2013-10-03 14:00:46 +00002529 int status = Finalize(thr);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002530 // Make sure the output is not lost.
Stephen Hines86277eb2015-03-23 12:06:32 -07002531 FlushStreams();
Dmitry Vyukovc78140f2013-10-03 14:00:46 +00002532 if (status)
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002533 Die();
Dmitry Vyukovc78140f2013-10-03 14:00:46 +00002534}
2535
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002536#if !SANITIZER_MAC && !SANITIZER_ANDROID
Dmitry Vyukov91ac0082012-12-04 07:28:25 +00002537static void unreachable() {
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002538 Report("FATAL: ThreadSanitizer: unreachable called\n");
Dmitry Vyukov91ac0082012-12-04 07:28:25 +00002539 Die();
2540}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002541#endif
Dmitry Vyukov91ac0082012-12-04 07:28:25 +00002542
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002543void InitializeInterceptors() {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002544#if !SANITIZER_MAC
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002545 // We need to setup it early, because functions like dlsym() can call it.
Dmitry Vyukov065c4ac2012-06-29 15:19:14 +00002546 REAL(memset) = internal_memset;
2547 REAL(memcpy) = internal_memcpy;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002548#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002549
Dmitry Vyukov8e1c7692013-03-19 12:24:19 +00002550 // Instruct libc malloc to consume less memory.
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002551#if SANITIZER_LINUX
Dmitry Vyukov8e1c7692013-03-19 12:24:19 +00002552 mallopt(1, 0); // M_MXFAST
2553 mallopt(-3, 32*1024); // M_MMAP_THRESHOLD
Stephen Hines6d186232014-11-26 17:56:19 -08002554#endif
Dmitry Vyukov8e1c7692013-03-19 12:24:19 +00002555
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002556 InitializeCommonInterceptors();
Kostya Serebryanyc8033192013-01-17 13:09:00 +00002557
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002558#if !SANITIZER_MAC
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002559 // We can not use TSAN_INTERCEPT to get setjmp addr,
2560 // because it does &setjmp and setjmp is not present in some versions of libc.
2561 using __interception::GetRealFunctionAddress;
2562 GetRealFunctionAddress("setjmp", (uptr*)&REAL(setjmp), 0, 0);
2563 GetRealFunctionAddress("_setjmp", (uptr*)&REAL(_setjmp), 0, 0);
2564 GetRealFunctionAddress("sigsetjmp", (uptr*)&REAL(sigsetjmp), 0, 0);
2565 GetRealFunctionAddress("__sigsetjmp", (uptr*)&REAL(__sigsetjmp), 0, 0);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002566#endif
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002567
Dmitry Vyukovf037f562012-05-31 18:03:59 +00002568 TSAN_INTERCEPT(longjmp);
2569 TSAN_INTERCEPT(siglongjmp);
2570
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002571 TSAN_INTERCEPT(malloc);
Dmitry Vyukov07ba8ef2012-11-30 17:27:58 +00002572 TSAN_INTERCEPT(__libc_memalign);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002573 TSAN_INTERCEPT(calloc);
2574 TSAN_INTERCEPT(realloc);
2575 TSAN_INTERCEPT(free);
2576 TSAN_INTERCEPT(cfree);
2577 TSAN_INTERCEPT(mmap);
Stephen Hines6d186232014-11-26 17:56:19 -08002578 TSAN_MAYBE_INTERCEPT_MMAP64;
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002579 TSAN_INTERCEPT(munmap);
Stephen Hines6d186232014-11-26 17:56:19 -08002580 TSAN_MAYBE_INTERCEPT_MEMALIGN;
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002581 TSAN_INTERCEPT(valloc);
Stephen Hines6d186232014-11-26 17:56:19 -08002582 TSAN_MAYBE_INTERCEPT_PVALLOC;
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002583 TSAN_INTERCEPT(posix_memalign);
2584
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002585 TSAN_INTERCEPT(strlen);
2586 TSAN_INTERCEPT(memset);
2587 TSAN_INTERCEPT(memcpy);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002588 TSAN_INTERCEPT(memmove);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002589 TSAN_INTERCEPT(strchr);
2590 TSAN_INTERCEPT(strchrnul);
2591 TSAN_INTERCEPT(strrchr);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002592 TSAN_INTERCEPT(strcpy); // NOLINT
2593 TSAN_INTERCEPT(strncpy);
Dmitry Vyukov39fa68e2013-09-21 23:44:19 +00002594 TSAN_INTERCEPT(strdup);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002595
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002596 TSAN_INTERCEPT(pthread_create);
2597 TSAN_INTERCEPT(pthread_join);
2598 TSAN_INTERCEPT(pthread_detach);
2599
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002600 TSAN_INTERCEPT_VER(pthread_cond_init, PTHREAD_ABI_BASE);
2601 TSAN_INTERCEPT_VER(pthread_cond_signal, PTHREAD_ABI_BASE);
2602 TSAN_INTERCEPT_VER(pthread_cond_broadcast, PTHREAD_ABI_BASE);
2603 TSAN_INTERCEPT_VER(pthread_cond_wait, PTHREAD_ABI_BASE);
2604 TSAN_INTERCEPT_VER(pthread_cond_timedwait, PTHREAD_ABI_BASE);
2605 TSAN_INTERCEPT_VER(pthread_cond_destroy, PTHREAD_ABI_BASE);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002606
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002607 TSAN_INTERCEPT(pthread_mutex_init);
2608 TSAN_INTERCEPT(pthread_mutex_destroy);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002609 TSAN_INTERCEPT(pthread_mutex_trylock);
2610 TSAN_INTERCEPT(pthread_mutex_timedlock);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002611
2612 TSAN_INTERCEPT(pthread_spin_init);
2613 TSAN_INTERCEPT(pthread_spin_destroy);
2614 TSAN_INTERCEPT(pthread_spin_lock);
2615 TSAN_INTERCEPT(pthread_spin_trylock);
2616 TSAN_INTERCEPT(pthread_spin_unlock);
2617
2618 TSAN_INTERCEPT(pthread_rwlock_init);
2619 TSAN_INTERCEPT(pthread_rwlock_destroy);
2620 TSAN_INTERCEPT(pthread_rwlock_rdlock);
2621 TSAN_INTERCEPT(pthread_rwlock_tryrdlock);
2622 TSAN_INTERCEPT(pthread_rwlock_timedrdlock);
2623 TSAN_INTERCEPT(pthread_rwlock_wrlock);
2624 TSAN_INTERCEPT(pthread_rwlock_trywrlock);
2625 TSAN_INTERCEPT(pthread_rwlock_timedwrlock);
2626 TSAN_INTERCEPT(pthread_rwlock_unlock);
2627
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002628 TSAN_INTERCEPT(pthread_barrier_init);
2629 TSAN_INTERCEPT(pthread_barrier_destroy);
2630 TSAN_INTERCEPT(pthread_barrier_wait);
2631
2632 TSAN_INTERCEPT(pthread_once);
2633
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00002634 TSAN_INTERCEPT(stat);
Stephen Hines6d186232014-11-26 17:56:19 -08002635 TSAN_MAYBE_INTERCEPT___XSTAT;
2636 TSAN_MAYBE_INTERCEPT_STAT64;
2637 TSAN_MAYBE_INTERCEPT___XSTAT64;
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00002638 TSAN_INTERCEPT(lstat);
Stephen Hines6d186232014-11-26 17:56:19 -08002639 TSAN_MAYBE_INTERCEPT___LXSTAT;
2640 TSAN_MAYBE_INTERCEPT_LSTAT64;
2641 TSAN_MAYBE_INTERCEPT___LXSTAT64;
Dmitry Vyukov61ba1b52013-01-29 09:23:09 +00002642 TSAN_INTERCEPT(fstat);
Stephen Hines6d186232014-11-26 17:56:19 -08002643 TSAN_MAYBE_INTERCEPT___FXSTAT;
2644 TSAN_MAYBE_INTERCEPT_FSTAT64;
2645 TSAN_MAYBE_INTERCEPT___FXSTAT64;
Dmitry Vyukovba3ae352012-12-07 18:30:40 +00002646 TSAN_INTERCEPT(open);
Stephen Hines6d186232014-11-26 17:56:19 -08002647 TSAN_MAYBE_INTERCEPT_OPEN64;
Dmitry Vyukovba3ae352012-12-07 18:30:40 +00002648 TSAN_INTERCEPT(creat);
Stephen Hines6d186232014-11-26 17:56:19 -08002649 TSAN_MAYBE_INTERCEPT_CREAT64;
Dmitry Vyukovba3ae352012-12-07 18:30:40 +00002650 TSAN_INTERCEPT(dup);
2651 TSAN_INTERCEPT(dup2);
2652 TSAN_INTERCEPT(dup3);
Stephen Hines6d186232014-11-26 17:56:19 -08002653 TSAN_MAYBE_INTERCEPT_EVENTFD;
2654 TSAN_MAYBE_INTERCEPT_SIGNALFD;
2655 TSAN_MAYBE_INTERCEPT_INOTIFY_INIT;
2656 TSAN_MAYBE_INTERCEPT_INOTIFY_INIT1;
Dmitry Vyukov68230a12012-12-07 19:23:59 +00002657 TSAN_INTERCEPT(socket);
Dmitry Vyukov983518e2012-12-14 09:57:42 +00002658 TSAN_INTERCEPT(socketpair);
Dmitry Vyukov68230a12012-12-07 19:23:59 +00002659 TSAN_INTERCEPT(connect);
Dmitry Vyukov52c70e52013-02-04 08:06:32 +00002660 TSAN_INTERCEPT(bind);
2661 TSAN_INTERCEPT(listen);
Stephen Hines6d186232014-11-26 17:56:19 -08002662 TSAN_MAYBE_INTERCEPT_EPOLL_CREATE;
2663 TSAN_MAYBE_INTERCEPT_EPOLL_CREATE1;
Dmitry Vyukovddeb2c32012-12-07 15:32:56 +00002664 TSAN_INTERCEPT(close);
Stephen Hines6d186232014-11-26 17:56:19 -08002665 TSAN_MAYBE_INTERCEPT___CLOSE;
2666 TSAN_MAYBE_INTERCEPT___RES_ICLOSE;
Dmitry Vyukovddeb2c32012-12-07 15:32:56 +00002667 TSAN_INTERCEPT(pipe);
2668 TSAN_INTERCEPT(pipe2);
2669
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002670 TSAN_INTERCEPT(send);
2671 TSAN_INTERCEPT(sendmsg);
2672 TSAN_INTERCEPT(recv);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002673
2674 TSAN_INTERCEPT(unlink);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002675 TSAN_INTERCEPT(tmpfile);
Stephen Hines6d186232014-11-26 17:56:19 -08002676 TSAN_MAYBE_INTERCEPT_TMPFILE64;
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002677 TSAN_INTERCEPT(fread);
2678 TSAN_INTERCEPT(fwrite);
Dmitry Vyukov5043f052013-03-21 12:50:43 +00002679 TSAN_INTERCEPT(abort);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002680 TSAN_INTERCEPT(puts);
2681 TSAN_INTERCEPT(rmdir);
Stephen Hines86277eb2015-03-23 12:06:32 -07002682 TSAN_INTERCEPT(closedir);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002683
Stephen Hines6d186232014-11-26 17:56:19 -08002684 TSAN_MAYBE_INTERCEPT_EPOLL_CTL;
2685 TSAN_MAYBE_INTERCEPT_EPOLL_WAIT;
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002686
2687 TSAN_INTERCEPT(sigaction);
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002688 TSAN_INTERCEPT(signal);
Dmitry Vyukovfbeab512013-07-16 11:28:04 +00002689 TSAN_INTERCEPT(sigsuspend);
Dmitry Vyukovd91d0672012-05-21 08:26:51 +00002690 TSAN_INTERCEPT(raise);
2691 TSAN_INTERCEPT(kill);
2692 TSAN_INTERCEPT(pthread_kill);
Dmitry Vyukov84853112012-08-31 17:27:49 +00002693 TSAN_INTERCEPT(sleep);
2694 TSAN_INTERCEPT(usleep);
2695 TSAN_INTERCEPT(nanosleep);
Dmitry Vyukovaad173b2012-11-09 19:55:06 +00002696 TSAN_INTERCEPT(gettimeofday);
Dmitry Vyukovcf878fd2013-08-13 16:51:27 +00002697 TSAN_INTERCEPT(getaddrinfo);
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002698
Dmitry Vyukov4554b7a2012-12-18 14:44:44 +00002699 TSAN_INTERCEPT(fork);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002700 TSAN_INTERCEPT(vfork);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002701#if !SANITIZER_ANDROID
2702 TSAN_INTERCEPT(dl_iterate_phdr);
2703#endif
Dmitry Vyukov31c05ea2013-01-29 13:05:30 +00002704 TSAN_INTERCEPT(on_exit);
2705 TSAN_INTERCEPT(__cxa_atexit);
Dmitry Vyukovc78140f2013-10-03 14:00:46 +00002706 TSAN_INTERCEPT(_exit);
Dmitry Vyukov4554b7a2012-12-18 14:44:44 +00002707
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002708#if !SANITIZER_MAC && !SANITIZER_ANDROID
Dmitry Vyukov91ac0082012-12-04 07:28:25 +00002709 // Need to setup it, because interceptors check that the function is resolved.
2710 // But atexit is emitted directly into the module, so can't be resolved.
2711 REAL(atexit) = (int(*)(void(*)()))unreachable;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002712#endif
2713
Dmitry Vyukov31c05ea2013-01-29 13:05:30 +00002714 if (REAL(__cxa_atexit)(&finalize, 0, 0)) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +00002715 Printf("ThreadSanitizer: failed to setup atexit callback\n");
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002716 Die();
2717 }
2718
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002719#if !SANITIZER_MAC
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002720 if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +00002721 Printf("ThreadSanitizer: failed to create thread key\n");
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002722 Die();
2723 }
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002724#endif
Dmitry Vyukov3f4e6d92012-12-07 14:40:10 +00002725
2726 FdInit();
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002727}
2728
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002729} // namespace __tsan
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002730
2731// Invisible barrier for tests.
2732// There were several unsuccessful iterations for this functionality:
2733// 1. Initially it was implemented in user code using
2734// REAL(pthread_barrier_wait). But pthread_barrier_wait is not supported on
2735// MacOS. Futexes are linux-specific for this matter.
2736// 2. Then we switched to atomics+usleep(10). But usleep produced parasitic
2737// "as-if synchronized via sleep" messages in reports which failed some
2738// output tests.
2739// 3. Then we switched to atomics+sched_yield. But this produced tons of tsan-
2740// visible events, which lead to "failed to restore stack trace" failures.
2741// Note that no_sanitize_thread attribute does not turn off atomic interception
2742// so attaching it to the function defined in user code does not help.
2743// That's why we now have what we have.
2744extern "C" SANITIZER_INTERFACE_ATTRIBUTE
2745void __tsan_testonly_barrier_init(u64 *barrier, u32 count) {
2746 if (count >= (1 << 8)) {
2747 Printf("barrier_init: count is too large (%d)\n", count);
2748 Die();
2749 }
2750 // 8 lsb is thread count, the remaining are count of entered threads.
2751 *barrier = count;
2752}
2753
2754extern "C" SANITIZER_INTERFACE_ATTRIBUTE
2755void __tsan_testonly_barrier_wait(u64 *barrier) {
2756 unsigned old = __atomic_fetch_add(barrier, 1 << 8, __ATOMIC_RELAXED);
2757 unsigned old_epoch = (old >> 8) / (old & 0xff);
2758 for (;;) {
2759 unsigned cur = __atomic_load_n(barrier, __ATOMIC_RELAXED);
2760 unsigned cur_epoch = (cur >> 8) / (cur & 0xff);
2761 if (cur_epoch != old_epoch)
2762 return;
2763 internal_sched_yield();
2764 }
2765}