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 %p/SharedLibs/dlclose-test-so.cc \
18// RUN:     -fPIC -shared -o %t-so.so
19// RUN: %clangxx_asan -O0 %s -o %t && %t 2>&1 | FileCheck %s
20// RUN: %clangxx_asan -O1 %p/SharedLibs/dlclose-test-so.cc \
21// RUN:     -fPIC -shared -o %t-so.so
22// RUN: %clangxx_asan -O1 %s -o %t && %t 2>&1 | FileCheck %s
23// RUN: %clangxx_asan -O2 %p/SharedLibs/dlclose-test-so.cc \
24// RUN:     -fPIC -shared -o %t-so.so
25// RUN: %clangxx_asan -O2 %s -o %t && %t 2>&1 | FileCheck %s
26// RUN: %clangxx_asan -O3 %p/SharedLibs/dlclose-test-so.cc \
27// RUN:     -fPIC -shared -o %t-so.so
28// RUN: %clangxx_asan -O3 %s -o %t && %t 2>&1 | FileCheck %s
29
30#include <assert.h>
31#include <dlfcn.h>
32#include <stdio.h>
33#include <string.h>
34#include <sys/mman.h>
35#include <unistd.h>
36
37#include <string>
38
39using std::string;
40
41typedef int *(fun_t)();
42
43int main(int argc, char *argv[]) {
44  string path = string(argv[0]) + "-so.so";
45  size_t PageSize = sysconf(_SC_PAGESIZE);
46  printf("opening %s ... \n", path.c_str());
47  void *lib = dlopen(path.c_str(), RTLD_NOW);
48  if (!lib) {
49    printf("error in dlopen(): %s\n", dlerror());
50    return 1;
51  }
52  fun_t *get = (fun_t*)dlsym(lib, "get_address_of_static_var");
53  if (!get) {
54    printf("failed dlsym\n");
55    return 1;
56  }
57  int *addr = get();
58  assert(((size_t)addr % 32) == 0);  // should be 32-byte aligned.
59  printf("addr: %p\n", addr);
60  addr[0] = 1;  // make sure we can write there.
61
62  // Now dlclose the shared library.
63  printf("attempting to dlclose\n");
64  if (dlclose(lib)) {
65    printf("failed to dlclose\n");
66    return 1;
67  }
68  // Now, the page where 'addr' is unmapped. Map it.
69  size_t page_beg = ((size_t)addr) & ~(PageSize - 1);
70  void *res = mmap((void*)(page_beg), PageSize,
71                   PROT_READ | PROT_WRITE,
72                   MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, 0, 0);
73  if (res == (char*)-1L) {
74    printf("failed to mmap\n");
75    return 1;
76  }
77  addr[1] = 2;  // BOOM (if the bug is not fixed).
78  printf("PASS\n");
79  // CHECK: PASS
80  return 0;
81}
82