From 578e66ac45dfcc5c739f3525bfb82d71282d925c Mon Sep 17 00:00:00 2001 From: Alexander Richardson Date: Fri, 8 Mar 2024 22:09:10 -0800 Subject: [PATCH] [tsan] Intercept __tls_get_addr_earlier This can be useful because dlsym() may call malloc on failure which could result in other interposed functions being called that could eventually make use of TLS. While the crash that I experienced originally has been fixed differently (by not using global-dynamic TLS accesses in the mutex deadlock detector, see https://github.com/llvm/llvm-project/pull/83890), moving this interception earlier is still a good since it makes the code a bit more robust against initialization order problems. Reviewed By: MaskRay, vitalybuka Pull Request: https://github.com/llvm/llvm-project/pull/83886 --- .../lib/tsan/rtl/tsan_interceptors_posix.cpp | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp index a9f6673ac44e90..24309ab66b2e67 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp +++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp @@ -2863,6 +2863,17 @@ void InitializeInterceptors() { new(interceptor_ctx()) InterceptorContext(); + // Interpose __tls_get_addr before the common interposers. This is needed + // because dlsym() may call malloc on failure which could result in other + // interposed functions being called that could eventually make use of TLS. +#ifdef NEED_TLS_GET_ADDR +# if !SANITIZER_S390 + TSAN_INTERCEPT(__tls_get_addr); +# else + TSAN_INTERCEPT(__tls_get_addr_internal); + TSAN_INTERCEPT(__tls_get_offset); +# endif +#endif InitializeCommonInterceptors(); InitializeSignalInterceptors(); InitializeLibdispatchInterceptors(); @@ -3010,15 +3021,6 @@ void InitializeInterceptors() { TSAN_INTERCEPT(__cxa_atexit); TSAN_INTERCEPT(_exit); -#ifdef NEED_TLS_GET_ADDR -#if !SANITIZER_S390 - TSAN_INTERCEPT(__tls_get_addr); -#else - TSAN_INTERCEPT(__tls_get_addr_internal); - TSAN_INTERCEPT(__tls_get_offset); -#endif -#endif - TSAN_MAYBE_INTERCEPT__LWP_EXIT; TSAN_MAYBE_INTERCEPT_THR_EXIT;