| 1 | /* Checks that BOLT correctly handles instrumentation of indirect calls |
| 2 | * including case with indirect calls in signals handlers. |
| 3 | */ |
| 4 | #include <signal.h> |
| 5 | #include <stdio.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/wait.h> |
| 8 | #include <unistd.h> |
| 9 | |
| 10 | int foo(int x) { return x + 1; } |
| 11 | |
| 12 | int bar(int (*fn)(int), int val) { return fn(val); } |
| 13 | |
| 14 | void sigHandler(int signum) { bar(fn: foo, val: 3); } |
| 15 | |
| 16 | int main(int argc, char **argv) { |
| 17 | long long i; |
| 18 | pid_t pid, wpid; |
| 19 | int wstatus; |
| 20 | signal(SIGUSR1, handler: sigHandler); |
| 21 | pid = fork(); |
| 22 | if (pid) { |
| 23 | do { |
| 24 | kill(pid: pid, SIGUSR1); |
| 25 | usleep(useconds: 0); |
| 26 | wpid = waitpid(pid: pid, stat_loc: &wstatus, WNOHANG); |
| 27 | } while (wpid == 0); |
| 28 | printf(format: "[parent]\n" ); |
| 29 | } else { |
| 30 | for (i = 0; i < 100000; i++) { |
| 31 | bar(fn: foo, val: i % 10); |
| 32 | } |
| 33 | printf(format: "[child]\n" ); |
| 34 | } |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | REQUIRES: system-linux,bolt-runtime,lit-max-individual-test-time |
| 40 | |
| 41 | RUN: %clang %cflags -D_GNU_SOURCE %s -o %t.exe -Wl,-q -pie -fpie |
| 42 | |
| 43 | RUN: llvm-bolt %t.exe --instrument --instrumentation-file=%t.fdata \ |
| 44 | RUN: --instrumentation-wait-forks=1 --conservative-instrumentation \ |
| 45 | RUN: -o %t.instrumented_conservative |
| 46 | |
| 47 | # Instrumented program needs to finish returning zero |
| 48 | RUN: %t.instrumented_conservative | FileCheck %s -check-prefix=CHECK-OUTPUT |
| 49 | |
| 50 | RUN: llvm-bolt %t.exe --instrument --instrumentation-file=%t.fdata \ |
| 51 | RUN: --instrumentation-wait-forks=1 \ |
| 52 | RUN: -o %t.instrumented |
| 53 | |
| 54 | # Instrumented program needs to finish returning zero |
| 55 | RUN: %t.instrumented | FileCheck %s -check-prefix=CHECK-OUTPUT |
| 56 | |
| 57 | # Test that the instrumented data makes sense |
| 58 | RUN: llvm-bolt %t.exe -o %t.bolted --data %t.fdata \ |
| 59 | RUN: --reorder-blocks=ext-tsp --reorder-functions=hfsort+ \ |
| 60 | RUN: --print-only=interp --print-finalized |
| 61 | |
| 62 | RUN: %t.bolted | FileCheck %s -check-prefix=CHECK-OUTPUT |
| 63 | |
| 64 | CHECK-OUTPUT: [child] |
| 65 | CHECK-OUTPUT: [parent] |
| 66 | */ |
| 67 | |