1 // REQUIRES: memprof-64-bits
2 // Stress test dynamic TLS + dlopen + threads.
4 // RUN: %clang_memprof -x c -DSO_NAME=f0 %s -shared -o %t-f0.so -fPIC
5 // RUN: %clang_memprof -x c -DSO_NAME=f1 %s -shared -o %t-f1.so -fPIC
6 // RUN: %clang_memprof -x c -DSO_NAME=f2 %s -shared -o %t-f2.so -fPIC
7 // RUN: %clang_memprof %s -ldl -pthread -o %t
10 // RUN: %env_memprof_opts=print_text=true:log_path=stderr:verbosity=2 %run %t 10 2 2>&1 | FileCheck %s
11 // RUN: %env_memprof_opts=print_text=true:log_path=stderr:verbosity=2:intercept_tls_get_addr=1 %run %t 10 2 2>&1 | FileCheck %s
12 // RUN: %env_memprof_opts=print_text=true:log_path=stderr:verbosity=2:intercept_tls_get_addr=0 %run %t 10 2 2>&1 | FileCheck %s --check-prefix=CHECK0
13 // CHECK: ==__tls_get_addr:
14 // CHECK: Creating thread 0
15 // CHECK: ==__tls_get_addr:
16 // CHECK: Creating thread 1
17 // CHECK: ==__tls_get_addr:
18 // CHECK: Creating thread 2
19 // CHECK: ==__tls_get_addr:
20 // CHECK: Creating thread 3
21 // CHECK: ==__tls_get_addr:
22 // Make sure that TLS slots don't leak
23 // CHECK-NOT: num_live_dtls 5
25 // CHECK0-NOT: ==__tls_get_addr:
29 $cc stress_dtls.c -pthread -ldl
30 for((i=0;i<100;i++)); do
31 $cc -fPIC -shared -DSO_NAME=f$i -o a.out-f$i.so stress_dtls.c;
33 ./a.out 2 4 # <<<<<< 2 threads, 4 libs
34 ./a.out 3 50 # <<<<<< 3 threads, 50 libs
45 typedef void **(*f_t
)();
49 #define MAX_N_FUNCTIONS 1000
50 f_t Functions
[MAX_N_FUNCTIONS
];
52 void *PrintStuff(void *unused
) {
54 // fprintf(stderr, "STACK: %p TLS: %p SELF: %p\n", &stack, &my_tls,
55 // (void *)pthread_self());
57 for (i
= 0; i
< MAX_N_FUNCTIONS
; i
++) {
60 uintptr_t dtls
= (uintptr_t)Functions
[i
]();
61 fprintf(stderr
, " dtls[%03d]: %lx\n", i
, dtls
);
62 *(long *)dtls
= 42; // check that this is writable.
67 int main(int argc
, char *argv
[]) {
71 num_threads
= atoi(argv
[1]);
73 num_libs
= atoi(argv
[2]);
74 assert(num_libs
<= MAX_N_FUNCTIONS
);
77 for (lib
= 0; lib
< num_libs
; lib
++) {
79 snprintf(buf
, sizeof(buf
), "%s-f%d.so", argv
[0], lib
);
80 void *handle
= dlopen(buf
, RTLD_LAZY
);
82 fprintf(stderr
, "%s\n", dlerror());
85 snprintf(buf
, sizeof(buf
), "f%d", lib
);
86 Functions
[lib
] = (f_t
)dlsym(handle
, buf
);
87 if (!Functions
[lib
]) {
88 fprintf(stderr
, "%s\n", dlerror());
91 fprintf(stderr
, "LIB[%03d] %s: %p\n", lib
, buf
, Functions
[lib
]);
95 for (i
= 0; i
< num_threads
; i
++) {
97 fprintf(stderr
, "Creating thread %d\n", i
);
98 pthread_create(&t
, 0, PrintStuff
, 0);
106 #define DTLS_SIZE (1 << 17)
108 __thread
void *huge_thread_local_array
[DTLS_SIZE
];
110 return &huge_thread_local_array
[0];