]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/tsan/dtls.c
Vendor import of compiler-rt trunk r351319 (just before the release_80
[FreeBSD/FreeBSD.git] / test / tsan / dtls.c
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
4 // XFAIL: netbsd
5
6 // Test that tsan cleans up dynamic TLS memory between reuse.
7
8 #include "test.h"
9
10 #ifndef BUILD_SO
11 #include <assert.h>
12 #include <dlfcn.h>
13
14 typedef volatile long *(* get_t)();
15 get_t GetTls;
16
17 void *Thread1(void *arg) {
18   pthread_detach(pthread_self());
19   volatile long *x = GetTls();
20   *x = 42;
21   fprintf(stderr, "stack: %p dtls: %p\n", &x, x);
22   barrier_wait(&barrier);
23   return 0;
24 }
25
26 void *Thread2(void *arg) {
27   volatile long *x = GetTls();
28   *x = 42;
29   fprintf(stderr, "stack: %p dtls: %p\n", &x, x);
30   return 0;
31 }
32
33 int main(int argc, char *argv[]) {
34   char path[4096];
35   snprintf(path, sizeof(path), "%s-so.so", argv[0]);
36
37   void *handle = dlopen(path, RTLD_LAZY);
38   if (!handle) fprintf(stderr, "%s\n", dlerror());
39   assert(handle != 0);
40   GetTls = (get_t)dlsym(handle, "GetTls");
41   assert(dlerror() == 0);
42
43   barrier_init(&barrier, 2);
44   pthread_t t[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.
49   sleep(1);
50   pthread_create(&t[1], 0, Thread2, 0);
51   pthread_join(t[1], 0);
52   fprintf(stderr, "DONE\n");
53   return 0;
54 }
55 #else  // BUILD_SO
56 __thread long huge_thread_local_array[1 << 17];
57 long *GetTls() {
58   return &huge_thread_local_array[0];
59 }
60 #endif
61
62 // CHECK-NOT: ThreadSanitizer: data race
63 // CHECK: DONE