Home | History | Annotate | Download | only in TestCases
      1 // Regression test for
      2 // https://code.google.com/p/address-sanitizer/issues/detail?id=178
      3 
      4 // RUN: %clangxx_asan -O0 %p/SharedLibs/init-order-dlopen-so.cc \
      5 // RUN:     -fPIC -shared -o %t-so.so
      6 // If the linker doesn't support --export-dynamic (which is ELF-specific),
      7 // try to link without that option.
      8 // FIXME: find a better solution.
      9 // RUN: %clangxx_asan -O0 %s -o %t -Wl,--export-dynamic || \
     10 // RUN:     %clangxx_asan -O0 %s -o %t
     11 // RUN: ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true %t 2>&1 | FileCheck %s
     12 #include <dlfcn.h>
     13 #include <pthread.h>
     14 #include <stdio.h>
     15 #include <unistd.h>
     16 
     17 #include <string>
     18 
     19 using std::string;
     20 
     21 int foo() {
     22   return 42;
     23 }
     24 int global = foo();
     25 
     26 __attribute__((visibility("default")))
     27 void inc_global() {
     28   global++;
     29 }
     30 
     31 void *global_poller(void *arg) {
     32   while (true) {
     33     if (global != 42)
     34       break;
     35     usleep(100);
     36   }
     37   return 0;
     38 }
     39 
     40 int main(int argc, char *argv[]) {
     41   pthread_t p;
     42   pthread_create(&p, 0, global_poller, 0);
     43   string path = string(argv[0]) + "-so.so";
     44   if (0 == dlopen(path.c_str(), RTLD_NOW)) {
     45     fprintf(stderr, "dlerror: %s\n", dlerror());
     46     return 1;
     47   }
     48   pthread_join(p, 0);
     49   printf("PASSED\n");
     50   // CHECK: PASSED
     51   return 0;
     52 }
     53