| 1 | /// atexit(3) not supported in dlopen(3)ed+dlclose(3)d DSO |
| 2 | // XFAIL: target={{.*netbsd.*}} |
| 3 | // XFAIL: target={{.*haiku.*}} |
| 4 | |
| 5 | // RUN: mkdir -p %t.d && cd %t.d |
| 6 | |
| 7 | // RUN: echo 'void func1(int k) {}' > func1.c |
| 8 | // RUN: echo 'void func2(int k) {}' > func2.c |
| 9 | // RUN: echo 'void func3(int k) {}' > func3.c |
| 10 | // RUN: %clang --coverage -fPIC -shared func1.c -o func1.so -dumpdir ./ |
| 11 | // RUN: %clang --coverage -fPIC -shared func2.c -o func2.so -dumpdir ./ |
| 12 | // RUN: %clang --coverage -fPIC -shared func3.c -o func3.so -dumpdir ./ |
| 13 | // RUN: %clang --coverage -fPIC -rpath %t.d %s -o %t -dumpdir ./ |
| 14 | |
| 15 | /// Test with two dlopened libraries. |
| 16 | // RUN: rm -f gcov-dlopen.gcda func1.gcda func2.gcda |
| 17 | // RUN: %run %t |
| 18 | // RUN: llvm-cov gcov -t gcov-dlopen.gcda | FileCheck %s |
| 19 | // RUN: llvm-cov gcov -t func1.gcda | FileCheck %s --check-prefix=FUNC1 |
| 20 | // RUN: llvm-cov gcov -t func2.gcda | FileCheck %s --check-prefix=FUNC2 |
| 21 | |
| 22 | // FUNC1: 1: 1:void func1(int k) {} |
| 23 | // FUNC2: 1: 1:void func2(int k) {} |
| 24 | |
| 25 | /// Test with three dlopened libraries. |
| 26 | // RUN: %clang -DUSE_LIB3 --coverage -fPIC -rpath %t.d %s -o %t -dumpdir ./ |
| 27 | // RUN: rm -f gcov-dlopen.gcda func1.gcda func2.gcda func3.gcda |
| 28 | // RUN: %run %t |
| 29 | // RUN: llvm-cov gcov -t gcov-dlopen.gcda | FileCheck %s --check-prefix=LIB3 |
| 30 | // RUN: llvm-cov gcov -t func1.gcda | FileCheck %s --check-prefix=FUNC1 |
| 31 | // RUN: llvm-cov gcov -t func2.gcda | FileCheck %s --check-prefix=FUNC2 |
| 32 | // RUN: llvm-cov gcov -t func3.gcda | FileCheck %s --check-prefix=FUNC3 |
| 33 | |
| 34 | // FUNC3: 1: 1:void func3(int k) {} |
| 35 | |
| 36 | #include <dlfcn.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | |
| 40 | int main(int argc, char *argv[]) { |
| 41 | void *f1_handle = dlopen(file: "func1.so" , RTLD_LAZY | RTLD_GLOBAL); |
| 42 | if (f1_handle == NULL) |
| 43 | return fprintf(stderr, format: "unable to open 'func1.so': %s\n" , dlerror()); |
| 44 | void (*func1)(void) = (void (*)(void))dlsym(handle: f1_handle, name: "func1" ); |
| 45 | if (func1 == NULL) |
| 46 | return fprintf(stderr, format: "unable to lookup symbol 'func1': %s\n" , dlerror()); |
| 47 | |
| 48 | void *f2_handle = dlopen(file: "func2.so" , RTLD_LAZY | RTLD_GLOBAL); |
| 49 | if (f2_handle == NULL) |
| 50 | return fprintf(stderr, format: "unable to open 'func2.so': %s\n" , dlerror()); |
| 51 | void (*func2)(void) = (void (*)(void))dlsym(handle: f2_handle, name: "func2" ); |
| 52 | if (func2 == NULL) |
| 53 | return fprintf(stderr, format: "unable to lookup symbol 'func2': %s\n" , dlerror()); |
| 54 | func2(); |
| 55 | |
| 56 | #ifdef USE_LIB3 |
| 57 | // CHECK: -: [[#@LINE+2]]: void *f3_handle |
| 58 | // LIB3: 1: [[#@LINE+1]]: void *f3_handle |
| 59 | void *f3_handle = dlopen("func3.so" , RTLD_LAZY | RTLD_GLOBAL); |
| 60 | if (f3_handle == NULL) |
| 61 | return fprintf(stderr, "unable to open 'func3.so': %s\n" , dlerror()); |
| 62 | void (*func3)(void) = (void (*)(void))dlsym(f3_handle, "func3" ); |
| 63 | if (func3 == NULL) |
| 64 | return fprintf(stderr, "unable to lookup symbol 'func3': %s\n" , dlerror()); |
| 65 | func3(); |
| 66 | #endif |
| 67 | |
| 68 | void (*gcov_reset1)() = (void (*)())dlsym(handle: f1_handle, name: "__gcov_reset" ); |
| 69 | if (gcov_reset1 == NULL) |
| 70 | return fprintf(stderr, format: "unable to find __gcov_reset in func1.so': %s\n" , dlerror()); |
| 71 | void (*gcov_reset2)() = (void (*)())dlsym(handle: f2_handle, name: "__gcov_reset" ); |
| 72 | if (gcov_reset2 == NULL) |
| 73 | return fprintf(stderr, format: "unable to find __gcov_reset in func2.so': %s\n" , dlerror()); |
| 74 | if (gcov_reset1 == gcov_reset2) |
| 75 | return fprintf(stderr, format: "same __gcov_reset found in func1.so and func2.so\n" ); |
| 76 | |
| 77 | /// Test that __gcov_dump is in the dynamic symbol table. |
| 78 | void (*gcov_dump1)() = (void (*)())dlsym(handle: f1_handle, name: "__gcov_dump" ); |
| 79 | if (gcov_dump1 == NULL) |
| 80 | return fprintf(stderr, format: "unable to find __gcov_dump in func1.so': %s\n" , dlerror()); |
| 81 | |
| 82 | if (dlclose(handle: f2_handle) != 0) |
| 83 | return fprintf(stderr, format: "unable to close 'func2.so': %s\n" , dlerror()); |
| 84 | |
| 85 | func1(); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |