... 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
79 lines
2.1 KiB
C++
79 lines
2.1 KiB
C++
// Test that lsan handles tls correctly for many threads
|
|
// RUN: LSAN_BASE="report_objects=1:use_stacks=0:use_registers=0"
|
|
// RUN: %clangxx_lsan %s -o %t
|
|
// RUN: %env_lsan_opts=$LSAN_BASE:"use_tls=0" not %run %t 2>&1 | FileCheck %s
|
|
// RUN: %env_lsan_opts=$LSAN_BASE:"use_tls=1" %run %t 2>&1
|
|
// RUN: %env_lsan_opts="" %run %t 2>&1
|
|
|
|
// On glibc, this requires the range returned by GetTLS to include
|
|
// specific_1stblock and specific in `struct pthread`.
|
|
// UNSUPPORTED: arm-linux, armhf-linux
|
|
|
|
// TSD on NetBSD does not use TLS
|
|
// UNSUPPORTED: netbsd
|
|
|
|
#include <assert.h>
|
|
#include <limits.h>
|
|
#include <pthread.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
static const int NUM_THREADS = 10;
|
|
|
|
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
|
|
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
int finished = 0;
|
|
|
|
// We won't be able to create the maximum number of keys, due to other users
|
|
// of the tls, but we'll use as many keys as we can before failing to create
|
|
// a new key.
|
|
pthread_key_t keys[PTHREAD_KEYS_MAX];
|
|
static const int PTHREAD_KEY_INVALID = 0xffffffff;
|
|
|
|
void alloc() {
|
|
for (int i = 0; i < PTHREAD_KEYS_MAX; ++i) {
|
|
void *ptr = malloc(123);
|
|
if ((keys[i] == PTHREAD_KEY_INVALID) || pthread_setspecific(keys[i], ptr)) {
|
|
free(ptr);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void pthread_destructor(void *arg) {
|
|
assert(0 && "pthread destructors shouldn't be called");
|
|
}
|
|
|
|
void *thread_start(void *arg) {
|
|
alloc();
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
finished++;
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
// don't exit, to intentionally leak tls data
|
|
while (1)
|
|
sleep(100);
|
|
}
|
|
|
|
int main() {
|
|
for (int i = 0; i < PTHREAD_KEYS_MAX; ++i) {
|
|
if (pthread_key_create(&keys[i], pthread_destructor)) {
|
|
keys[i] = PTHREAD_KEY_INVALID;
|
|
break;
|
|
}
|
|
}
|
|
|
|
pthread_t thread[NUM_THREADS];
|
|
for (int i = 0; i < NUM_THREADS; ++i) {
|
|
assert(0 == pthread_create(&thread[i], 0, thread_start, 0));
|
|
}
|
|
// spin until all threads have finished
|
|
while (finished < NUM_THREADS)
|
|
sleep(1);
|
|
exit(0);
|
|
}
|
|
|
|
// CHECK: LeakSanitizer: detected memory leaks
|
|
// CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer:
|