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