llvm-project
64 строки · 1.5 Кб
1// RUN: %clang_tsan %darwin_min_target_with_tls_support %s -o %t
2// RUN: %clang_tsan %darwin_min_target_with_tls_support %s -DBUILD_SO -fPIC -o \
3// RUN: %t-so.so -shared
4// RUN: %run %t 2>&1 | FileCheck %s
5// XFAIL: target={{.*netbsd.*}}
6
7// Test that tsan cleans up dynamic TLS memory between reuse.
8
9#include "test.h"10
11#ifndef BUILD_SO12#include <assert.h>13#include <dlfcn.h>14
15typedef volatile long *(* get_t)();16get_t GetTls;17
18void *Thread1(void *arg) {19pthread_detach(pthread_self());20volatile long *x = GetTls();21*x = 42;22fprintf(stderr, "stack: %p dtls: %p\n", &x, x);23barrier_wait(&barrier);24return 0;25}
26
27void *Thread2(void *arg) {28volatile long *x = GetTls();29*x = 42;30fprintf(stderr, "stack: %p dtls: %p\n", &x, x);31return 0;32}
33
34int main(int argc, char *argv[]) {35char path[4096];36snprintf(path, sizeof(path), "%s-so.so", argv[0]);37
38void *handle = dlopen(path, RTLD_LAZY);39if (!handle) fprintf(stderr, "%s\n", dlerror());40assert(handle != 0);41GetTls = (get_t)dlsym(handle, "GetTls");42assert(dlerror() == 0);43
44barrier_init(&barrier, 2);45pthread_t t[2];46pthread_create(&t[0], 0, Thread1, 0);47barrier_wait(&barrier);48// Wait for actual thread termination without using pthread_join,49// which would synchronize threads.50sleep(1);51pthread_create(&t[1], 0, Thread2, 0);52pthread_join(t[1], 0);53fprintf(stderr, "DONE\n");54return 0;55}
56#else // BUILD_SO57__thread long huge_thread_local_array[1 << 17];58long *GetTls() {59return &huge_thread_local_array[0];60}
61#endif62
63// CHECK-NOT: ThreadSanitizer: data race
64// CHECK: DONE
65