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
10int foo(int x) { return x + 1; }
11
12int bar(int (*fn)(int), int val) { return fn(val); }
13
14void sigHandler(int signum) { bar(fn: foo, val: 3); }
15
16int 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/*
39REQUIRES: system-linux,bolt-runtime,lit-max-individual-test-time
40
41RUN: %clang %cflags %s -o %t.exe -Wl,-q -pie -fpie
42
43RUN: llvm-bolt %t.exe --instrument --instrumentation-file=%t.fdata \
44RUN: --instrumentation-wait-forks=1 --conservative-instrumentation \
45RUN: -o %t.instrumented_conservative
46
47# Instrumented program needs to finish returning zero
48RUN: %t.instrumented_conservative | FileCheck %s -check-prefix=CHECK-OUTPUT
49
50RUN: llvm-bolt %t.exe --instrument --instrumentation-file=%t.fdata \
51RUN: --instrumentation-wait-forks=1 \
52RUN: -o %t.instrumented
53
54# Instrumented program needs to finish returning zero
55RUN: %t.instrumented | FileCheck %s -check-prefix=CHECK-OUTPUT
56
57# Test that the instrumented data makes sense
58RUN: llvm-bolt %t.exe -o %t.bolted --data %t.fdata \
59RUN: --reorder-blocks=ext-tsp --reorder-functions=hfsort+ \
60RUN: --print-only=interp --print-finalized
61
62RUN: %t.bolted | FileCheck %s -check-prefix=CHECK-OUTPUT
63
64CHECK-OUTPUT: [child]
65CHECK-OUTPUT: [parent]
66*/
67

source code of bolt/test/runtime/X86/instrumentation-indirect.c