Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / dfsan / conditional_callbacks_sig.c
blob218c02c3eadbd8268064af2102417a0ac0bd484c
1 // RUN: %clang_dfsan -fno-sanitize=dataflow -O2 -fPIE -DCALLBACKS -c %s -o %t-callbacks.o
2 // RUN: %clang_dfsan -fsanitize-ignorelist=%S/Inputs/flags_abilist.txt -O2 -mllvm -dfsan-conditional-callbacks %s %t-callbacks.o -o %t
3 // RUN: %run %t FooBarBaz 2>&1 | FileCheck %s
5 #include <assert.h>
6 #include <sanitizer/dfsan_interface.h>
7 #include <signal.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <unistd.h>
13 #ifdef CALLBACKS
14 // Compile this code without DFSan to avoid recursive instrumentation.
16 void my_dfsan_conditional_callback(dfsan_label Label, dfsan_origin Origin) {
17 assert(Label != 0);
18 assert(Origin == 0);
20 static int Count = 0;
21 switch (Count++) {
22 case 0:
23 assert(Label == 1);
24 break;
25 case 1:
26 assert(Label == 4);
27 break;
28 default:
29 break;
32 fprintf(stderr, "Label %u used as condition\n", Label);
35 #else
36 // Compile this code with DFSan and -dfsan-conditional-callbacks to insert the
37 // callbacks.
39 extern void my_dfsan_conditional_callback(dfsan_label Label,
40 dfsan_origin Origin);
42 volatile int x = 0;
43 volatile int y = 1;
44 volatile int z = 0;
46 void SignalHandler(int signo) {
47 assert(dfsan_get_label(x) == 0);
48 assert(dfsan_get_label(y) != 0);
49 assert(dfsan_get_label(z) != 0);
50 // Running the conditional callback from a signal handler is risky,
51 // because the code must be written with signal handler context in mind.
52 // Instead dfsan_get_labels_in_signal_conditional() will indicate labels
53 // used in conditions inside signal handlers.
54 // CHECK-NOT: Label 8 used as condition
55 if (z != 0) {
56 x = y;
60 int main(int Argc, char *Argv[]) {
61 assert(Argc >= 1);
62 int unknown = (Argv[0][0] != 0) ? 1 : 0;
63 dfsan_set_label(1, &unknown, sizeof(unknown));
65 dfsan_set_conditional_callback(my_dfsan_conditional_callback);
67 // CHECK: Label 1 used as condition
68 if (unknown) {
69 z = 42;
72 assert(dfsan_get_labels_in_signal_conditional() == 0);
73 dfsan_set_label(4, (void *)&y, sizeof(y));
74 dfsan_set_label(8, (void *)&z, sizeof(z));
76 struct sigaction sa = {};
77 sa.sa_handler = SignalHandler;
78 int r = sigaction(SIGHUP, &sa, NULL);
79 assert(dfsan_get_label(r) == 0);
81 kill(getpid(), SIGHUP);
82 signal(SIGHUP, SIG_DFL);
84 assert(dfsan_get_labels_in_signal_conditional() == 8);
85 assert(x == 1);
86 // CHECK: Label 4 used as condition
87 if (x != 0) {
88 z = 123;
90 // Flush should clear the conditional signals seen.
91 dfsan_flush();
92 assert(dfsan_get_labels_in_signal_conditional() == 0);
93 return 0;
96 #endif // #ifdef CALLBACKS