Move MSan lit-tests under test/msan
[blocksruntime.git] / lib / asan / lit_tests / TestCases / Linux / stress_dtls.c
blob4e22f8ae9a1d94182a942c0130dbf39d33e96bc0
1 // REQUIRES: asan-64-bits
2 // Stress test dynamic TLS + dlopen + threads.
3 //
4 // Note that glibc 2.15 seems utterly broken on this test,
5 // it fails with ~17 DSOs dlopen-ed.
6 // glibc 2.19 seems fine.
7 //
8 //
9 // RUN: %clangxx_asan -x c -DSO_NAME=f0 %s -shared -o %t-f0.so -fPIC
10 // RUN: %clangxx_asan -x c -DSO_NAME=f1 %s -shared -o %t-f1.so -fPIC
11 // RUN: %clangxx_asan -x c -DSO_NAME=f2 %s -shared -o %t-f2.so -fPIC
12 // RUN: %clangxx_asan %s -o %t
13 // RUN: %t 0 3
14 // RUN: %t 2 3
15 // RUN: ASAN_OPTIONS=verbosity=2 %t 2 2 2>&1 | FileCheck %s
16 // CHECK: __tls_get_addr
17 // CHECK: __tls_get_addr
18 // CHECK: __tls_get_addr
19 // CHECK: __tls_get_addr
20 // CHECK: __tls_get_addr
22 cc=your-compiler
24 $cc stress_dtls.c -lpthread -ldl
25 for((i=0;i<100;i++)); do
26 $cc -fPIC -shared -DSO_NAME=f$i -o a.out-f$i.so stress_dtls.c;
27 done
28 ./a.out 2 4 # <<<<<< 2 threads, 4 libs
29 ./a.out 3 50 # <<<<<< 3 threads, 50 libs
31 #ifndef SO_NAME
32 #define _GNU_SOURCE
33 #include <assert.h>
34 #include <dlfcn.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <pthread.h>
38 #include <stdint.h>
40 typedef void **(*f_t)();
42 __thread int my_tls;
44 #define MAX_N_FUNCTIONS 1000
45 f_t Functions[MAX_N_FUNCTIONS];
47 void *PrintStuff(void *unused) {
48 uintptr_t stack;
49 // fprintf(stderr, "STACK: %p TLS: %p SELF: %p\n", &stack, &my_tls,
50 // (void *)pthread_self());
51 int i;
52 for (i = 0; i < MAX_N_FUNCTIONS; i++) {
53 if (!Functions[i]) break;
54 uintptr_t dtls = (uintptr_t)Functions[i]();
55 fprintf(stderr, " dtls[%03d]: %lx\n", i, dtls);
56 *(long*)dtls = 42; // check that this is writable.
58 return NULL;
61 int main(int argc, char *argv[]) {
62 int num_threads = 1;
63 int num_libs = 1;
64 if (argc >= 2)
65 num_threads = atoi(argv[1]);
66 if (argc >= 3)
67 num_libs = atoi(argv[2]);
68 assert(num_libs <= MAX_N_FUNCTIONS);
70 int lib;
71 for (lib = 0; lib < num_libs; lib++) {
72 char buf[4096];
73 snprintf(buf, sizeof(buf), "%s-f%d.so", argv[0], lib);
74 void *handle = dlopen(buf, RTLD_LAZY);
75 if (!handle) {
76 fprintf(stderr, "%s\n", dlerror());
77 exit(1);
79 snprintf(buf, sizeof(buf), "f%d", lib);
80 Functions[lib] = (f_t)dlsym(handle, buf);
81 if (!Functions[lib]) {
82 fprintf(stderr, "%s\n", dlerror());
83 exit(1);
85 fprintf(stderr, "LIB[%03d] %s: %p\n", lib, buf, Functions[lib]);
86 PrintStuff(0);
88 int i;
89 for (i = 0; i < num_threads; i++) {
90 pthread_t t;
91 pthread_create(&t, 0, PrintStuff, 0);
92 pthread_join(t, 0);
95 return 0;
97 #else // SO_NAME
98 #ifndef DTLS_SIZE
99 # define DTLS_SIZE (1 << 17)
100 #endif
101 __thread void *huge_thread_local_array[DTLS_SIZE];
102 void **SO_NAME() {
103 return &huge_thread_local_array[0];
105 #endif