Revert r251916 ("[tsan] Port TSan interceptors on OS X").

llvm-svn: 251922
This commit is contained in:
Kuba Brecka 2015-11-03 14:42:03 +00:00
parent 9b89608698
commit 9d71caa9a4
1 changed files with 103 additions and 151 deletions

View File

@ -28,26 +28,14 @@
#include "tsan_mman.h" #include "tsan_mman.h"
#include "tsan_fd.h" #include "tsan_fd.h"
#if SANITIZER_POSIX
#include "sanitizer_common/sanitizer_posix.h"
#endif
using namespace __tsan; // NOLINT using namespace __tsan; // NOLINT
#if SANITIZER_FREEBSD || SANITIZER_MAC
#define __errno_location __error
#define stdout __stdoutp
#define stderr __stderrp
#endif
#if SANITIZER_FREEBSD #if SANITIZER_FREEBSD
#define __errno_location __error
#define __libc_realloc __realloc #define __libc_realloc __realloc
#define __libc_calloc __calloc #define __libc_calloc __calloc
#elif SANITIZER_MAC #define stdout __stdoutp
#define __libc_malloc REAL(malloc) #define stderr __stderrp
#define __libc_realloc REAL(realloc)
#define __libc_calloc REAL(calloc)
#define __libc_free REAL(free)
#endif #endif
#if SANITIZER_LINUX || SANITIZER_FREEBSD #if SANITIZER_LINUX || SANITIZER_FREEBSD
@ -92,6 +80,7 @@ extern "C" int pthread_attr_setstacksize(void *attr, uptr stacksize);
extern "C" int pthread_key_create(unsigned *key, void (*destructor)(void* v)); extern "C" int pthread_key_create(unsigned *key, void (*destructor)(void* v));
extern "C" int pthread_setspecific(unsigned key, const void *v); extern "C" int pthread_setspecific(unsigned key, const void *v);
DECLARE_REAL(int, pthread_mutexattr_gettype, void *, void *) DECLARE_REAL(int, pthread_mutexattr_gettype, void *, void *)
extern "C" int pthread_yield();
extern "C" int pthread_sigmask(int how, const __sanitizer_sigset_t *set, extern "C" int pthread_sigmask(int how, const __sanitizer_sigset_t *set,
__sanitizer_sigset_t *oldset); __sanitizer_sigset_t *oldset);
// REAL(sigfillset) defined in common interceptors. // REAL(sigfillset) defined in common interceptors.
@ -334,79 +323,6 @@ TSAN_INTERCEPTOR(int, nanosleep, void *req, void *rem) {
return res; return res;
} }
TSAN_INTERCEPTOR(void*, malloc, uptr size) {
if (cur_thread()->in_symbolizer)
return __libc_malloc(size);
void *p = 0;
{
SCOPED_INTERCEPTOR_RAW(malloc, size);
p = user_alloc(thr, pc, size);
}
invoke_malloc_hook(p, size);
return p;
}
#if !SANITIZER_MAC
TSAN_INTERCEPTOR(void*, __libc_memalign, uptr align, uptr sz) {
SCOPED_TSAN_INTERCEPTOR(__libc_memalign, align, sz);
return user_alloc(thr, pc, sz, align);
}
#endif
TSAN_INTERCEPTOR(void*, calloc, uptr size, uptr n) {
if (cur_thread()->in_symbolizer)
return __libc_calloc(size, n);
void *p = 0;
{
SCOPED_INTERCEPTOR_RAW(calloc, size, n);
p = user_calloc(thr, pc, size, n);
}
invoke_malloc_hook(p, n * size);
return p;
}
TSAN_INTERCEPTOR(void*, realloc, void *p, uptr size) {
if (cur_thread()->in_symbolizer)
return __libc_realloc(p, size);
if (p)
invoke_free_hook(p);
{
SCOPED_INTERCEPTOR_RAW(realloc, p, size);
p = user_realloc(thr, pc, p, size);
}
invoke_malloc_hook(p, size);
return p;
}
TSAN_INTERCEPTOR(void, free, void *p) {
if (p == 0)
return;
if (cur_thread()->in_symbolizer)
return __libc_free(p);
invoke_free_hook(p);
SCOPED_INTERCEPTOR_RAW(free, p);
user_free(thr, pc, p);
}
#if !SANITIZER_MAC
TSAN_INTERCEPTOR(void, cfree, void *p) {
if (p == 0)
return;
if (cur_thread()->in_symbolizer)
return __libc_free(p);
invoke_free_hook(p);
SCOPED_INTERCEPTOR_RAW(cfree, p);
user_free(thr, pc, p);
}
#endif
#if !SANITIZER_MAC
TSAN_INTERCEPTOR(uptr, malloc_usable_size, void *p) {
SCOPED_INTERCEPTOR_RAW(malloc_usable_size, p);
return user_alloc_usable_size(p);
}
#endif
// The sole reason tsan wraps atexit callbacks is to establish synchronization // The sole reason tsan wraps atexit callbacks is to establish synchronization
// between callback setup and callback execution. // between callback setup and callback execution.
struct AtExitCtx { struct AtExitCtx {
@ -465,7 +381,6 @@ static void on_exit_wrapper(int status, void *arg) {
__libc_free(ctx); __libc_free(ctx);
} }
#if !SANITIZER_MAC
TSAN_INTERCEPTOR(int, on_exit, void(*f)(int, void*), void *arg) { TSAN_INTERCEPTOR(int, on_exit, void(*f)(int, void*), void *arg) {
if (cur_thread()->in_symbolizer) if (cur_thread()->in_symbolizer)
return 0; return 0;
@ -481,7 +396,6 @@ TSAN_INTERCEPTOR(int, on_exit, void(*f)(int, void*), void *arg) {
ThreadIgnoreEnd(thr, pc); ThreadIgnoreEnd(thr, pc);
return res; return res;
} }
#endif
// Cleanup old bufs. // Cleanup old bufs.
static void JmpBufGarbageCollect(ThreadState *thr, uptr sp) { static void JmpBufGarbageCollect(ThreadState *thr, uptr sp) {
@ -605,6 +519,73 @@ TSAN_INTERCEPTOR(void, siglongjmp, uptr *env, int val) {
REAL(siglongjmp)(env, val); REAL(siglongjmp)(env, val);
} }
TSAN_INTERCEPTOR(void*, malloc, uptr size) {
if (cur_thread()->in_symbolizer)
return __libc_malloc(size);
void *p = 0;
{
SCOPED_INTERCEPTOR_RAW(malloc, size);
p = user_alloc(thr, pc, size);
}
invoke_malloc_hook(p, size);
return p;
}
TSAN_INTERCEPTOR(void*, __libc_memalign, uptr align, uptr sz) {
SCOPED_TSAN_INTERCEPTOR(__libc_memalign, align, sz);
return user_alloc(thr, pc, sz, align);
}
TSAN_INTERCEPTOR(void*, calloc, uptr size, uptr n) {
if (cur_thread()->in_symbolizer)
return __libc_calloc(size, n);
void *p = 0;
{
SCOPED_INTERCEPTOR_RAW(calloc, size, n);
p = user_calloc(thr, pc, size, n);
}
invoke_malloc_hook(p, n * size);
return p;
}
TSAN_INTERCEPTOR(void*, realloc, void *p, uptr size) {
if (cur_thread()->in_symbolizer)
return __libc_realloc(p, size);
if (p)
invoke_free_hook(p);
{
SCOPED_INTERCEPTOR_RAW(realloc, p, size);
p = user_realloc(thr, pc, p, size);
}
invoke_malloc_hook(p, size);
return p;
}
TSAN_INTERCEPTOR(void, free, void *p) {
if (p == 0)
return;
if (cur_thread()->in_symbolizer)
return __libc_free(p);
invoke_free_hook(p);
SCOPED_INTERCEPTOR_RAW(free, p);
user_free(thr, pc, p);
}
TSAN_INTERCEPTOR(void, cfree, void *p) {
if (p == 0)
return;
if (cur_thread()->in_symbolizer)
return __libc_free(p);
invoke_free_hook(p);
SCOPED_INTERCEPTOR_RAW(cfree, p);
user_free(thr, pc, p);
}
TSAN_INTERCEPTOR(uptr, malloc_usable_size, void *p) {
SCOPED_INTERCEPTOR_RAW(malloc_usable_size, p);
return user_alloc_usable_size(p);
}
TSAN_INTERCEPTOR(uptr, strlen, const char *s) { TSAN_INTERCEPTOR(uptr, strlen, const char *s) {
SCOPED_TSAN_INTERCEPTOR(strlen, s); SCOPED_TSAN_INTERCEPTOR(strlen, s);
uptr len = internal_strlen(s); uptr len = internal_strlen(s);
@ -647,7 +628,6 @@ TSAN_INTERCEPTOR(char*, strchr, char *s, int c) {
return res; return res;
} }
#if !SANITIZER_MAC
TSAN_INTERCEPTOR(char*, strchrnul, char *s, int c) { TSAN_INTERCEPTOR(char*, strchrnul, char *s, int c) {
SCOPED_TSAN_INTERCEPTOR(strchrnul, s, c); SCOPED_TSAN_INTERCEPTOR(strchrnul, s, c);
char *res = REAL(strchrnul)(s, c); char *res = REAL(strchrnul)(s, c);
@ -655,7 +635,6 @@ TSAN_INTERCEPTOR(char*, strchrnul, char *s, int c) {
READ_STRING(thr, pc, s, len); READ_STRING(thr, pc, s, len);
return res; return res;
} }
#endif
TSAN_INTERCEPTOR(char*, strrchr, char *s, int c) { TSAN_INTERCEPTOR(char*, strrchr, char *s, int c) {
SCOPED_TSAN_INTERCEPTOR(strrchr, s, c); SCOPED_TSAN_INTERCEPTOR(strrchr, s, c);
@ -713,7 +692,7 @@ TSAN_INTERCEPTOR(void*, mmap, void *addr, long_t sz, int prot,
return res; return res;
} }
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(void*, mmap64, void *addr, long_t sz, int prot, TSAN_INTERCEPTOR(void*, mmap64, void *addr, long_t sz, int prot,
int flags, int fd, u64 off) { int flags, int fd, u64 off) {
SCOPED_TSAN_INTERCEPTOR(mmap64, addr, sz, prot, flags, fd, off); SCOPED_TSAN_INTERCEPTOR(mmap64, addr, sz, prot, flags, fd, off);
@ -743,7 +722,7 @@ TSAN_INTERCEPTOR(int, munmap, void *addr, long_t sz) {
return res; return res;
} }
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(void*, memalign, uptr align, uptr sz) { TSAN_INTERCEPTOR(void*, memalign, uptr align, uptr sz) {
SCOPED_INTERCEPTOR_RAW(memalign, align, sz); SCOPED_INTERCEPTOR_RAW(memalign, align, sz);
return user_alloc(thr, pc, sz, align); return user_alloc(thr, pc, sz, align);
@ -753,19 +732,17 @@ TSAN_INTERCEPTOR(void*, memalign, uptr align, uptr sz) {
#define TSAN_MAYBE_INTERCEPT_MEMALIGN #define TSAN_MAYBE_INTERCEPT_MEMALIGN
#endif #endif
#if !SANITIZER_MAC
TSAN_INTERCEPTOR(void*, aligned_alloc, uptr align, uptr sz) { TSAN_INTERCEPTOR(void*, aligned_alloc, uptr align, uptr sz) {
SCOPED_INTERCEPTOR_RAW(memalign, align, sz); SCOPED_INTERCEPTOR_RAW(memalign, align, sz);
return user_alloc(thr, pc, sz, align); return user_alloc(thr, pc, sz, align);
} }
#endif
TSAN_INTERCEPTOR(void*, valloc, uptr sz) { TSAN_INTERCEPTOR(void*, valloc, uptr sz) {
SCOPED_INTERCEPTOR_RAW(valloc, sz); SCOPED_INTERCEPTOR_RAW(valloc, sz);
return user_alloc(thr, pc, sz, GetPageSizeCached()); return user_alloc(thr, pc, sz, GetPageSizeCached());
} }
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(void*, pvalloc, uptr sz) { TSAN_INTERCEPTOR(void*, pvalloc, uptr sz) {
SCOPED_INTERCEPTOR_RAW(pvalloc, sz); SCOPED_INTERCEPTOR_RAW(pvalloc, sz);
sz = RoundUp(sz, GetPageSizeCached()); sz = RoundUp(sz, GetPageSizeCached());
@ -854,7 +831,7 @@ extern "C" void *__tsan_thread_start_func(void *arg) {
} }
ThreadIgnoreEnd(thr, 0); ThreadIgnoreEnd(thr, 0);
while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0) while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0)
internal_sched_yield(); pthread_yield();
ThreadStart(thr, tid, GetTid()); ThreadStart(thr, tid, GetTid());
atomic_store(&p->tid, 0, memory_order_release); atomic_store(&p->tid, 0, memory_order_release);
} }
@ -914,7 +891,7 @@ TSAN_INTERCEPTOR(int, pthread_create,
// before the new thread got a chance to acquire from it in ThreadStart. // before the new thread got a chance to acquire from it in ThreadStart.
atomic_store(&p.tid, tid, memory_order_release); atomic_store(&p.tid, tid, memory_order_release);
while (atomic_load(&p.tid, memory_order_acquire) != 0) while (atomic_load(&p.tid, memory_order_acquire) != 0)
internal_sched_yield(); pthread_yield();
} }
if (attr == &myattr) if (attr == &myattr)
pthread_attr_destroy(&myattr); pthread_attr_destroy(&myattr);
@ -1117,7 +1094,6 @@ TSAN_INTERCEPTOR(int, pthread_mutex_trylock, void *m) {
return res; return res;
} }
#if !SANITIZER_MAC
TSAN_INTERCEPTOR(int, pthread_mutex_timedlock, void *m, void *abstime) { TSAN_INTERCEPTOR(int, pthread_mutex_timedlock, void *m, void *abstime) {
SCOPED_TSAN_INTERCEPTOR(pthread_mutex_timedlock, m, abstime); SCOPED_TSAN_INTERCEPTOR(pthread_mutex_timedlock, m, abstime);
int res = REAL(pthread_mutex_timedlock)(m, abstime); int res = REAL(pthread_mutex_timedlock)(m, abstime);
@ -1126,9 +1102,7 @@ TSAN_INTERCEPTOR(int, pthread_mutex_timedlock, void *m, void *abstime) {
} }
return res; return res;
} }
#endif
#if !SANITIZER_MAC
TSAN_INTERCEPTOR(int, pthread_spin_init, void *m, int pshared) { TSAN_INTERCEPTOR(int, pthread_spin_init, void *m, int pshared) {
SCOPED_TSAN_INTERCEPTOR(pthread_spin_init, m, pshared); SCOPED_TSAN_INTERCEPTOR(pthread_spin_init, m, pshared);
int res = REAL(pthread_spin_init)(m, pshared); int res = REAL(pthread_spin_init)(m, pshared);
@ -1171,7 +1145,6 @@ TSAN_INTERCEPTOR(int, pthread_spin_unlock, void *m) {
int res = REAL(pthread_spin_unlock)(m); int res = REAL(pthread_spin_unlock)(m);
return res; return res;
} }
#endif
TSAN_INTERCEPTOR(int, pthread_rwlock_init, void *m, void *a) { TSAN_INTERCEPTOR(int, pthread_rwlock_init, void *m, void *a) {
SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_init, m, a); SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_init, m, a);
@ -1209,7 +1182,6 @@ TSAN_INTERCEPTOR(int, pthread_rwlock_tryrdlock, void *m) {
return res; return res;
} }
#if !SANITIZER_MAC
TSAN_INTERCEPTOR(int, pthread_rwlock_timedrdlock, void *m, void *abstime) { TSAN_INTERCEPTOR(int, pthread_rwlock_timedrdlock, void *m, void *abstime) {
SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_timedrdlock, m, abstime); SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_timedrdlock, m, abstime);
int res = REAL(pthread_rwlock_timedrdlock)(m, abstime); int res = REAL(pthread_rwlock_timedrdlock)(m, abstime);
@ -1218,7 +1190,6 @@ TSAN_INTERCEPTOR(int, pthread_rwlock_timedrdlock, void *m, void *abstime) {
} }
return res; return res;
} }
#endif
TSAN_INTERCEPTOR(int, pthread_rwlock_wrlock, void *m) { TSAN_INTERCEPTOR(int, pthread_rwlock_wrlock, void *m) {
SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_wrlock, m); SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_wrlock, m);
@ -1238,7 +1209,6 @@ TSAN_INTERCEPTOR(int, pthread_rwlock_trywrlock, void *m) {
return res; return res;
} }
#if !SANITIZER_MAC
TSAN_INTERCEPTOR(int, pthread_rwlock_timedwrlock, void *m, void *abstime) { TSAN_INTERCEPTOR(int, pthread_rwlock_timedwrlock, void *m, void *abstime) {
SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_timedwrlock, m, abstime); SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_timedwrlock, m, abstime);
int res = REAL(pthread_rwlock_timedwrlock)(m, abstime); int res = REAL(pthread_rwlock_timedwrlock)(m, abstime);
@ -1247,7 +1217,6 @@ TSAN_INTERCEPTOR(int, pthread_rwlock_timedwrlock, void *m, void *abstime) {
} }
return res; return res;
} }
#endif
TSAN_INTERCEPTOR(int, pthread_rwlock_unlock, void *m) { TSAN_INTERCEPTOR(int, pthread_rwlock_unlock, void *m) {
SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_unlock, m); SCOPED_TSAN_INTERCEPTOR(pthread_rwlock_unlock, m);
@ -1256,7 +1225,6 @@ TSAN_INTERCEPTOR(int, pthread_rwlock_unlock, void *m) {
return res; return res;
} }
#if !SANITIZER_MAC
TSAN_INTERCEPTOR(int, pthread_barrier_init, void *b, void *a, unsigned count) { TSAN_INTERCEPTOR(int, pthread_barrier_init, void *b, void *a, unsigned count) {
SCOPED_TSAN_INTERCEPTOR(pthread_barrier_init, b, a, count); SCOPED_TSAN_INTERCEPTOR(pthread_barrier_init, b, a, count);
MemoryWrite(thr, pc, (uptr)b, kSizeLog1); MemoryWrite(thr, pc, (uptr)b, kSizeLog1);
@ -1282,7 +1250,6 @@ TSAN_INTERCEPTOR(int, pthread_barrier_wait, void *b) {
} }
return res; return res;
} }
#endif
TSAN_INTERCEPTOR(int, pthread_once, void *o, void (*f)()) { TSAN_INTERCEPTOR(int, pthread_once, void *o, void (*f)()) {
SCOPED_INTERCEPTOR_RAW(pthread_once, o, f); SCOPED_INTERCEPTOR_RAW(pthread_once, o, f);
@ -1298,7 +1265,7 @@ TSAN_INTERCEPTOR(int, pthread_once, void *o, void (*f)()) {
atomic_store(a, 2, memory_order_release); atomic_store(a, 2, memory_order_release);
} else { } else {
while (v != 2) { while (v != 2) {
internal_sched_yield(); pthread_yield();
v = atomic_load(a, memory_order_acquire); v = atomic_load(a, memory_order_acquire);
} }
if (!thr->in_ignored_lib) if (!thr->in_ignored_lib)
@ -1307,7 +1274,7 @@ TSAN_INTERCEPTOR(int, pthread_once, void *o, void (*f)()) {
return 0; return 0;
} }
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) { TSAN_INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
SCOPED_TSAN_INTERCEPTOR(__xstat, version, path, buf); SCOPED_TSAN_INTERCEPTOR(__xstat, version, path, buf);
READ_STRING(thr, pc, path, 0); READ_STRING(thr, pc, path, 0);
@ -1319,7 +1286,7 @@ TSAN_INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
#endif #endif
TSAN_INTERCEPTOR(int, stat, const char *path, void *buf) { TSAN_INTERCEPTOR(int, stat, const char *path, void *buf) {
#if SANITIZER_FREEBSD || SANITIZER_MAC #if SANITIZER_FREEBSD
SCOPED_TSAN_INTERCEPTOR(stat, path, buf); SCOPED_TSAN_INTERCEPTOR(stat, path, buf);
READ_STRING(thr, pc, path, 0); READ_STRING(thr, pc, path, 0);
return REAL(stat)(path, buf); return REAL(stat)(path, buf);
@ -1330,7 +1297,7 @@ TSAN_INTERCEPTOR(int, stat, const char *path, void *buf) {
#endif #endif
} }
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) { TSAN_INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
SCOPED_TSAN_INTERCEPTOR(__xstat64, version, path, buf); SCOPED_TSAN_INTERCEPTOR(__xstat64, version, path, buf);
READ_STRING(thr, pc, path, 0); READ_STRING(thr, pc, path, 0);
@ -1341,7 +1308,7 @@ TSAN_INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
#define TSAN_MAYBE_INTERCEPT___XSTAT64 #define TSAN_MAYBE_INTERCEPT___XSTAT64
#endif #endif
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, stat64, const char *path, void *buf) { TSAN_INTERCEPTOR(int, stat64, const char *path, void *buf) {
SCOPED_TSAN_INTERCEPTOR(__xstat64, 0, path, buf); SCOPED_TSAN_INTERCEPTOR(__xstat64, 0, path, buf);
READ_STRING(thr, pc, path, 0); READ_STRING(thr, pc, path, 0);
@ -1352,7 +1319,7 @@ TSAN_INTERCEPTOR(int, stat64, const char *path, void *buf) {
#define TSAN_MAYBE_INTERCEPT_STAT64 #define TSAN_MAYBE_INTERCEPT_STAT64
#endif #endif
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) { TSAN_INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
SCOPED_TSAN_INTERCEPTOR(__lxstat, version, path, buf); SCOPED_TSAN_INTERCEPTOR(__lxstat, version, path, buf);
READ_STRING(thr, pc, path, 0); READ_STRING(thr, pc, path, 0);
@ -1364,7 +1331,7 @@ TSAN_INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
#endif #endif
TSAN_INTERCEPTOR(int, lstat, const char *path, void *buf) { TSAN_INTERCEPTOR(int, lstat, const char *path, void *buf) {
#if SANITIZER_FREEBSD || SANITIZER_MAC #if SANITIZER_FREEBSD
SCOPED_TSAN_INTERCEPTOR(lstat, path, buf); SCOPED_TSAN_INTERCEPTOR(lstat, path, buf);
READ_STRING(thr, pc, path, 0); READ_STRING(thr, pc, path, 0);
return REAL(lstat)(path, buf); return REAL(lstat)(path, buf);
@ -1375,7 +1342,7 @@ TSAN_INTERCEPTOR(int, lstat, const char *path, void *buf) {
#endif #endif
} }
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) { TSAN_INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
SCOPED_TSAN_INTERCEPTOR(__lxstat64, version, path, buf); SCOPED_TSAN_INTERCEPTOR(__lxstat64, version, path, buf);
READ_STRING(thr, pc, path, 0); READ_STRING(thr, pc, path, 0);
@ -1386,7 +1353,7 @@ TSAN_INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
#define TSAN_MAYBE_INTERCEPT___LXSTAT64 #define TSAN_MAYBE_INTERCEPT___LXSTAT64
#endif #endif
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, lstat64, const char *path, void *buf) { TSAN_INTERCEPTOR(int, lstat64, const char *path, void *buf) {
SCOPED_TSAN_INTERCEPTOR(__lxstat64, 0, path, buf); SCOPED_TSAN_INTERCEPTOR(__lxstat64, 0, path, buf);
READ_STRING(thr, pc, path, 0); READ_STRING(thr, pc, path, 0);
@ -1397,7 +1364,7 @@ TSAN_INTERCEPTOR(int, lstat64, const char *path, void *buf) {
#define TSAN_MAYBE_INTERCEPT_LSTAT64 #define TSAN_MAYBE_INTERCEPT_LSTAT64
#endif #endif
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, __fxstat, int version, int fd, void *buf) { TSAN_INTERCEPTOR(int, __fxstat, int version, int fd, void *buf) {
SCOPED_TSAN_INTERCEPTOR(__fxstat, version, fd, buf); SCOPED_TSAN_INTERCEPTOR(__fxstat, version, fd, buf);
if (fd > 0) if (fd > 0)
@ -1410,7 +1377,7 @@ TSAN_INTERCEPTOR(int, __fxstat, int version, int fd, void *buf) {
#endif #endif
TSAN_INTERCEPTOR(int, fstat, int fd, void *buf) { TSAN_INTERCEPTOR(int, fstat, int fd, void *buf) {
#if SANITIZER_FREEBSD || SANITIZER_MAC #if SANITIZER_FREEBSD
SCOPED_TSAN_INTERCEPTOR(fstat, fd, buf); SCOPED_TSAN_INTERCEPTOR(fstat, fd, buf);
if (fd > 0) if (fd > 0)
FdAccess(thr, pc, fd); FdAccess(thr, pc, fd);
@ -1423,7 +1390,7 @@ TSAN_INTERCEPTOR(int, fstat, int fd, void *buf) {
#endif #endif
} }
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, __fxstat64, int version, int fd, void *buf) { TSAN_INTERCEPTOR(int, __fxstat64, int version, int fd, void *buf) {
SCOPED_TSAN_INTERCEPTOR(__fxstat64, version, fd, buf); SCOPED_TSAN_INTERCEPTOR(__fxstat64, version, fd, buf);
if (fd > 0) if (fd > 0)
@ -1435,7 +1402,7 @@ TSAN_INTERCEPTOR(int, __fxstat64, int version, int fd, void *buf) {
#define TSAN_MAYBE_INTERCEPT___FXSTAT64 #define TSAN_MAYBE_INTERCEPT___FXSTAT64
#endif #endif
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, fstat64, int fd, void *buf) { TSAN_INTERCEPTOR(int, fstat64, int fd, void *buf) {
SCOPED_TSAN_INTERCEPTOR(__fxstat64, 0, fd, buf); SCOPED_TSAN_INTERCEPTOR(__fxstat64, 0, fd, buf);
if (fd > 0) if (fd > 0)
@ -1456,7 +1423,7 @@ TSAN_INTERCEPTOR(int, open, const char *name, int flags, int mode) {
return fd; return fd;
} }
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, open64, const char *name, int flags, int mode) { TSAN_INTERCEPTOR(int, open64, const char *name, int flags, int mode) {
SCOPED_TSAN_INTERCEPTOR(open64, name, flags, mode); SCOPED_TSAN_INTERCEPTOR(open64, name, flags, mode);
READ_STRING(thr, pc, name, 0); READ_STRING(thr, pc, name, 0);
@ -1479,7 +1446,7 @@ TSAN_INTERCEPTOR(int, creat, const char *name, int mode) {
return fd; return fd;
} }
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, creat64, const char *name, int mode) { TSAN_INTERCEPTOR(int, creat64, const char *name, int mode) {
SCOPED_TSAN_INTERCEPTOR(creat64, name, mode); SCOPED_TSAN_INTERCEPTOR(creat64, name, mode);
READ_STRING(thr, pc, name, 0); READ_STRING(thr, pc, name, 0);
@ -1509,7 +1476,6 @@ TSAN_INTERCEPTOR(int, dup2, int oldfd, int newfd) {
return newfd2; return newfd2;
} }
#if !SANITIZER_MAC
TSAN_INTERCEPTOR(int, dup3, int oldfd, int newfd, int flags) { TSAN_INTERCEPTOR(int, dup3, int oldfd, int newfd, int flags) {
SCOPED_TSAN_INTERCEPTOR(dup3, oldfd, newfd, flags); SCOPED_TSAN_INTERCEPTOR(dup3, oldfd, newfd, flags);
int newfd2 = REAL(dup3)(oldfd, newfd, flags); int newfd2 = REAL(dup3)(oldfd, newfd, flags);
@ -1517,9 +1483,8 @@ TSAN_INTERCEPTOR(int, dup3, int oldfd, int newfd, int flags) {
FdDup(thr, pc, oldfd, newfd2, false); FdDup(thr, pc, oldfd, newfd2, false);
return newfd2; return newfd2;
} }
#endif
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, eventfd, unsigned initval, int flags) { TSAN_INTERCEPTOR(int, eventfd, unsigned initval, int flags) {
SCOPED_TSAN_INTERCEPTOR(eventfd, initval, flags); SCOPED_TSAN_INTERCEPTOR(eventfd, initval, flags);
int fd = REAL(eventfd)(initval, flags); int fd = REAL(eventfd)(initval, flags);
@ -1532,7 +1497,7 @@ TSAN_INTERCEPTOR(int, eventfd, unsigned initval, int flags) {
#define TSAN_MAYBE_INTERCEPT_EVENTFD #define TSAN_MAYBE_INTERCEPT_EVENTFD
#endif #endif
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, signalfd, int fd, void *mask, int flags) { TSAN_INTERCEPTOR(int, signalfd, int fd, void *mask, int flags) {
SCOPED_TSAN_INTERCEPTOR(signalfd, fd, mask, flags); SCOPED_TSAN_INTERCEPTOR(signalfd, fd, mask, flags);
if (fd >= 0) if (fd >= 0)
@ -1547,7 +1512,7 @@ TSAN_INTERCEPTOR(int, signalfd, int fd, void *mask, int flags) {
#define TSAN_MAYBE_INTERCEPT_SIGNALFD #define TSAN_MAYBE_INTERCEPT_SIGNALFD
#endif #endif
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, inotify_init, int fake) { TSAN_INTERCEPTOR(int, inotify_init, int fake) {
SCOPED_TSAN_INTERCEPTOR(inotify_init, fake); SCOPED_TSAN_INTERCEPTOR(inotify_init, fake);
int fd = REAL(inotify_init)(fake); int fd = REAL(inotify_init)(fake);
@ -1560,7 +1525,7 @@ TSAN_INTERCEPTOR(int, inotify_init, int fake) {
#define TSAN_MAYBE_INTERCEPT_INOTIFY_INIT #define TSAN_MAYBE_INTERCEPT_INOTIFY_INIT
#endif #endif
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, inotify_init1, int flags) { TSAN_INTERCEPTOR(int, inotify_init1, int flags) {
SCOPED_TSAN_INTERCEPTOR(inotify_init1, flags); SCOPED_TSAN_INTERCEPTOR(inotify_init1, flags);
int fd = REAL(inotify_init1)(flags); int fd = REAL(inotify_init1)(flags);
@ -1614,7 +1579,7 @@ TSAN_INTERCEPTOR(int, listen, int fd, int backlog) {
return res; return res;
} }
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, epoll_create, int size) { TSAN_INTERCEPTOR(int, epoll_create, int size) {
SCOPED_TSAN_INTERCEPTOR(epoll_create, size); SCOPED_TSAN_INTERCEPTOR(epoll_create, size);
int fd = REAL(epoll_create)(size); int fd = REAL(epoll_create)(size);
@ -1627,7 +1592,7 @@ TSAN_INTERCEPTOR(int, epoll_create, int size) {
#define TSAN_MAYBE_INTERCEPT_EPOLL_CREATE #define TSAN_MAYBE_INTERCEPT_EPOLL_CREATE
#endif #endif
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, epoll_create1, int flags) { TSAN_INTERCEPTOR(int, epoll_create1, int flags) {
SCOPED_TSAN_INTERCEPTOR(epoll_create1, flags); SCOPED_TSAN_INTERCEPTOR(epoll_create1, flags);
int fd = REAL(epoll_create1)(flags); int fd = REAL(epoll_create1)(flags);
@ -1647,7 +1612,7 @@ TSAN_INTERCEPTOR(int, close, int fd) {
return REAL(close)(fd); return REAL(close)(fd);
} }
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, __close, int fd) { TSAN_INTERCEPTOR(int, __close, int fd) {
SCOPED_TSAN_INTERCEPTOR(__close, fd); SCOPED_TSAN_INTERCEPTOR(__close, fd);
if (fd >= 0) if (fd >= 0)
@ -1660,7 +1625,7 @@ TSAN_INTERCEPTOR(int, __close, int fd) {
#endif #endif
// glibc guts // glibc guts
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(void, __res_iclose, void *state, bool free_addr) { TSAN_INTERCEPTOR(void, __res_iclose, void *state, bool free_addr) {
SCOPED_TSAN_INTERCEPTOR(__res_iclose, state, free_addr); SCOPED_TSAN_INTERCEPTOR(__res_iclose, state, free_addr);
int fds[64]; int fds[64];
@ -1684,7 +1649,6 @@ TSAN_INTERCEPTOR(int, pipe, int *pipefd) {
return res; return res;
} }
#if !SANITIZER_MAC
TSAN_INTERCEPTOR(int, pipe2, int *pipefd, int flags) { TSAN_INTERCEPTOR(int, pipe2, int *pipefd, int flags) {
SCOPED_TSAN_INTERCEPTOR(pipe2, pipefd, flags); SCOPED_TSAN_INTERCEPTOR(pipe2, pipefd, flags);
int res = REAL(pipe2)(pipefd, flags); int res = REAL(pipe2)(pipefd, flags);
@ -1692,7 +1656,6 @@ TSAN_INTERCEPTOR(int, pipe2, int *pipefd, int flags) {
FdPipeCreate(thr, pc, pipefd[0], pipefd[1]); FdPipeCreate(thr, pc, pipefd[0], pipefd[1]);
return res; return res;
} }
#endif
TSAN_INTERCEPTOR(long_t, send, int fd, void *buf, long_t len, int flags) { TSAN_INTERCEPTOR(long_t, send, int fd, void *buf, long_t len, int flags) {
SCOPED_TSAN_INTERCEPTOR(send, fd, buf, len, flags); SCOPED_TSAN_INTERCEPTOR(send, fd, buf, len, flags);
@ -1743,7 +1706,7 @@ TSAN_INTERCEPTOR(void*, tmpfile, int fake) {
return res; return res;
} }
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(void*, tmpfile64, int fake) { TSAN_INTERCEPTOR(void*, tmpfile64, int fake) {
SCOPED_TSAN_INTERCEPTOR(tmpfile64, fake); SCOPED_TSAN_INTERCEPTOR(tmpfile64, fake);
void *res = REAL(tmpfile64)(fake); void *res = REAL(tmpfile64)(fake);
@ -1810,7 +1773,7 @@ TSAN_INTERCEPTOR(int, closedir, void *dirp) {
return REAL(closedir)(dirp); return REAL(closedir)(dirp);
} }
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, epoll_ctl, int epfd, int op, int fd, void *ev) { TSAN_INTERCEPTOR(int, epoll_ctl, int epfd, int op, int fd, void *ev) {
SCOPED_TSAN_INTERCEPTOR(epoll_ctl, epfd, op, fd, ev); SCOPED_TSAN_INTERCEPTOR(epoll_ctl, epfd, op, fd, ev);
if (epfd >= 0) if (epfd >= 0)
@ -1827,7 +1790,7 @@ TSAN_INTERCEPTOR(int, epoll_ctl, int epfd, int op, int fd, void *ev) {
#define TSAN_MAYBE_INTERCEPT_EPOLL_CTL #define TSAN_MAYBE_INTERCEPT_EPOLL_CTL
#endif #endif
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
TSAN_INTERCEPTOR(int, epoll_wait, int epfd, void *ev, int cnt, int timeout) { TSAN_INTERCEPTOR(int, epoll_wait, int epfd, void *ev, int cnt, int timeout) {
SCOPED_TSAN_INTERCEPTOR(epoll_wait, epfd, ev, cnt, timeout); SCOPED_TSAN_INTERCEPTOR(epoll_wait, epfd, ev, cnt, timeout);
if (epfd >= 0) if (epfd >= 0)
@ -2126,7 +2089,6 @@ TSAN_INTERCEPTOR(int, vfork, int fake) {
return WRAP(fork)(fake); return WRAP(fork)(fake);
} }
#if SANITIZER_LINUX
typedef int (*dl_iterate_phdr_cb_t)(__sanitizer_dl_phdr_info *info, SIZE_T size, typedef int (*dl_iterate_phdr_cb_t)(__sanitizer_dl_phdr_info *info, SIZE_T size,
void *data); void *data);
struct dl_iterate_phdr_data { struct dl_iterate_phdr_data {
@ -2170,7 +2132,6 @@ TSAN_INTERCEPTOR(int, dl_iterate_phdr, dl_iterate_phdr_cb_t cb, void *data) {
int res = REAL(dl_iterate_phdr)(dl_iterate_phdr_cb, &cbdata); int res = REAL(dl_iterate_phdr)(dl_iterate_phdr_cb, &cbdata);
return res; return res;
} }
#endif
static int OnExit(ThreadState *thr) { static int OnExit(ThreadState *thr) {
int status = Finalize(thr); int status = Finalize(thr);
@ -2305,11 +2266,9 @@ static void HandleRecvmsg(ThreadState *thr, uptr pc,
MutexRepair(((TsanInterceptorContext *)ctx)->thr, \ MutexRepair(((TsanInterceptorContext *)ctx)->thr, \
((TsanInterceptorContext *)ctx)->pc, (uptr)m) ((TsanInterceptorContext *)ctx)->pc, (uptr)m)
#if !SANITIZER_MAC
#define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) \ #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) \
HandleRecvmsg(((TsanInterceptorContext *)ctx)->thr, \ HandleRecvmsg(((TsanInterceptorContext *)ctx)->thr, \
((TsanInterceptorContext *)ctx)->pc, msg) ((TsanInterceptorContext *)ctx)->pc, msg)
#endif
#define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \ #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
if (TsanThread *t = GetCurrentThread()) { \ if (TsanThread *t = GetCurrentThread()) { \
@ -2450,21 +2409,18 @@ static void unreachable() {
} }
void InitializeInterceptors() { void InitializeInterceptors() {
#if !SANITIZER_MAC
// We need to setup it early, because functions like dlsym() can call it. // We need to setup it early, because functions like dlsym() can call it.
REAL(memset) = internal_memset; REAL(memset) = internal_memset;
REAL(memcpy) = internal_memcpy; REAL(memcpy) = internal_memcpy;
#endif
// Instruct libc malloc to consume less memory. // Instruct libc malloc to consume less memory.
#if !SANITIZER_FREEBSD && !SANITIZER_MAC #if !SANITIZER_FREEBSD
mallopt(1, 0); // M_MXFAST mallopt(1, 0); // M_MXFAST
mallopt(-3, 32*1024); // M_MMAP_THRESHOLD mallopt(-3, 32*1024); // M_MMAP_THRESHOLD
#endif #endif
InitializeCommonInterceptors(); InitializeCommonInterceptors();
#if !SANITIZER_MAC
// We can not use TSAN_INTERCEPT to get setjmp addr, // We can not use TSAN_INTERCEPT to get setjmp addr,
// because it does &setjmp and setjmp is not present in some versions of libc. // because it does &setjmp and setjmp is not present in some versions of libc.
using __interception::GetRealFunctionAddress; using __interception::GetRealFunctionAddress;
@ -2472,7 +2428,6 @@ void InitializeInterceptors() {
GetRealFunctionAddress("_setjmp", (uptr*)&REAL(_setjmp), 0, 0); GetRealFunctionAddress("_setjmp", (uptr*)&REAL(_setjmp), 0, 0);
GetRealFunctionAddress("sigsetjmp", (uptr*)&REAL(sigsetjmp), 0, 0); GetRealFunctionAddress("sigsetjmp", (uptr*)&REAL(sigsetjmp), 0, 0);
GetRealFunctionAddress("__sigsetjmp", (uptr*)&REAL(__sigsetjmp), 0, 0); GetRealFunctionAddress("__sigsetjmp", (uptr*)&REAL(__sigsetjmp), 0, 0);
#endif
TSAN_INTERCEPT(longjmp); TSAN_INTERCEPT(longjmp);
TSAN_INTERCEPT(siglongjmp); TSAN_INTERCEPT(siglongjmp);
@ -2612,12 +2567,9 @@ void InitializeInterceptors() {
TSAN_INTERCEPT(__cxa_atexit); TSAN_INTERCEPT(__cxa_atexit);
TSAN_INTERCEPT(_exit); TSAN_INTERCEPT(_exit);
#if !SANITIZER_MAC
// Need to setup it, because interceptors check that the function is resolved. // Need to setup it, because interceptors check that the function is resolved.
// But atexit is emitted directly into the module, so can't be resolved. // But atexit is emitted directly into the module, so can't be resolved.
REAL(atexit) = (int(*)(void(*)()))unreachable; REAL(atexit) = (int(*)(void(*)()))unreachable;
#endif
if (REAL(__cxa_atexit)(&finalize, 0, 0)) { if (REAL(__cxa_atexit)(&finalize, 0, 0)) {
Printf("ThreadSanitizer: failed to setup atexit callback\n"); Printf("ThreadSanitizer: failed to setup atexit callback\n");
Die(); Die();