1 #ifndef TSAN_INTERCEPTORS_H
2 #define TSAN_INTERCEPTORS_H
4 #include "sanitizer_common/sanitizer_stacktrace.h"
9 class ScopedInterceptor
{
11 ScopedInterceptor(ThreadState
*thr
, const char *fname
, uptr pc
);
13 void DisableIgnores() {
14 if (UNLIKELY(ignoring_
))
17 void EnableIgnores() {
18 if (UNLIKELY(ignoring_
))
23 ThreadState
*const thr_
;
24 bool in_ignored_lib_
= false;
25 bool in_blocking_func_
= false;
26 bool ignoring_
= false;
28 void DisableIgnoresImpl();
29 void EnableIgnoresImpl();
32 struct TsanInterceptorContext
{
37 LibIgnore
*libignore();
40 inline bool in_symbolizer() {
41 return UNLIKELY(cur_thread_init()->in_symbolizer
);
45 inline bool MustIgnoreInterceptor(ThreadState
*thr
) {
46 return !thr
->is_inited
|| thr
->ignore_interceptors
|| thr
->in_ignored_lib
;
51 #define SCOPED_INTERCEPTOR_RAW(func, ...) \
52 ThreadState *thr = cur_thread_init(); \
53 ScopedInterceptor si(thr, #func, GET_CALLER_PC()); \
54 UNUSED const uptr pc = GET_CURRENT_PC();
57 // Debugging of crashes on powerpc after commit:
58 // c80604f7a3 ("tsan: remove real func check from interceptors")
59 // Somehow replacing if with DCHECK leads to strange failures in:
60 // SanitizerCommon-tsan-powerpc64le-Linux :: Linux/ptrace.cpp
61 // https://lab.llvm.org/buildbot/#/builders/105
62 // https://lab.llvm.org/buildbot/#/builders/121
63 // https://lab.llvm.org/buildbot/#/builders/57
64 # define CHECK_REAL_FUNC(func) \
65 if (REAL(func) == 0) { \
66 Report("FATAL: ThreadSanitizer: failed to intercept %s\n", #func); \
70 # define CHECK_REAL_FUNC(func) DCHECK(REAL(func))
73 #define SCOPED_TSAN_INTERCEPTOR(func, ...) \
74 SCOPED_INTERCEPTOR_RAW(func, __VA_ARGS__); \
75 CHECK_REAL_FUNC(func); \
76 if (MustIgnoreInterceptor(thr)) \
77 return REAL(func)(__VA_ARGS__);
79 #define SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START() \
82 #define SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END() \
85 #define TSAN_INTERCEPTOR(ret, func, ...) INTERCEPTOR(ret, func, __VA_ARGS__)
88 # define TSAN_INTERCEPTOR_FREEBSD_ALIAS(ret, func, ...) \
89 TSAN_INTERCEPTOR(ret, _pthread_##func, __VA_ARGS__) \
90 ALIAS(WRAP(pthread_##func));
92 # define TSAN_INTERCEPTOR_FREEBSD_ALIAS(ret, func, ...)
96 # define TSAN_INTERCEPTOR_NETBSD_ALIAS(ret, func, ...) \
97 TSAN_INTERCEPTOR(ret, __libc_##func, __VA_ARGS__) \
98 ALIAS(WRAP(pthread_##func));
99 # define TSAN_INTERCEPTOR_NETBSD_ALIAS_THR(ret, func, ...) \
100 TSAN_INTERCEPTOR(ret, __libc_thr_##func, __VA_ARGS__) \
101 ALIAS(WRAP(pthread_##func));
102 # define TSAN_INTERCEPTOR_NETBSD_ALIAS_THR2(ret, func, func2, ...) \
103 TSAN_INTERCEPTOR(ret, __libc_thr_##func, __VA_ARGS__) \
104 ALIAS(WRAP(pthread_##func2));
106 # define TSAN_INTERCEPTOR_NETBSD_ALIAS(ret, func, ...)
107 # define TSAN_INTERCEPTOR_NETBSD_ALIAS_THR(ret, func, ...)
108 # define TSAN_INTERCEPTOR_NETBSD_ALIAS_THR2(ret, func, func2, ...)
111 #define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name)
113 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED \
114 (!cur_thread_init()->is_inited)
116 #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
117 MemoryAccessRange(((TsanInterceptorContext *)ctx)->thr, \
118 ((TsanInterceptorContext *)ctx)->pc, (uptr)ptr, size, \
121 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
122 MemoryAccessRange(((TsanInterceptorContext *) ctx)->thr, \
123 ((TsanInterceptorContext *) ctx)->pc, (uptr) ptr, size, \
126 #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
127 SCOPED_TSAN_INTERCEPTOR(func, __VA_ARGS__); \
128 TsanInterceptorContext _ctx = {thr, pc}; \
129 ctx = (void *)&_ctx; \
132 #endif // TSAN_INTERCEPTORS_H