1 // RUN: %clang_tsan %s -o %t
2 // RUN: %clang_tsan %s -DBUILD_SO -fPIC -o %t-so.so -shared
3 // RUN: %run %t 2>&1 | FileCheck %s
6 // Test that tsan cleans up dynamic TLS memory between reuse.
14 typedef volatile long *(* get_t)();
17 void *Thread1(void *arg) {
18 pthread_detach(pthread_self());
19 volatile long *x = GetTls();
21 fprintf(stderr, "stack: %p dtls: %p\n", &x, x);
22 barrier_wait(&barrier);
26 void *Thread2(void *arg) {
27 volatile long *x = GetTls();
29 fprintf(stderr, "stack: %p dtls: %p\n", &x, x);
33 int main(int argc, char *argv[]) {
35 snprintf(path, sizeof(path), "%s-so.so", argv[0]);
37 void *handle = dlopen(path, RTLD_LAZY);
38 if (!handle) fprintf(stderr, "%s\n", dlerror());
40 GetTls = (get_t)dlsym(handle, "GetTls");
41 assert(dlerror() == 0);
43 barrier_init(&barrier, 2);
45 pthread_create(&t[0], 0, Thread1, 0);
46 barrier_wait(&barrier);
47 // Wait for actual thread termination without using pthread_join,
48 // which would synchronize threads.
50 pthread_create(&t[1], 0, Thread2, 0);
51 pthread_join(t[1], 0);
52 fprintf(stderr, "DONE\n");
56 __thread long huge_thread_local_array[1 << 17];
58 return &huge_thread_local_array[0];
62 // CHECK-NOT: ThreadSanitizer: data race