1 // RUN: %clangxx_tsan -O1 %s -DBUILD_SO -fPIC -shared -o %t-so.so
2 // RUN: %clangxx_tsan -O1 %s %link_libcxx_tsan -o %t && %run %t 2>&1 | FileCheck %s
3 
4 // dl_iterate_phdr doesn't exist on OS X.
5 // UNSUPPORTED: darwin
6 
7 #ifdef BUILD_SO
8 
9 #include "test.h"
10 
11 int exported_var = 0;
12 
13 #else  // BUILD_SO
14 
15 #include "test.h"
16 #include <dlfcn.h>
17 #include <link.h>
18 #include <string.h>
19 #include <string>
20 
callback(struct dl_phdr_info * info,size_t size,void * data)21 static int callback(struct dl_phdr_info *info, size_t size, void *data) {
22   if (info->dlpi_name[0] == '\0')
23     info->dlpi_name = "/proc/self/exe";
24   return !strcmp(info->dlpi_name, "non existent module");
25 }
26 
thread(void * unused)27 void *thread(void *unused) {
28   for (int i = 0; i < 1000; i++) {
29     barrier_wait(&barrier);
30     dl_iterate_phdr(callback, 0);
31   }
32   return 0;
33 }
34 
main(int argc,char * argv[])35 int main(int argc, char *argv[]) {
36   barrier_init(&barrier, 2);
37   std::string path = std::string(argv[0]) + std::string("-so.so");
38   pthread_t th;
39   pthread_create(&th, 0, thread, 0);
40   for (int i = 0; i < 1000; i++) {
41     barrier_wait(&barrier);
42     void *lib = dlopen(path.c_str(), RTLD_NOW);
43     if (!lib) {
44       printf("error in dlopen: %s\n", dlerror());
45       return 1;
46     }
47     dlclose(lib);
48   }
49   pthread_join(th, 0);
50   fprintf(stderr, "DONE\n");
51   return 0;
52 }
53 
54 #endif  // BUILD_SO
55 
56 // CHECK-NOT: WARNING: ThreadSanitizer: data race
57 // CHECK: DONE
58