Home | History | Annotate | Download | only in TestCases
      1 // Regression test for
      2 // http://code.google.com/p/address-sanitizer/issues/detail?id=19
      3 // Bug description:
      4 // 1. application dlopens foo.so
      5 // 2. asan registers all globals from foo.so
      6 // 3. application dlcloses foo.so
      7 // 4. application mmaps some memory to the location where foo.so was before
      8 // 5. application starts using this mmaped memory, but asan still thinks there
      9 // are globals.
     10 // 6. BOOM
     11 
     12 // This sublte test assumes that after a foo.so is dlclose-d
     13 // we can mmap the region of memory that has been occupied by the library.
     14 // It works on i368/x86_64 Linux, but not necessary anywhere else.
     15 // REQUIRES: x86_64-supported-target,i386-supported-target
     16 
     17 // RUN: %clangxx_asan -O0 -DSHARED_LIB %s -fPIC -shared -o %t-so.so
     18 // RUN: %clangxx_asan -O0 %s -ldl -o %t && %run %t 2>&1 | FileCheck %s
     19 // RUN: %clangxx_asan -O1 -DSHARED_LIB %s -fPIC -shared -o %t-so.so
     20 // RUN: %clangxx_asan -O1 %s -ldl -o %t && %run %t 2>&1 | FileCheck %s
     21 // RUN: %clangxx_asan -O2 -DSHARED_LIB %s -fPIC -shared -o %t-so.so
     22 // RUN: %clangxx_asan -O2 %s -ldl -o %t && %run %t 2>&1 | FileCheck %s
     23 // RUN: %clangxx_asan -O3 -DSHARED_LIB %s -fPIC -shared -o %t-so.so
     24 // RUN: %clangxx_asan -O3 %s -ldl -o %t && %run %t 2>&1 | FileCheck %s
     25 
     26 #if !defined(SHARED_LIB)
     27 #include <assert.h>
     28 #include <dlfcn.h>
     29 #include <stdio.h>
     30 #include <string.h>
     31 #include <sys/mman.h>
     32 #include <unistd.h>
     33 
     34 #include <string>
     35 
     36 using std::string;
     37 
     38 typedef int *(fun_t)();
     39 
     40 int main(int argc, char *argv[]) {
     41   string path = string(argv[0]) + "-so.so";
     42   size_t PageSize = sysconf(_SC_PAGESIZE);
     43   printf("opening %s ... \n", path.c_str());
     44   void *lib = dlopen(path.c_str(), RTLD_NOW);
     45   if (!lib) {
     46     printf("error in dlopen(): %s\n", dlerror());
     47     return 1;
     48   }
     49   fun_t *get = (fun_t*)dlsym(lib, "get_address_of_static_var");
     50   if (!get) {
     51     printf("failed dlsym\n");
     52     return 1;
     53   }
     54   int *addr = get();
     55   assert(((size_t)addr % 32) == 0);  // should be 32-byte aligned.
     56   printf("addr: %p\n", addr);
     57   addr[0] = 1;  // make sure we can write there.
     58 
     59   // Now dlclose the shared library.
     60   printf("attempting to dlclose\n");
     61   if (dlclose(lib)) {
     62     printf("failed to dlclose\n");
     63     return 1;
     64   }
     65   // Now, the page where 'addr' is unmapped. Map it.
     66   size_t page_beg = ((size_t)addr) & ~(PageSize - 1);
     67   void *res = mmap((void*)(page_beg), PageSize,
     68                    PROT_READ | PROT_WRITE,
     69                    MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, 0, 0);
     70   if (res == (char*)-1L) {
     71     printf("failed to mmap\n");
     72     return 1;
     73   }
     74   addr[1] = 2;  // BOOM (if the bug is not fixed).
     75   printf("PASS\n");
     76   // CHECK: PASS
     77   return 0;
     78 }
     79 #else  // SHARED_LIB
     80 #include <stdio.h>
     81 
     82 static int pad1;
     83 static int static_var;
     84 static int pad2;
     85 
     86 extern "C"
     87 int *get_address_of_static_var() {
     88   return &static_var;
     89 }
     90 
     91 __attribute__((constructor))
     92 void at_dlopen() {
     93   printf("%s: I am being dlopened\n", __FILE__);
     94 }
     95 __attribute__((destructor))
     96 void at_dlclose() {
     97   printf("%s: I am being dlclosed\n", __FILE__);
     98 }
     99 #endif  // SHARED_LIB
    100