blob: d492af6662fa58b4fee7a75fe63e39e054ab83ef [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001// Check that unloading a module doesn't break coverage dumping for remaining
2// modules.
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -07003// RUN: %clangxx_asan -fsanitize-coverage=func -DSHARED %s -shared -o %dynamiclib1 -fPIC
4// RUN: %clangxx_asan -fsanitize-coverage=func -DSHARED %s -shared -o %dynamiclib2 -fPIC
5// RUN: %clangxx_asan -fsanitize-coverage=func %s %libdl -o %t
Stephen Hines2d1fdb22014-05-28 23:58:16 -07006// RUN: mkdir -p %T/coverage-module-unloaded && cd %T/coverage-module-unloaded
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08007// RUN: %env_asan_opts=coverage=1:verbosity=1 %run %t %dynamiclib1 %dynamiclib2 2>&1 | FileCheck %s
8// RUN: %env_asan_opts=coverage=1:verbosity=1 %run %t %dynamiclib1 %dynamiclib2 foo 2>&1 | FileCheck %s
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07009// RUN: rm -r %T/coverage-module-unloaded
Stephen Hines2d1fdb22014-05-28 23:58:16 -070010//
11// https://code.google.com/p/address-sanitizer/issues/detail?id=263
12// XFAIL: android
13
14#include <assert.h>
15#include <dlfcn.h>
16#include <stdio.h>
17#include <unistd.h>
18
19#ifdef SHARED
20extern "C" {
21void bar() { printf("bar\n"); }
22}
23#else
24
25int main(int argc, char **argv) {
26 fprintf(stderr, "PID: %d\n", getpid());
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070027 assert(argc > 2);
28 void *handle1 = dlopen(argv[1], RTLD_LAZY); // %dynamiclib1
Stephen Hines2d1fdb22014-05-28 23:58:16 -070029 assert(handle1);
30 void (*bar1)() = (void (*)())dlsym(handle1, "bar");
31 assert(bar1);
32 bar1();
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070033 void *handle2 = dlopen(argv[2], RTLD_LAZY); // %dynamiclib2
Stephen Hines2d1fdb22014-05-28 23:58:16 -070034 assert(handle2);
35 void (*bar2)() = (void (*)())dlsym(handle2, "bar");
36 assert(bar2);
37 bar2();
38
39 // It matters whether the unloaded module has a higher or lower address range
40 // than the remaining one. Make sure to test both cases.
41 if (argc < 2)
42 dlclose(bar1 < bar2 ? handle1 : handle2);
43 else
44 dlclose(bar1 < bar2 ? handle2 : handle1);
45 return 0;
46}
47#endif
48
49// CHECK: PID: [[PID:[0-9]+]]
50// CHECK: [[PID]].sancov: 1 PCs written
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070051// CHECK: coverage-module-unloaded{{.*}}1.[[PID]]
52// CHECK: coverage-module-unloaded{{.*}}2.[[PID]]
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070053// Even though we've unloaded one of the libs we still dump the coverage file
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070054// for that lib (although the data will be inaccurate, if at all useful)