xref: /llvm-project/compiler-rt/test/asan/TestCases/Posix/coverage-module-unloaded.cpp (revision ad14b5b008e2f643cb989ec645f12bf26a8659bb)
1 // Check that unloading a module doesn't break coverage dumping for remaining
2 // modules.
3 // RUN: %clangxx_asan -fsanitize-coverage=func,trace-pc-guard -DSHARED %s -shared -o %dynamiclib1 -fPIC
4 // RUN: %clangxx_asan -fsanitize-coverage=func,trace-pc-guard -DSHARED %s -shared -o %dynamiclib2 -fPIC
5 // RUN: %clangxx_asan -fsanitize-coverage=func,trace-pc-guard %s %libdl -o %t.exe
6 // RUN: mkdir -p %t.tmp/coverage-module-unloaded && cd %t.tmp/coverage-module-unloaded
7 // RUN: %env_asan_opts=coverage=1:verbosity=1 %run %t.exe %dynamiclib1 %dynamiclib2 2>&1        | FileCheck %s
8 // RUN: %env_asan_opts=coverage=1:verbosity=1 %run %t.exe %dynamiclib1 %dynamiclib2 foo 2>&1    | FileCheck %s
9 //
10 // https://code.google.com/p/address-sanitizer/issues/detail?id=263
11 // XFAIL: android
12 // UNSUPPORTED: ios
13 
14 #include <assert.h>
15 #include <dlfcn.h>
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 
20 #ifdef SHARED
21 extern "C" {
bar()22 void bar() { printf("bar\n"); }
23 }
24 #else
25 
main(int argc,char ** argv)26 int main(int argc, char **argv) {
27   fprintf(stderr, "PID: %d\n", getpid());
28   assert(argc > 2);
29   void *handle1 = dlopen(argv[1], RTLD_LAZY);  // %dynamiclib1
30   assert(handle1);
31   void (*bar1)() = (void (*)())dlsym(handle1, "bar");
32   assert(bar1);
33   bar1();
34   void *handle2 = dlopen(argv[2], RTLD_LAZY);  // %dynamiclib2
35   assert(handle2);
36   void (*bar2)() = (void (*)())dlsym(handle2, "bar");
37   assert(bar2);
38   bar2();
39 
40   // It matters whether the unloaded module has a higher or lower address range
41   // than the remaining one. Make sure to test both cases.
42   bool lt = reinterpret_cast<uintptr_t>(bar1) < reinterpret_cast<uintptr_t>(bar2);
43   if (argc < 2)
44     dlclose(lt ? handle1 : handle2);
45   else
46     dlclose(lt ? handle2 : handle1);
47   return 0;
48 }
49 #endif
50 
51 // CHECK: PID: [[PID:[0-9]+]]
52 // CHECK-DAG: exe{{.*}}[[PID]].sancov: {{.*}}PCs written
53 // CHECK-DAG: dynamic{{.*}}[[PID]].sancov: {{.*}}PCs written
54