1// Check that unloading a module doesn't break coverage dumping for remaining
2// modules.
3// RUN: %clangxx_asan -mllvm -asan-coverage=1 -DSHARED %s -shared -o %T/libcoverage_module_unloaded_test_1.so -fPIC
4// RUN: %clangxx_asan -mllvm -asan-coverage=1 -DSHARED %s -shared -o %T/libcoverage_module_unloaded_test_2.so -fPIC
5// RUN: %clangxx_asan -mllvm -asan-coverage=1 -DSO_DIR=\"%T\" %s -o %t
6// RUN: export ASAN_OPTIONS=coverage=1:verbosity=1
7// RUN: mkdir -p %T/coverage-module-unloaded && cd %T/coverage-module-unloaded
8// RUN: %run %t 2>&1         | FileCheck %s
9// RUN: %run %t foo 2>&1         | FileCheck %s
10// RUN: cd .. && rm coverage-module-unloaded -r
11//
12// https://code.google.com/p/address-sanitizer/issues/detail?id=263
13// XFAIL: android
14
15#include <assert.h>
16#include <dlfcn.h>
17#include <stdio.h>
18#include <unistd.h>
19
20#ifdef SHARED
21extern "C" {
22void bar() { printf("bar\n"); }
23}
24#else
25
26int main(int argc, char **argv) {
27  fprintf(stderr, "PID: %d\n", getpid());
28  void *handle1 =
29      dlopen(SO_DIR "/libcoverage_module_unloaded_test_1.so", RTLD_LAZY);
30  assert(handle1);
31  void (*bar1)() = (void (*)())dlsym(handle1, "bar");
32  assert(bar1);
33  bar1();
34  void *handle2 =
35      dlopen(SO_DIR "/libcoverage_module_unloaded_test_2.so", RTLD_LAZY);
36  assert(handle2);
37  void (*bar2)() = (void (*)())dlsym(handle2, "bar");
38  assert(bar2);
39  bar2();
40
41  // It matters whether the unloaded module has a higher or lower address range
42  // than the remaining one. Make sure to test both cases.
43  if (argc < 2)
44    dlclose(bar1 < bar2 ? handle1 : handle2);
45  else
46    dlclose(bar1 < bar2 ? handle2 : handle1);
47  return 0;
48}
49#endif
50
51// CHECK: PID: [[PID:[0-9]+]]
52// CHECK: [[PID]].sancov: 1 PCs written
53// CHECK: .so.[[PID]]
54// If we get coverage for both DSOs, it means the module wasn't unloaded and
55// this test is useless.
56// CHECK-NOT: .so.[[PID]]
57