Path: blob/main/contrib/llvm-project/compiler-rt/lib/tsan/rtl/tsan_interceptors.h
35269 views
#ifndef TSAN_INTERCEPTORS_H1#define TSAN_INTERCEPTORS_H23#include "sanitizer_common/sanitizer_stacktrace.h"4#include "tsan_rtl.h"56namespace __tsan {78class ScopedInterceptor {9public:10ScopedInterceptor(ThreadState *thr, const char *fname, uptr pc);11~ScopedInterceptor();12void DisableIgnores() {13if (UNLIKELY(ignoring_))14DisableIgnoresImpl();15}16void EnableIgnores() {17if (UNLIKELY(ignoring_))18EnableIgnoresImpl();19}2021private:22ThreadState *const thr_;23bool in_ignored_lib_ = false;24bool in_blocking_func_ = false;25bool ignoring_ = false;2627void DisableIgnoresImpl();28void EnableIgnoresImpl();29};3031struct TsanInterceptorContext {32ThreadState *thr;33const uptr pc;34};3536LibIgnore *libignore();3738#if !SANITIZER_GO39inline bool in_symbolizer() {40return UNLIKELY(cur_thread_init()->in_symbolizer);41}42#endif4344inline bool MustIgnoreInterceptor(ThreadState *thr) {45return !thr->is_inited || thr->ignore_interceptors || thr->in_ignored_lib;46}4748} // namespace __tsan4950#define SCOPED_INTERCEPTOR_RAW(func, ...) \51ThreadState *thr = cur_thread_init(); \52ScopedInterceptor si(thr, #func, GET_CALLER_PC()); \53UNUSED const uptr pc = GET_CURRENT_PC();5455#ifdef __powerpc64__56// Debugging of crashes on powerpc after commit:57// c80604f7a3 ("tsan: remove real func check from interceptors")58// Somehow replacing if with DCHECK leads to strange failures in:59// SanitizerCommon-tsan-powerpc64le-Linux :: Linux/ptrace.cpp60// https://lab.llvm.org/buildbot/#/builders/10561// https://lab.llvm.org/buildbot/#/builders/12162// https://lab.llvm.org/buildbot/#/builders/5763# define CHECK_REAL_FUNC(func) \64if (REAL(func) == 0) { \65Report("FATAL: ThreadSanitizer: failed to intercept %s\n", #func); \66Die(); \67}68#else69# define CHECK_REAL_FUNC(func) DCHECK(REAL(func))70#endif7172#define SCOPED_TSAN_INTERCEPTOR(func, ...) \73SCOPED_INTERCEPTOR_RAW(func, __VA_ARGS__); \74CHECK_REAL_FUNC(func); \75if (MustIgnoreInterceptor(thr)) \76return REAL(func)(__VA_ARGS__);7778#define SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_START() \79si.DisableIgnores();8081#define SCOPED_TSAN_INTERCEPTOR_USER_CALLBACK_END() \82si.EnableIgnores();8384#define TSAN_INTERCEPTOR(ret, func, ...) INTERCEPTOR(ret, func, __VA_ARGS__)8586#if SANITIZER_FREEBSD87# define TSAN_INTERCEPTOR_FREEBSD_ALIAS(ret, func, ...) \88TSAN_INTERCEPTOR(ret, _pthread_##func, __VA_ARGS__) \89ALIAS(WRAP(pthread_##func));90#else91# define TSAN_INTERCEPTOR_FREEBSD_ALIAS(ret, func, ...)92#endif9394#if SANITIZER_NETBSD95# define TSAN_INTERCEPTOR_NETBSD_ALIAS(ret, func, ...) \96TSAN_INTERCEPTOR(ret, __libc_##func, __VA_ARGS__) \97ALIAS(WRAP(pthread_##func));98# define TSAN_INTERCEPTOR_NETBSD_ALIAS_THR(ret, func, ...) \99TSAN_INTERCEPTOR(ret, __libc_thr_##func, __VA_ARGS__) \100ALIAS(WRAP(pthread_##func));101# define TSAN_INTERCEPTOR_NETBSD_ALIAS_THR2(ret, func, func2, ...) \102TSAN_INTERCEPTOR(ret, __libc_thr_##func, __VA_ARGS__) \103ALIAS(WRAP(pthread_##func2));104#else105# define TSAN_INTERCEPTOR_NETBSD_ALIAS(ret, func, ...)106# define TSAN_INTERCEPTOR_NETBSD_ALIAS_THR(ret, func, ...)107# define TSAN_INTERCEPTOR_NETBSD_ALIAS_THR2(ret, func, func2, ...)108#endif109110#define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name)111112#define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED \113(!cur_thread_init()->is_inited)114115#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \116MemoryAccessRange(((TsanInterceptorContext *)ctx)->thr, \117((TsanInterceptorContext *)ctx)->pc, (uptr)ptr, size, \118true)119120#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \121MemoryAccessRange(((TsanInterceptorContext *) ctx)->thr, \122((TsanInterceptorContext *) ctx)->pc, (uptr) ptr, size, \123false)124125#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \126SCOPED_TSAN_INTERCEPTOR(func, __VA_ARGS__); \127TsanInterceptorContext _ctx = {thr, pc}; \128ctx = (void *)&_ctx; \129(void)ctx;130131#endif // TSAN_INTERCEPTORS_H132133134