llvm-project/compiler-rt/lib/tsan
Fangrui Song afec953857 [sanitizer] Simplify GetTls with dl_iterate_phdr on Linux and use it on musl/FreeBSD
... so that FreeBSD specific GetTls/glibc specific pthread_self code can be
removed. This also helps FreeBSD arm64/powerpc64 which don't have GetTls
implementation yet.

GetTls is the range of

* thread control block and optional TLS_PRE_TCB_SIZE
* static TLS blocks plus static TLS surplus

On glibc, lsan requires the range to include
`pthread::{specific_1stblock,specific}` so that allocations only referenced by
`pthread_setspecific` can be scanned.

This patch uses `dl_iterate_phdr` to collect TLS blocks. Find the one
with `dlpi_tls_modid==1` as one of the initially loaded module, then find
consecutive ranges. The boundaries give us addr and size.

This allows us to drop the glibc internal `_dl_get_tls_static_info` and
`InitTlsSize`. However, huge glibc x86-64 binaries with numerous shared objects
may observe time complexity penalty, so exclude them for now. Use the simplified
method with non-Android Linux for now, but in theory this can be used with *BSD
and potentially other ELF OSes.

This removal of RISC-V `__builtin_thread_pointer` makes the code compilable with
more compiler versions (added in Clang in 2020-03, added in GCC in 2020-07).

This simplification enables D99566 for TLS Variant I architectures.

Note: as of musl 1.2.2 and FreeBSD 12.2, dlpi_tls_data returned by
dl_iterate_phdr is not desired: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=254774
This can be worked around by using `__tls_get_addr({modid,0})` instead
of `dlpi_tls_data`. The workaround can be shared with the workaround for glibc<2.25.

This fixes some tests on Alpine Linux x86-64 (musl)

```
test/lsan/Linux/cleanup_in_tsd_destructor.c
test/lsan/Linux/fork.cpp
test/lsan/Linux/fork_threaded.cpp
test/lsan/Linux/use_tls_static.cpp
test/lsan/many_tls_keys_thread.cpp

test/msan/tls_reuse.cpp
```

and `test/lsan/TestCases/many_tls_keys_pthread.cpp` on glibc aarch64.

The number of sanitizer test failures does not change on FreeBSD/amd64 12.2.

Differential Revision: https://reviews.llvm.org/D98926
2021-04-15 15:34:43 -07:00
..
benchmarks compiler-rt: Rename .cc file in lib/tsan/{benchmarks,dd,go} to .cpp 2019-08-01 14:30:49 +00:00
dd [tsan] Remove stdlib.h from dd_interceptors.cpp 2020-12-29 14:00:54 -08:00
go [sanitizers] Pass CMAKE_C_FLAGS into TSan buildgo script 2021-02-22 18:49:02 +01:00
rtl [sanitizer] Simplify GetTls with dl_iterate_phdr on Linux and use it on musl/FreeBSD 2021-04-15 15:34:43 -07:00
tests [CMake] Add -fno-rtti into tsan unittests 2020-10-30 20:03:38 -07:00
.clang-format [sanitizer] Remove unneeded blank lines 2018-05-09 00:44:26 +00:00
CMakeLists.txt [gn build] add build file for tsan runtime 2021-04-02 12:59:14 -04:00
analyze_libtsan.sh Revert "[NFC][TSAN] Logs to debug test script on bot" 2020-10-29 01:11:16 -07:00
check_analyze.sh Revert "[NFC][TSAN] Logs to debug test script on bot" 2020-10-29 01:11:16 -07:00
check_cmake.sh