[LVI] Add trunc to i1 handling. (#124480)
[llvm-project.git] / compiler-rt / test / asan / TestCases / Posix / dlclose-test.cpp
blobdb5507a2ea963b33dfa3ec4776d09f747ee4e9f5
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
12 // This subtle test assumes that after a foo.so is dlclose-d
13 // we can mmap the region of memory that was occupied by the library.
14 // This test works on x86 and Darwin, but not confirmed working anywhere else.
15 // REQUIRES: x86-target-arch || darwin
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
26 // XFAIL: i386-netbsd
28 #if !defined(SHARED_LIB)
29 #include <assert.h>
30 #include <dlfcn.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <sys/mman.h>
34 #include <unistd.h>
36 #include <string>
38 #if defined(__FreeBSD__)
39 // The MAP_NORESERVE define has been removed in FreeBSD 11.x, and even before
40 // that, it was never implemented. So just define it to zero.
41 #undef MAP_NORESERVE
42 #define MAP_NORESERVE 0
43 #endif
45 using std::string;
47 typedef int *(fun_t)();
49 int main(int argc, char *argv[]) {
50 string path = string(argv[0]) + "-so.so";
51 size_t PageSize = sysconf(_SC_PAGESIZE);
52 printf("opening %s ... \n", path.c_str());
53 void *lib = dlopen(path.c_str(), RTLD_NOW);
54 if (!lib) {
55 printf("error in dlopen(): %s\n", dlerror());
56 return 1;
58 fun_t *get = (fun_t*)dlsym(lib, "get_address_of_static_var");
59 if (!get) {
60 printf("failed dlsym\n");
61 return 1;
63 int *addr = get();
64 assert(((size_t)addr % 32) == 0); // should be 32-byte aligned.
65 printf("addr: %p\n", addr);
66 addr[0] = 1; // make sure we can write there.
68 // Now dlclose the shared library.
69 printf("attempting to dlclose\n");
70 if (dlclose(lib)) {
71 printf("failed to dlclose\n");
72 return 1;
74 // Now, the page where 'addr' is unmapped. Map it.
75 size_t page_beg = ((size_t)addr) & ~(PageSize - 1);
76 void *res = mmap((void*)(page_beg), PageSize,
77 PROT_READ | PROT_WRITE,
78 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, -1, 0);
79 if (res == (char*)-1L) {
80 printf("failed to mmap\n");
81 return 1;
83 addr[1] = 2; // BOOM (if the bug is not fixed).
84 printf("PASS\n");
85 // CHECK: PASS
86 return 0;
88 #else // SHARED_LIB
89 #include <stdio.h>
91 static int pad1;
92 static int static_var;
93 static int pad2;
95 extern "C"
96 int *get_address_of_static_var() {
97 return &static_var;
100 __attribute__((constructor))
101 void at_dlopen() {
102 printf("%s: I am being dlopened\n", __FILE__);
104 __attribute__((destructor))
105 void at_dlclose() {
106 printf("%s: I am being dlclosed\n", __FILE__);
108 #endif // SHARED_LIB