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
6 #include <sanitizer/dfsan_interface.h>
10 #include <sys/types.h>
14 // Compile this code without DFSan to avoid recursive instrumentation.
16 void my_dfsan_conditional_callback(dfsan_label Label
, dfsan_origin Origin
) {
32 fprintf(stderr
, "Label %u used as condition\n", Label
);
36 // Compile this code with DFSan and -dfsan-conditional-callbacks to insert the
39 extern void my_dfsan_conditional_callback(dfsan_label Label
,
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
60 int main(int Argc
, char *Argv
[]) {
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
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);
86 // CHECK: Label 4 used as condition
90 // Flush should clear the conditional signals seen.
92 assert(dfsan_get_labels_in_signal_conditional() == 0);
96 #endif // #ifdef CALLBACKS