1 // REQUIRES: asan-64-bits
2 // UNSUPPORTED: android
3 // Stress test dynamic TLS + dlopen + threads.
5 // Note that glibc 2.15 seems utterly broken on this test,
6 // it fails with ~17 DSOs dlopen-ed.
7 // glibc 2.19 seems fine.
10 // RUN: %clangxx_asan -x c -DSO_NAME=f0 %s -shared -o %t-f0.so -fPIC
11 // RUN: %clangxx_asan -x c -DSO_NAME=f1 %s -shared -o %t-f1.so -fPIC
12 // RUN: %clangxx_asan -x c -DSO_NAME=f2 %s -shared -o %t-f2.so -fPIC
13 // RUN: %clangxx_asan %s -ldl -pthread -o %t
16 // RUN: %env_asan_opts=verbosity=2 %run %t 10 2 2>&1 | FileCheck %s
17 // RUN: %env_asan_opts=verbosity=2:intercept_tls_get_addr=1 %run %t 10 2 2>&1 | FileCheck %s
18 // RUN: %env_asan_opts=verbosity=2:intercept_tls_get_addr=0 %run %t 10 2 2>&1 | FileCheck %s --check-prefix=CHECK0
19 // CHECK: __tls_get_addr
20 // CHECK: Creating thread 0
21 // CHECK: __tls_get_addr
22 // CHECK: Creating thread 1
23 // CHECK: __tls_get_addr
24 // CHECK: Creating thread 2
25 // CHECK: __tls_get_addr
26 // CHECK: Creating thread 3
27 // CHECK: __tls_get_addr
28 // Make sure that TLS slots don't leak
29 // CHECK-NOT: num_live_dtls 5
31 // CHECK0-NOT: __tls_get_addr
35 $cc stress_dtls.c -pthread -ldl
36 for((i=0;i<100;i++)); do
37 $cc -fPIC -shared -DSO_NAME=f$i -o a.out-f$i.so stress_dtls.c;
39 ./a.out 2 4 # <<<<<< 2 threads, 4 libs
40 ./a.out 3 50 # <<<<<< 3 threads, 50 libs
51 typedef void **(*f_t
)();
55 #define MAX_N_FUNCTIONS 1000
56 f_t Functions
[MAX_N_FUNCTIONS
];
58 void *PrintStuff(void *unused
) {
60 // fprintf(stderr, "STACK: %p TLS: %p SELF: %p\n", &stack, &my_tls,
61 // (void *)pthread_self());
63 for (i
= 0; i
< MAX_N_FUNCTIONS
; i
++) {
64 if (!Functions
[i
]) break;
65 uintptr_t dtls
= (uintptr_t)Functions
[i
]();
66 fprintf(stderr
, " dtls[%03d]: %lx\n", i
, dtls
);
67 *(long*)dtls
= 42; // check that this is writable.
72 int main(int argc
, char *argv
[]) {
76 num_threads
= atoi(argv
[1]);
78 num_libs
= atoi(argv
[2]);
79 assert(num_libs
<= MAX_N_FUNCTIONS
);
82 for (lib
= 0; lib
< num_libs
; lib
++) {
84 snprintf(buf
, sizeof(buf
), "%s-f%d.so", argv
[0], lib
);
85 void *handle
= dlopen(buf
, RTLD_LAZY
);
87 fprintf(stderr
, "%s\n", dlerror());
90 snprintf(buf
, sizeof(buf
), "f%d", lib
);
91 Functions
[lib
] = (f_t
)dlsym(handle
, buf
);
92 if (!Functions
[lib
]) {
93 fprintf(stderr
, "%s\n", dlerror());
96 fprintf(stderr
, "LIB[%03d] %s: %p\n", lib
, buf
, Functions
[lib
]);
100 for (i
= 0; i
< num_threads
; i
++) {
102 fprintf(stderr
, "Creating thread %d\n", i
);
103 pthread_create(&t
, 0, PrintStuff
, 0);
111 # define DTLS_SIZE (1 << 17)
113 __thread
void *huge_thread_local_array
[DTLS_SIZE
];
115 return &huge_thread_local_array
[0];