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 x86 Linux, but not necessary anywhere else. 15// REQUIRES: x86-target-arch 16 17// RUN: %clangxx_asan -O0 -DSHARED_LIB %s -fPIC -shared -o %t-so.so 18// RUN: %clangxx_asan -O0 %s %libdl -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 %libdl -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 %libdl -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 %libdl -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#if defined(__FreeBSD__) 37// The MAP_NORESERVE define has been removed in FreeBSD 11.x, and even before 38// that, it was never implemented. So just define it to zero. 39#undef MAP_NORESERVE 40#define MAP_NORESERVE 0 41#endif 42 43using std::string; 44 45typedef int *(fun_t)(); 46 47int main(int argc, char *argv[]) { 48 string path = string(argv[0]) + "-so.so"; 49 size_t PageSize = sysconf(_SC_PAGESIZE); 50 printf("opening %s ... \n", path.c_str()); 51 void *lib = dlopen(path.c_str(), RTLD_NOW); 52 if (!lib) { 53 printf("error in dlopen(): %s\n", dlerror()); 54 return 1; 55 } 56 fun_t *get = (fun_t*)dlsym(lib, "get_address_of_static_var"); 57 if (!get) { 58 printf("failed dlsym\n"); 59 return 1; 60 } 61 int *addr = get(); 62 assert(((size_t)addr % 32) == 0); // should be 32-byte aligned. 63 printf("addr: %p\n", addr); 64 addr[0] = 1; // make sure we can write there. 65 66 // Now dlclose the shared library. 67 printf("attempting to dlclose\n"); 68 if (dlclose(lib)) { 69 printf("failed to dlclose\n"); 70 return 1; 71 } 72 // Now, the page where 'addr' is unmapped. Map it. 73 size_t page_beg = ((size_t)addr) & ~(PageSize - 1); 74 void *res = mmap((void*)(page_beg), PageSize, 75 PROT_READ | PROT_WRITE, 76 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, -1, 0); 77 if (res == (char*)-1L) { 78 printf("failed to mmap\n"); 79 return 1; 80 } 81 addr[1] = 2; // BOOM (if the bug is not fixed). 82 printf("PASS\n"); 83 // CHECK: PASS 84 return 0; 85} 86#else // SHARED_LIB 87#include <stdio.h> 88 89static int pad1; 90static int static_var; 91static int pad2; 92 93extern "C" 94int *get_address_of_static_var() { 95 return &static_var; 96} 97 98__attribute__((constructor)) 99void at_dlopen() { 100 printf("%s: I am being dlopened\n", __FILE__); 101} 102__attribute__((destructor)) 103void at_dlclose() { 104 printf("%s: I am being dlclosed\n", __FILE__); 105} 106#endif // SHARED_LIB 107