Home | History | Annotate | Download | only in Linux
      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 -ldl -pthread -o %t
     13 // RUN: %run %t 0 3
     14 // RUN: %run %t 2 3
     15 // RUN: ASAN_OPTIONS=verbosity=2 %run %t 10 2 2>&1 | FileCheck %s
     16 // RUN: ASAN_OPTIONS=verbosity=2:intercept_tls_get_addr=1 %run %t 10 2 2>&1 | FileCheck %s
     17 // RUN: ASAN_OPTIONS=verbosity=2:intercept_tls_get_addr=0 %run %t 10 2 2>&1 | FileCheck %s --check-prefix=CHECK0
     18 // CHECK: __tls_get_addr
     19 // CHECK: Creating thread 0
     20 // CHECK: __tls_get_addr
     21 // CHECK: Creating thread 1
     22 // CHECK: __tls_get_addr
     23 // CHECK: Creating thread 2
     24 // CHECK: __tls_get_addr
     25 // CHECK: Creating thread 3
     26 // CHECK: __tls_get_addr
     27 // Make sure that TLS slots don't leak
     28 // CHECK-NOT: num_live_dtls 5
     29 //
     30 // CHECK0-NOT: __tls_get_addr
     31 /*
     32 cc=your-compiler
     33 
     34 $cc stress_dtls.c -pthread -ldl
     35 for((i=0;i<100;i++)); do
     36   $cc -fPIC -shared -DSO_NAME=f$i -o a.out-f$i.so stress_dtls.c;
     37 done
     38 ./a.out 2 4  # <<<<<< 2 threads, 4 libs
     39 ./a.out 3 50 # <<<<<< 3 threads, 50 libs
     40 */
     41 #ifndef SO_NAME
     42 #define _GNU_SOURCE
     43 #include <assert.h>
     44 #include <dlfcn.h>
     45 #include <stdio.h>
     46 #include <stdlib.h>
     47 #include <pthread.h>
     48 #include <stdint.h>
     49 
     50 typedef void **(*f_t)();
     51 
     52 __thread int my_tls;
     53 
     54 #define MAX_N_FUNCTIONS 1000
     55 f_t Functions[MAX_N_FUNCTIONS];
     56 
     57 void *PrintStuff(void *unused) {
     58   uintptr_t stack;
     59   // fprintf(stderr, "STACK: %p TLS: %p SELF: %p\n", &stack, &my_tls,
     60   //        (void *)pthread_self());
     61   int i;
     62   for (i = 0; i < MAX_N_FUNCTIONS; i++) {
     63     if (!Functions[i]) break;
     64     uintptr_t dtls = (uintptr_t)Functions[i]();
     65     fprintf(stderr, "  dtls[%03d]: %lx\n", i, dtls);
     66     *(long*)dtls = 42;  // check that this is writable.
     67   }
     68   return NULL;
     69 }
     70 
     71 int main(int argc, char *argv[]) {
     72   int num_threads = 1;
     73   int num_libs = 1;
     74   if (argc >= 2)
     75     num_threads = atoi(argv[1]);
     76   if (argc >= 3)
     77     num_libs = atoi(argv[2]);
     78   assert(num_libs <= MAX_N_FUNCTIONS);
     79 
     80   int lib;
     81   for (lib = 0; lib < num_libs; lib++) {
     82     char buf[4096];
     83     snprintf(buf, sizeof(buf), "%s-f%d.so", argv[0], lib);
     84     void *handle = dlopen(buf, RTLD_LAZY);
     85     if (!handle) {
     86       fprintf(stderr, "%s\n", dlerror());
     87       exit(1);
     88     }
     89     snprintf(buf, sizeof(buf), "f%d", lib);
     90     Functions[lib] = (f_t)dlsym(handle, buf);
     91     if (!Functions[lib]) {
     92       fprintf(stderr, "%s\n", dlerror());
     93       exit(1);
     94     }
     95     fprintf(stderr, "LIB[%03d] %s: %p\n", lib, buf, Functions[lib]);
     96     PrintStuff(0);
     97 
     98     int i;
     99     for (i = 0; i < num_threads; i++) {
    100       pthread_t t;
    101       fprintf(stderr, "Creating thread %d\n", i);
    102       pthread_create(&t, 0, PrintStuff, 0);
    103       pthread_join(t, 0);
    104     }
    105   }
    106   return 0;
    107 }
    108 #else  // SO_NAME
    109 #ifndef DTLS_SIZE
    110 # define DTLS_SIZE (1 << 17)
    111 #endif
    112 __thread void *huge_thread_local_array[DTLS_SIZE];
    113 void **SO_NAME() {
    114   return &huge_thread_local_array[0];
    115 }
    116 #endif
    117